Debian Codegraph @

exporters/otlp/otlptrace/internal/tracetransform/span_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package tracetransform
5
6 import (
7 "testing"
8 "time"
9
10 "github.com/google/go-cmp/cmp"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
14 "google.golang.org/protobuf/proto"
15
16 "go.opentelemetry.io/otel/attribute"
17 "go.opentelemetry.io/otel/codes"
18 "go.opentelemetry.io/otel/sdk/instrumentation"
19 "go.opentelemetry.io/otel/sdk/resource"
20 tracesdk "go.opentelemetry.io/otel/sdk/trace"
21 "go.opentelemetry.io/otel/sdk/trace/tracetest"
22 semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
23 "go.opentelemetry.io/otel/trace"
24 )
25
26 func TestSpanKind(t *testing.T) {
27 for _, test := range []struct {
28 kind trace.SpanKind
29 expected tracepb.Span_SpanKind
30 }{
31 {
32 trace.SpanKindInternal,
33 tracepb.Span_SPAN_KIND_INTERNAL,
34 },
35 {
36 trace.SpanKindClient,
37 tracepb.Span_SPAN_KIND_CLIENT,
38 },
39 {
40 trace.SpanKindServer,
41 tracepb.Span_SPAN_KIND_SERVER,
42 },
43 {
44 trace.SpanKindProducer,
45 tracepb.Span_SPAN_KIND_PRODUCER,
46 },
47 {
48 trace.SpanKindConsumer,
49 tracepb.Span_SPAN_KIND_CONSUMER,
50 },
51 {
52 trace.SpanKind(-1),
53 tracepb.Span_SPAN_KIND_UNSPECIFIED,
54 },
55 } {
56 assert.Equal(t, test.expected, spanKind(test.kind))
57 }
58 }
59
60 func TestNilSpanEvent(t *testing.T) {
61 assert.Nil(t, spanEvents(nil))
62 }
63
64 func TestEmptySpanEvent(t *testing.T) {
65 assert.Nil(t, spanEvents([]tracesdk.Event{}))
66 }
67
68 func TestSpanEvent(t *testing.T) {
69 attrs := []attribute.KeyValue{attribute.Int("one", 1), attribute.Int("two", 2)}
70 eventTime := time.Date(2020, 5, 20, 0, 0, 0, 0, time.UTC)
71 negativeEventTime := time.Date(1969, 7, 20, 20, 17, 0, 0, time.UTC)
72 got := spanEvents([]tracesdk.Event{
73 {
74 Name: "test 1",
75 Attributes: []attribute.KeyValue{},
76 Time: eventTime,
77 },
78 {
79 Name: "test 2",
80 Attributes: attrs,
81 Time: eventTime,
82 DroppedAttributeCount: 2,
83 },
84 {
85 Name: "test 3",
86 Attributes: attrs,
87 Time: negativeEventTime,
88 DroppedAttributeCount: 2,
89 },
90 })
91 if !assert.Len(t, got, 3) {
92 return
93 }
94 eventTimestamp := uint64(1589932800 * 1e9)
95 assert.Equal(t, &tracepb.Span_Event{Name: "test 1", Attributes: nil, TimeUnixNano: eventTimestamp}, got[0])
96 // Do not test Attributes directly, just that the return value goes to the correct field.
97 assert.Equal(
98 t,
99 &tracepb.Span_Event{
100 Name: "test 2",
101 Attributes: KeyValues(attrs),
102 TimeUnixNano: eventTimestamp,
103 DroppedAttributesCount: 2,
104 },
105 got[1],
106 )
107 assert.Equal(
108 t,
109 &tracepb.Span_Event{Name: "test 3", Attributes: KeyValues(attrs), TimeUnixNano: 0, DroppedAttributesCount: 2},
110 got[2],
111 )
112 }
113
114 func TestNilLinks(t *testing.T) {
115 assert.Nil(t, links(nil))
116 }
117
118 func TestEmptyLinks(t *testing.T) {
119 assert.Nil(t, links([]tracesdk.Link{}))
120 }
121
122 func TestLinks(t *testing.T) {
123 attrs := []attribute.KeyValue{attribute.Int("one", 1), attribute.Int("two", 2)}
124 l := []tracesdk.Link{
125 {
126 DroppedAttributeCount: 3,
127 },
128 {
129 SpanContext: trace.SpanContext{},
130 Attributes: attrs,
131 DroppedAttributeCount: 3,
132 },
133 }
134 got := links(l)
135
136 // Make sure we get the same number back first.
137 if !assert.Len(t, got, 2) {
138 return
139 }
140
141 // Empty should be empty.
142 expected := &tracepb.Span_Link{
143 TraceId: []uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
144 SpanId: []uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
145 DroppedAttributesCount: 3,
146 Flags: 0x100,
147 }
148 assert.Equal(t, expected, got[0])
149
150 // Do not test Attributes directly, just that the return value goes to the correct field.
151 expected.Attributes = KeyValues(attrs)
152 assert.Equal(t, expected, got[1])
153
154 // Changes to our links should not change the produced links.
155 l[1].SpanContext = l[1].SpanContext.WithTraceID(trace.TraceID{})
156 assert.Equal(t, expected, got[1])
157 assert.Equal(t, l[1].DroppedAttributeCount, int(got[1].DroppedAttributesCount))
158 }
159
160 func TestStatus(t *testing.T) {
161 for _, test := range []struct {
162 code codes.Code
163 message string
164 otlpStatus tracepb.Status_StatusCode
165 }{
166 {
167 codes.Ok,
168 "test Ok",
169 tracepb.Status_STATUS_CODE_OK,
170 },
171 {
172 codes.Unset,
173 "test Unset",
174 tracepb.Status_STATUS_CODE_UNSET,
175 },
176 {
177 message: "default code is unset",
178 otlpStatus: tracepb.Status_STATUS_CODE_UNSET,
179 },
180 {
181 codes.Error,
182 "test Error",
183 tracepb.Status_STATUS_CODE_ERROR,
184 },
185 } {
186 expected := &tracepb.Status{Code: test.otlpStatus, Message: test.message}
187 assert.Equal(t, expected, status(test.code, test.message))
188 }
189 }
190
191 func TestBuildSpanFlags(t *testing.T) {
192 for _, tt := range []struct {
193 name string
194 spanContext trace.SpanContext
195 wantFlags uint32
196 }{
197 {
198 name: "with an empty span context",
199 wantFlags: 0x100,
200 },
201 {
202 name: "with a remote span context",
203 spanContext: trace.NewSpanContext(trace.SpanContextConfig{
204 Remote: true,
205 }),
206 wantFlags: 0x300,
207 },
208 } {
209 t.Run(tt.name, func(t *testing.T) {
210 assert.Equal(t, tt.wantFlags, buildSpanFlagsWith(tt.spanContext.TraceFlags(), tt.spanContext))
211 })
212 }
213 }
214
215 func TestSpanFlagsLower8BitsFromTraceFlags(t *testing.T) {
216 for _, tc := range []struct {
217 name string
218 traceFlags trace.TraceFlags
219 parentRemote bool
220 wantLow8 uint32
221 wantMask uint32
222 }{
223 {name: "unsampled root", traceFlags: 0x00, parentRemote: false, wantLow8: 0x00, wantMask: 0x100},
224 {name: "sampled root", traceFlags: 0x01, parentRemote: false, wantLow8: 0x01, wantMask: 0x100},
225 {name: "custom bits root", traceFlags: 0x05, parentRemote: false, wantLow8: 0x05, wantMask: 0x100},
226 {name: "unsampled remote parent", traceFlags: 0x00, parentRemote: true, wantLow8: 0x00, wantMask: 0x300},
227 {name: "sampled remote parent", traceFlags: 0x01, parentRemote: true, wantLow8: 0x01, wantMask: 0x300},
228 } {
229 t.Run(tc.name, func(t *testing.T) {
230 parent := trace.NewSpanContext(trace.SpanContextConfig{Remote: tc.parentRemote})
231 got := buildSpanFlagsWith(tc.traceFlags, parent)
232 assert.Equal(t, tc.wantLow8, got&0xff)
233 assert.Equal(t, tc.wantMask, got&0x300)
234 // Ensure higher bits are not set beyond 0-9
235 assert.Equal(t, uint32(0), got&^uint32(0x3ff))
236 })
237 }
238 }
239
240 func TestSpanAndLinkExportLower8Bits(t *testing.T) {
241 // Span: sampled child with local parent
242 spanData := tracetest.SpanStub{
243 SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
244 TraceID: trace.TraceID{0x1},
245 SpanID: trace.SpanID{0x2},
246 TraceFlags: trace.TraceFlags(0x01),
247 }),
248 Parent: trace.NewSpanContext(trace.SpanContextConfig{}),
249 Name: "flags-test",
250 }
251 rss := Spans(tracetest.SpanStubs{spanData}.Snapshots())
252 require.Len(t, rss, 1)
253 scopeSpans := rss[0].GetScopeSpans()
254 require.Len(t, scopeSpans, 1)
255 require.Len(t, scopeSpans[0].Spans, 1)
256 s := scopeSpans[0].Spans[0]
257 assert.Equal(t, uint32(0x01), s.Flags&0xff)
258 assert.Equal(t, uint32(0x100), s.Flags&0x300)
259
260 // Link: sampled link local
261 l := []tracesdk.Link{
262 {SpanContext: trace.NewSpanContext(trace.SpanContextConfig{TraceFlags: 0x01})},
263 }
264 gotLinks := links(l)
265 require.Len(t, gotLinks, 1)
266 assert.Equal(t, uint32(0x01), gotLinks[0].Flags&0xff)
267 assert.Equal(t, uint32(0x100), gotLinks[0].Flags&0x300)
268 }
269
270 func TestNilSpan(t *testing.T) {
271 assert.Nil(t, span(nil))
272 }
273
274 func TestNilSpanData(t *testing.T) {
275 assert.Nil(t, Spans(nil))
276 }
277
278 func TestEmptySpanData(t *testing.T) {
279 assert.Nil(t, Spans(nil))
280 }
281
282 func TestSpanData(t *testing.T) {
283 // Full test of span data
284
285 // March 31, 2020 5:01:26 1234nanos (UTC)
286 startTime := time.Unix(1585674086, 1234)
287 endTime := startTime.Add(10 * time.Second)
288 traceState, _ := trace.ParseTraceState("key1=val1,key2=val2")
289 spanData := tracetest.SpanStub{
290 SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
291 TraceID: trace.TraceID{
292 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
293 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
294 },
295 SpanID: trace.SpanID{0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8},
296 TraceState: traceState,
297 }),
298 Parent: trace.NewSpanContext(trace.SpanContextConfig{
299 TraceID: trace.TraceID{
300 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
301 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
302 },
303 SpanID: trace.SpanID{0xEF, 0xEE, 0xED, 0xEC, 0xEB, 0xEA, 0xE9, 0xE8},
304 TraceState: traceState,
305 Remote: true,
306 }),
307 SpanKind: trace.SpanKindServer,
308 Name: "span data to span data",
309 StartTime: startTime,
310 EndTime: endTime,
311 Events: []tracesdk.Event{
312 {
313 Time: startTime,
314 Attributes: []attribute.KeyValue{
315 attribute.Int64("CompressedByteSize", 512),
316 },
317 },
318 {
319 Time: endTime,
320 Attributes: []attribute.KeyValue{
321 attribute.String("EventType", "Recv"),
322 },
323 },
324 },
325 Links: []tracesdk.Link{
326 {
327 SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
328 TraceID: trace.TraceID{
329 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
330 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
331 },
332 SpanID: trace.SpanID{0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7},
333 TraceFlags: 0,
334 }),
335 Attributes: []attribute.KeyValue{
336 attribute.String("LinkType", "Parent"),
337 },
338 DroppedAttributeCount: 0,
339 },
340 {
341 SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
342 TraceID: trace.TraceID{
343 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
344 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
345 },
346 SpanID: trace.SpanID{0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7},
347 TraceFlags: 0,
348 }),
349 Attributes: []attribute.KeyValue{
350 attribute.String("LinkType", "Child"),
351 },
352 DroppedAttributeCount: 0,
353 },
354 },
355 Status: tracesdk.Status{
356 Code: codes.Error,
357 Description: "utterly unrecognized",
358 },
359 Attributes: []attribute.KeyValue{
360 attribute.Int64("timeout_ns", 12e9),
361 },
362 DroppedAttributes: 1,
363 DroppedEvents: 2,
364 DroppedLinks: 3,
365 Resource: resource.NewWithAttributes(
366 "http://example.com/custom-resource-schema",
367 attribute.String("rk1", "rv1"),
368 attribute.Int64("rk2", 5),
369 attribute.StringSlice("rk3", []string{"sv1", "sv2"}),
370 ),
371 InstrumentationScope: instrumentation.Scope{
372 Name: "go.opentelemetry.io/test/otel",
373 Version: "v0.0.1",
374 SchemaURL: semconv.SchemaURL,
375 },
376 }
377
378 // Not checking resource as the underlying map of our Resource makes
379 // ordering impossible to guarantee on the output. The Resource
380 // transform function has unit tests that should suffice.
381 expectedSpan := &tracepb.Span{
382 TraceId: []byte{
383 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
384 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
385 },
386 SpanId: []byte{0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8},
387 ParentSpanId: []byte{0xEF, 0xEE, 0xED, 0xEC, 0xEB, 0xEA, 0xE9, 0xE8},
388 TraceState: "key1=val1,key2=val2",
389 Flags: 0x300, // lower 8 bits (trace flags) are 0x00 in this fixture; update in new tests below
390 Name: spanData.Name,
391 Kind: tracepb.Span_SPAN_KIND_SERVER,
392 StartTimeUnixNano: uint64(startTime.UnixNano()),
393 EndTimeUnixNano: uint64(endTime.UnixNano()),
394 Status: status(spanData.Status.Code, spanData.Status.Description),
395 Events: spanEvents(spanData.Events),
396 Links: links(spanData.Links),
397 Attributes: KeyValues(spanData.Attributes),
398 DroppedAttributesCount: 1,
399 DroppedEventsCount: 2,
400 DroppedLinksCount: 3,
401 }
402
403 got := Spans(tracetest.SpanStubs{spanData}.Snapshots())
404 require.Len(t, got, 1)
405
406 assert.Equal(t, got[0].GetResource(), Resource(spanData.Resource))
407 assert.Equal(t, got[0].SchemaUrl, spanData.Resource.SchemaURL())
408 scopeSpans := got[0].GetScopeSpans()
409 require.Len(t, scopeSpans, 1)
410 assert.Equal(t, scopeSpans[0].SchemaUrl, spanData.InstrumentationScope.SchemaURL)
411 assert.Equal(t, scopeSpans[0].GetScope(), InstrumentationScope(spanData.InstrumentationScope))
412 require.Len(t, scopeSpans[0].Spans, 1)
413 actualSpan := scopeSpans[0].Spans[0]
414
415 if diff := cmp.Diff(expectedSpan, actualSpan, cmp.Comparer(proto.Equal)); diff != "" {
416 t.Fatalf("transformed span differs %v\n", diff)
417 }
418 }
419
420 // Empty parent span ID should be treated as root span.
421 func TestRootSpanData(t *testing.T) {
422 sd := Spans(tracetest.SpanStubs{
423 {},
424 }.Snapshots())
425 require.Len(t, sd, 1)
426 rs := sd[0]
427 scopeSpans := rs.GetScopeSpans()
428 require.Len(t, scopeSpans, 1)
429 got := scopeSpans[0].GetSpans()[0].GetParentSpanId()
430
431 // Empty means root span.
432 assert.Nil(t, got, "incorrect transform of root parent span ID")
433 }
434
435 func TestSpanDataNilResource(t *testing.T) {
436 assert.NotPanics(t, func() {
437 Spans(tracetest.SpanStubs{
438 {},
439 }.Snapshots())
440 })
441 }
442
443 func BenchmarkSpans(b *testing.B) {
444 records := []tracesdk.ReadOnlySpan{
445 tracetest.SpanStub{
446 Attributes: []attribute.KeyValue{
447 attribute.String("a", "b"),
448 attribute.String("b", "b"),
449 attribute.String("c", "b"),
450 attribute.String("d", "b"),
451 },
452 Links: []tracesdk.Link{
453 {},
454 {},
455 {},
456 {},
457 {},
458 },
459 }.Snapshot(),
460 }
461
462 b.ResetTimer()
463 b.ReportAllocs()
464 b.RunParallel(func(pb *testing.PB) {
465 var out []*tracepb.ResourceSpans
466 for pb.Next() {
467 out = Spans(records)
468 }
469 _ = out
470 })
471 }