-
Notifications
You must be signed in to change notification settings - Fork 415
/
elastictrace_test.go
172 lines (158 loc) · 4.36 KB
/
elastictrace_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
package elastic
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer"
)
const debug = false
const (
elasticV6URL = "http://127.0.0.1:9202"
elasticV7URL = "http://127.0.0.1:9203"
)
func TestMain(m *testing.M) {
_, ok := os.LookupEnv("INTEGRATION")
if !ok {
fmt.Println("--- SKIP: to enable integration test, set the INTEGRATION environment variable")
os.Exit(0)
}
os.Exit(m.Run())
}
func checkPUTTrace(assert *assert.Assertions, mt mocktracer.Tracer) {
span := mt.FinishedSpans()[1]
assert.Equal("my-es-service", span.Tag(ext.ServiceName))
assert.Equal("PUT /twitter/tweet/?", span.Tag(ext.ResourceName))
assert.Equal("/twitter/tweet/1", span.Tag("elasticsearch.url"))
assert.Equal("PUT", span.Tag("elasticsearch.method"))
assert.Equal(`{"user": "test", "message": "hello"}`, span.Tag("elasticsearch.body"))
assert.Equal("elastic/go-elasticsearch.v6", span.Tag(ext.Component))
assert.Equal(ext.SpanKindClient, span.Tag(ext.SpanKind))
}
func checkGETTrace(assert *assert.Assertions, mt mocktracer.Tracer) {
span := mt.FinishedSpans()[0]
assert.Equal("my-es-service", span.Tag(ext.ServiceName))
assert.Equal("GET /twitter/tweet/?", span.Tag(ext.ResourceName))
assert.Equal("/twitter/tweet/1", span.Tag("elasticsearch.url"))
assert.Equal("GET", span.Tag("elasticsearch.method"))
assert.Equal("elastic/go-elasticsearch.v6", span.Tag(ext.Component))
assert.Equal(ext.SpanKindClient, span.Tag(ext.SpanKind))
}
func checkErrTrace(assert *assert.Assertions, mt mocktracer.Tracer) {
span := mt.FinishedSpans()[0]
assert.Equal("my-es-service", span.Tag(ext.ServiceName))
assert.Equal("GET /not-real-index/_doc/?", span.Tag(ext.ResourceName))
assert.Equal("/not-real-index/_doc/1", span.Tag("elasticsearch.url"))
assert.NotEmpty(span.Tag(ext.Error))
assert.Equal("*errors.errorString", fmt.Sprintf("%T", span.Tag(ext.Error).(error)))
assert.Equal("elastic/go-elasticsearch.v6", span.Tag(ext.Component))
assert.Equal(ext.SpanKindClient, span.Tag(ext.SpanKind))
}
func TestQuantize(t *testing.T) {
for _, tc := range []struct {
url, method string
expected string
}{
{
url: "/twitter/tweets",
method: "POST",
expected: "POST /twitter/tweets",
},
{
url: "/logs_2016_05/event/_search",
method: "GET",
expected: "GET /logs_?_?/event/_search",
},
{
url: "/twitter/tweets/123",
method: "GET",
expected: "GET /twitter/tweets/?",
},
{
url: "/logs_2016_05/event/123",
method: "PUT",
expected: "PUT /logs_?_?/event/?",
},
} {
assert.Equal(t, tc.expected, quantize(tc.url, tc.method))
}
}
func TestPeek(t *testing.T) {
assert := assert.New(t)
for _, tt := range [...]struct {
max int // content length
txt string // stream
n int // bytes to peek at
snip string // expected snippet
err error // expected error
}{
0: {
// extract 3 bytes from a content of length 7
txt: "ABCDEFG",
max: 7,
n: 3,
snip: "ABC",
},
1: {
// extract 7 bytes from a content of length 7
txt: "ABCDEFG",
max: 7,
n: 7,
snip: "ABCDEFG",
},
2: {
// extract 100 bytes from a content of length 9 (impossible scenario)
txt: "ABCDEFG",
max: 9,
n: 100,
snip: "ABCDEFG",
},
3: {
// extract 5 bytes from a content of length 2 (impossible scenario)
txt: "ABCDEFG",
max: 2,
n: 5,
snip: "AB",
},
4: {
txt: "ABCDEFG",
max: 0,
n: 1,
snip: "A",
},
5: {
n: 4,
max: 4,
err: errors.New("empty stream"),
},
6: {
txt: "ABCDEFG",
n: 4,
max: -1,
snip: "ABCD",
},
} {
var readcloser io.ReadCloser
if tt.txt != "" {
readcloser = io.NopCloser(bytes.NewBufferString(tt.txt))
}
snip, rc, err := peek(readcloser, "", tt.max, tt.n)
assert.Equal(tt.err, err)
assert.Equal(tt.snip, snip)
if readcloser != nil {
// if a non-nil io.ReadCloser was sent, the returned io.ReadCloser
// must always return the entire original content.
all, err := io.ReadAll(rc)
assert.Nil(err)
assert.Equal(tt.txt, string(all))
}
}
}