1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package trace
16
17 import (
18 "testing"
19 "time"
20
21 "github.com/stretchr/testify/assert"
22
23 "go.opentelemetry.io/otel/attribute"
24 )
25
26 func TestNewSpanConfig(t *testing.T) {
27 k1v1 := attribute.String("key1", "value1")
28 k1v2 := attribute.String("key1", "value2")
29 k2v2 := attribute.String("key2", "value2")
30
31 timestamp0 := time.Unix(0, 0)
32 timestamp1 := time.Unix(0, 0)
33
34 link1 := Link{
35 SpanContext: SpanContext{traceID: TraceID([16]byte{1, 1}), spanID: SpanID{3}},
36 Attributes: []attribute.KeyValue{k1v1},
37 }
38 link2 := Link{
39 SpanContext: SpanContext{traceID: TraceID([16]byte{1, 1}), spanID: SpanID{3}},
40 Attributes: []attribute.KeyValue{k1v2, k2v2},
41 }
42
43 tests := []struct {
44 options []SpanStartOption
45 expected SpanConfig
46 }{
47 {
48
49 []SpanStartOption{},
50 SpanConfig{},
51 },
52 {
53 []SpanStartOption{
54 WithAttributes(k1v1),
55 },
56 SpanConfig{
57 attributes: []attribute.KeyValue{k1v1},
58 },
59 },
60 {
61
62 []SpanStartOption{
63 WithAttributes(k1v1),
64 WithAttributes(k1v2),
65 WithAttributes(k2v2),
66 },
67 SpanConfig{
68
69 attributes: []attribute.KeyValue{k1v1, k1v2, k2v2},
70 },
71 },
72 {
73 []SpanStartOption{
74 WithAttributes(k1v1, k1v2, k2v2),
75 },
76 SpanConfig{
77
78 attributes: []attribute.KeyValue{k1v1, k1v2, k2v2},
79 },
80 },
81 {
82 []SpanStartOption{
83 WithTimestamp(timestamp0),
84 },
85 SpanConfig{
86 timestamp: timestamp0,
87 },
88 },
89 {
90 []SpanStartOption{
91
92 WithTimestamp(timestamp0),
93 WithTimestamp(timestamp1),
94 },
95 SpanConfig{
96 timestamp: timestamp1,
97 },
98 },
99 {
100 []SpanStartOption{
101 WithLinks(link1),
102 },
103 SpanConfig{
104 links: []Link{link1},
105 },
106 },
107 {
108 []SpanStartOption{
109
110 WithLinks(link1),
111 WithLinks(link1, link2),
112 },
113 SpanConfig{
114
115 links: []Link{link1, link1, link2},
116 },
117 },
118 {
119 []SpanStartOption{
120 WithNewRoot(),
121 },
122 SpanConfig{
123 newRoot: true,
124 },
125 },
126 {
127 []SpanStartOption{
128
129 WithNewRoot(),
130 WithNewRoot(),
131 },
132 SpanConfig{
133 newRoot: true,
134 },
135 },
136 {
137 []SpanStartOption{
138 WithSpanKind(SpanKindConsumer),
139 },
140 SpanConfig{
141 spanKind: SpanKindConsumer,
142 },
143 },
144 {
145 []SpanStartOption{
146
147 WithSpanKind(SpanKindClient),
148 WithSpanKind(SpanKindConsumer),
149 },
150 SpanConfig{
151 spanKind: SpanKindConsumer,
152 },
153 },
154 {
155
156 []SpanStartOption{
157 WithAttributes(k1v1),
158 WithTimestamp(timestamp0),
159 WithLinks(link1, link2),
160 WithNewRoot(),
161 WithSpanKind(SpanKindConsumer),
162 },
163 SpanConfig{
164 attributes: []attribute.KeyValue{k1v1},
165 timestamp: timestamp0,
166 links: []Link{link1, link2},
167 newRoot: true,
168 spanKind: SpanKindConsumer,
169 },
170 },
171 }
172 for _, test := range tests {
173 assert.Equal(t, test.expected, NewSpanStartConfig(test.options...))
174 }
175 }
176
177 func TestEndSpanConfig(t *testing.T) {
178 timestamp := time.Unix(0, 0)
179
180 tests := []struct {
181 options []SpanEndOption
182 expected SpanConfig
183 }{
184 {
185 []SpanEndOption{},
186 SpanConfig{},
187 },
188 {
189 []SpanEndOption{
190 WithStackTrace(true),
191 },
192 SpanConfig{
193 stackTrace: true,
194 },
195 },
196 {
197 []SpanEndOption{
198 WithTimestamp(timestamp),
199 },
200 SpanConfig{
201 timestamp: timestamp,
202 },
203 },
204 }
205 for _, test := range tests {
206 assert.Equal(t, test.expected, NewSpanEndConfig(test.options...))
207 }
208 }
209
210 func TestTracerConfig(t *testing.T) {
211 v1 := "semver:0.0.1"
212 v2 := "semver:1.0.0"
213 schemaURL := "https://opentelemetry.io/schemas/1.2.0"
214 attrs := attribute.NewSet(
215 attribute.String("user", "alice"),
216 attribute.Bool("admin", true),
217 )
218
219 c := NewTracerConfig(
220
221 WithInstrumentationVersion(v1),
222 WithInstrumentationVersion(v2),
223 WithSchemaURL(schemaURL),
224 WithInstrumentationAttributes(attrs.ToSlice()...),
225 )
226
227 assert.Equal(t, v2, c.InstrumentationVersion(), "instrumentation version")
228 assert.Equal(t, schemaURL, c.SchemaURL(), "schema URL")
229 assert.Equal(t, attrs, c.InstrumentationAttributes(), "instrumentation attributes")
230 }
231
232
233
234 var (
235 tracerConfig TracerConfig
236 spanConfig SpanConfig
237 eventConfig EventConfig
238 )
239
240 func BenchmarkNewTracerConfig(b *testing.B) {
241 opts := []TracerOption{
242 WithInstrumentationVersion("testing version"),
243 WithSchemaURL("testing URL"),
244 }
245
246 b.ReportAllocs()
247 b.ResetTimer()
248
249 for i := 0; i < b.N; i++ {
250 tracerConfig = NewTracerConfig(opts...)
251 }
252 }
253
254 func BenchmarkNewSpanStartConfig(b *testing.B) {
255 opts := []SpanStartOption{
256 WithAttributes(attribute.Bool("key", true)),
257 WithTimestamp(time.Now()),
258 WithLinks(Link{}),
259 WithNewRoot(),
260 WithSpanKind(SpanKindClient),
261 }
262
263 b.ReportAllocs()
264 b.ResetTimer()
265
266 for i := 0; i < b.N; i++ {
267 spanConfig = NewSpanStartConfig(opts...)
268 }
269 }
270
271 func BenchmarkNewSpanEndConfig(b *testing.B) {
272 opts := []SpanEndOption{
273 WithTimestamp(time.Now()),
274 WithStackTrace(true),
275 }
276
277 b.ReportAllocs()
278 b.ResetTimer()
279
280 for i := 0; i < b.N; i++ {
281 spanConfig = NewSpanEndConfig(opts...)
282 }
283 }
284
285 func BenchmarkNewEventConfig(b *testing.B) {
286 opts := []EventOption{
287 WithAttributes(attribute.Bool("key", true)),
288 WithTimestamp(time.Now()),
289 WithStackTrace(true),
290 }
291
292 b.ReportAllocs()
293 b.ResetTimer()
294
295 for i := 0; i < b.N; i++ {
296 eventConfig = NewEventConfig(opts...)
297 }
298 }
299
View as plain text