Debian Codegraph @

trace/trace_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package trace
5
6 import (
7 "bytes"
8 "testing"
9
10 "github.com/google/go-cmp/cmp"
11 "github.com/stretchr/testify/assert"
12 )
13
14 func TestSpanContextIsValid(t *testing.T) {
15 for _, testcase := range []struct {
16 name string
17 tid TraceID
18 sid SpanID
19 want bool
20 }{
21 {
22 name: "SpanContext.IsValid() returns true if sc has both an Trace ID and Span ID",
23 tid: [16]byte{1},
24 sid: [8]byte{42},
25 want: true,
26 }, {
27 name: "SpanContext.IsValid() returns false if sc has neither an Trace ID nor Span ID",
28 tid: TraceID([16]byte{}),
29 sid: [8]byte{},
30 want: false,
31 }, {
32 name: "SpanContext.IsValid() returns false if sc has a Span ID but not a Trace ID",
33 tid: TraceID([16]byte{}),
34 sid: [8]byte{42},
35 want: false,
36 }, {
37 name: "SpanContext.IsValid() returns false if sc has a Trace ID but not a Span ID",
38 tid: TraceID([16]byte{1}),
39 sid: [8]byte{},
40 want: false,
41 },
42 } {
43 t.Run(testcase.name, func(t *testing.T) {
44 sc := SpanContext{
45 traceID: testcase.tid,
46 spanID: testcase.sid,
47 }
48 have := sc.IsValid()
49 if have != testcase.want {
50 t.Errorf("Want: %v, but have: %v", testcase.want, have)
51 }
52 })
53 }
54 }
55
56 func TestSpanContextEqual(t *testing.T) {
57 a := SpanContext{
58 traceID: [16]byte{1},
59 spanID: [8]byte{42},
60 }
61
62 b := SpanContext{
63 traceID: [16]byte{1},
64 spanID: [8]byte{42},
65 }
66
67 c := SpanContext{
68 traceID: [16]byte{2},
69 spanID: [8]byte{42},
70 }
71
72 if !a.Equal(b) {
73 t.Error("Want: true, but have: false")
74 }
75
76 if a.Equal(c) {
77 t.Error("Want: false, but have: true")
78 }
79 }
80
81 func TestSpanContextIsSampled(t *testing.T) {
82 for _, testcase := range []struct {
83 name string
84 tf TraceFlags
85 want bool
86 }{
87 {
88 name: "SpanContext.IsSampled() returns false if sc is not sampled",
89 want: false,
90 }, {
91 name: "SpanContext.IsSampled() returns true if sc is sampled",
92 tf: FlagsSampled,
93 want: true,
94 },
95 } {
96 t.Run(testcase.name, func(t *testing.T) {
97 sc := SpanContext{
98 traceFlags: testcase.tf,
99 }
100
101 have := sc.IsSampled()
102
103 if have != testcase.want {
104 t.Errorf("Want: %v, but have: %v", testcase.want, have)
105 }
106 })
107 }
108 }
109
110 func TestSpanContextIsRandom(t *testing.T) {
111 for _, testcase := range []struct {
112 name string
113 tf TraceFlags
114 want bool
115 }{
116 {
117 name: "SpanContext.IsRandom() returns false if sc is not random",
118 want: false,
119 }, {
120 name: "SpanContext.IsRandom() returns true if sc is random",
121 tf: FlagsRandom,
122 want: true,
123 },
124 } {
125 t.Run(testcase.name, func(t *testing.T) {
126 sc := SpanContext{
127 traceFlags: testcase.tf,
128 }
129
130 have := sc.IsRandom()
131
132 if have != testcase.want {
133 t.Errorf("Want: %v, but have: %v", testcase.want, have)
134 }
135 })
136 }
137 }
138
139 func TestSpanContextIsRemote(t *testing.T) {
140 for _, testcase := range []struct {
141 name string
142 remote bool
143 want bool
144 }{
145 {
146 name: "SpanContext.IsRemote() returns false if sc is not remote",
147 want: false,
148 }, {
149 name: "SpanContext.IsRemote() returns true if sc is remote",
150 remote: true,
151 want: true,
152 },
153 } {
154 t.Run(testcase.name, func(t *testing.T) {
155 sc := SpanContext{
156 remote: testcase.remote,
157 }
158
159 have := sc.IsRemote()
160
161 if have != testcase.want {
162 t.Errorf("Want: %v, but have: %v", testcase.want, have)
163 }
164 })
165 }
166 }
167
168 func TestSpanContextMarshalJSON(t *testing.T) {
169 for _, testcase := range []struct {
170 name string
171 tid TraceID
172 sid SpanID
173 tstate TraceState
174 tflags TraceFlags
175 isRemote bool
176 want []byte
177 }{
178 {
179 name: "SpanContext.MarshalJSON() returns json with partial data",
180 tid: [16]byte{1},
181 sid: [8]byte{42},
182 want: []byte(`{"TraceID":"01000000000000000000000000000000","SpanID":"2a00000000000000","TraceFlags":"00","TraceState":"","Remote":false}`),
183 },
184 {
185 name: "SpanContext.MarshalJSON() returns json with full data",
186 tid: [16]byte{1},
187 sid: [8]byte{42},
188 tflags: FlagsSampled,
189 isRemote: true,
190 tstate: TraceState{list: []member{
191 {Key: "foo", Value: "1"},
192 }},
193 want: []byte(`{"TraceID":"01000000000000000000000000000000","SpanID":"2a00000000000000","TraceFlags":"01","TraceState":"foo=1","Remote":true}`),
194 },
195 } {
196 t.Run(testcase.name, func(t *testing.T) {
197 sc := SpanContext{
198 traceID: testcase.tid,
199 spanID: testcase.sid,
200 traceFlags: testcase.tflags,
201 traceState: testcase.tstate,
202 remote: testcase.isRemote,
203 }
204 have, err := sc.MarshalJSON()
205 if err != nil {
206 t.Errorf("Marshaling failed: %v", err)
207 }
208
209 if !bytes.Equal(have, testcase.want) {
210 t.Errorf("Want: %v, but have: %v", string(testcase.want), string(have))
211 }
212 })
213 }
214 }
215
216 func TestSpanIDFromHex(t *testing.T) {
217 for _, testcase := range []struct {
218 name string
219 hex string
220 sid SpanID
221 valid bool
222 }{
223 {
224 name: "Valid SpanID",
225 sid: SpanID([8]byte{42}),
226 hex: "2a00000000000000",
227 valid: true,
228 }, {
229 name: "Invalid SpanID with invalid length",
230 hex: "80f198ee56343ba",
231 valid: false,
232 }, {
233 name: "Invalid SpanID with invalid char",
234 hex: "80f198ee563433g7",
235 valid: false,
236 }, {
237 name: "Invalid SpanID with uppercase",
238 hex: "80f198ee53ba86F7",
239 valid: false,
240 }, {
241 name: "Invalid SpanID with zero value",
242 hex: "0000000000000000",
243 valid: false,
244 },
245 } {
246 t.Run(testcase.name, func(t *testing.T) {
247 sid, err := SpanIDFromHex(testcase.hex)
248
249 if testcase.valid && err != nil {
250 t.Errorf("Expected SpanID %s to be valid but end with error %s", testcase.hex, err.Error())
251 } else if !testcase.valid && err == nil {
252 t.Errorf("Expected SpanID %s to be invalid but end no error", testcase.hex)
253 }
254
255 if sid != testcase.sid {
256 t.Errorf("Want: %v, but have: %v", testcase.sid, sid)
257 }
258 })
259 }
260 }
261
262 func TestIsValidFromHex(t *testing.T) {
263 for _, testcase := range []struct {
264 name string
265 hex string
266 tid TraceID
267 valid bool
268 }{
269 {
270 name: "Valid TraceID",
271 tid: TraceID([16]byte{128, 241, 152, 238, 86, 52, 59, 168, 100, 254, 139, 42, 87, 211, 239, 247}),
272 hex: "80f198ee56343ba864fe8b2a57d3eff7",
273 valid: true,
274 }, {
275 name: "Invalid TraceID with invalid length",
276 hex: "80f198ee56343ba864fe8b2a57d3eff",
277 valid: false,
278 }, {
279 name: "Invalid TraceID with invalid char",
280 hex: "80f198ee56343ba864fe8b2a57d3efg7",
281 valid: false,
282 }, {
283 name: "Invalid TraceID with uppercase",
284 hex: "80f198ee56343ba864fe8b2a57d3efF7",
285 valid: false,
286 }, {
287 name: "Invalid TraceID with zero value",
288 hex: "00000000000000000000000000000000",
289 valid: false,
290 },
291 } {
292 t.Run(testcase.name, func(t *testing.T) {
293 tid, err := TraceIDFromHex(testcase.hex)
294
295 if testcase.valid && err != nil {
296 t.Errorf("Expected TraceID %s to be valid but end with error %s", testcase.hex, err.Error())
297 }
298
299 if !testcase.valid && err == nil {
300 t.Errorf("Expected TraceID %s to be invalid but end no error", testcase.hex)
301 }
302
303 if tid != testcase.tid {
304 t.Errorf("Want: %v, but have: %v", testcase.tid, tid)
305 }
306 })
307 }
308 }
309
310 func TestSpanContextHasTraceID(t *testing.T) {
311 for _, testcase := range []struct {
312 name string
313 tid TraceID
314 want bool
315 }{
316 {
317 name: "SpanContext.HasTraceID() returns true if both Low and High are nonzero",
318 tid: TraceID([16]byte{1}),
319 want: true,
320 }, {
321 name: "SpanContext.HasTraceID() returns false if neither Low nor High are nonzero",
322 tid: TraceID{},
323 want: false,
324 },
325 } {
326 t.Run(testcase.name, func(t *testing.T) {
327 // proto: func (sc SpanContext) HasTraceID() bool{}
328 sc := SpanContext{traceID: testcase.tid}
329 have := sc.HasTraceID()
330 if have != testcase.want {
331 t.Errorf("Want: %v, but have: %v", testcase.want, have)
332 }
333 })
334 }
335 }
336
337 func TestSpanContextHasSpanID(t *testing.T) {
338 for _, testcase := range []struct {
339 name string
340 sc SpanContext
341 want bool
342 }{
343 {
344 name: "SpanContext.HasSpanID() returns true if self.SpanID != 0",
345 sc: SpanContext{spanID: [8]byte{42}},
346 want: true,
347 }, {
348 name: "SpanContext.HasSpanID() returns false if self.SpanID == 0",
349 sc: SpanContext{},
350 want: false,
351 },
352 } {
353 t.Run(testcase.name, func(t *testing.T) {
354 // proto: func (sc SpanContext) HasSpanID() bool {}
355 have := testcase.sc.HasSpanID()
356 if have != testcase.want {
357 t.Errorf("Want: %v, but have: %v", testcase.want, have)
358 }
359 })
360 }
361 }
362
363 func TestTraceFlagsIsSampled(t *testing.T) {
364 for _, testcase := range []struct {
365 name string
366 tf TraceFlags
367 want bool
368 }{
369 {
370 name: "sampled",
371 tf: FlagsSampled,
372 want: true,
373 }, {
374 name: "unused bits are ignored, still not sampled",
375 tf: ^FlagsSampled,
376 want: false,
377 }, {
378 name: "unused bits are ignored, still sampled",
379 tf: FlagsSampled | ^FlagsSampled,
380 want: true,
381 }, {
382 name: "not sampled/default",
383 want: false,
384 },
385 } {
386 t.Run(testcase.name, func(t *testing.T) {
387 have := testcase.tf.IsSampled()
388 if have != testcase.want {
389 t.Errorf("Want: %v, but have: %v", testcase.want, have)
390 }
391 })
392 }
393 }
394
395 func TestTraceFlagsWithSampled(t *testing.T) {
396 for _, testcase := range []struct {
397 name string
398 start TraceFlags
399 sample bool
400 want TraceFlags
401 }{
402 {
403 name: "sampled unchanged",
404 start: FlagsSampled,
405 want: FlagsSampled,
406 sample: true,
407 }, {
408 name: "become sampled",
409 want: FlagsSampled,
410 sample: true,
411 }, {
412 name: "unused bits are ignored, still not sampled",
413 start: ^FlagsSampled,
414 want: ^FlagsSampled,
415 sample: false,
416 }, {
417 name: "unused bits are ignored, becomes sampled",
418 start: ^FlagsSampled,
419 want: FlagsSampled | ^FlagsSampled,
420 sample: true,
421 }, {
422 name: "not sampled/default",
423 sample: false,
424 },
425 } {
426 t.Run(testcase.name, func(t *testing.T) {
427 have := testcase.start.WithSampled(testcase.sample)
428 if have != testcase.want {
429 t.Errorf("Want: %v, but have: %v", testcase.want, have)
430 }
431 })
432 }
433 }
434
435 func TestTraceFlagsIsRandom(t *testing.T) {
436 for _, testcase := range []struct {
437 name string
438 tf TraceFlags
439 want bool
440 }{
441 {
442 name: "random",
443 tf: FlagsRandom,
444 want: true,
445 }, {
446 name: "unused bits are ignored, still not random",
447 tf: ^FlagsRandom,
448 want: false,
449 }, {
450 name: "unused bits are ignored, still random",
451 tf: FlagsRandom | ^FlagsRandom,
452 want: true,
453 }, {
454 name: "not random/default",
455 want: false,
456 },
457 } {
458 t.Run(testcase.name, func(t *testing.T) {
459 have := testcase.tf.IsRandom()
460 if have != testcase.want {
461 t.Errorf("Want: %v, but have: %v", testcase.want, have)
462 }
463 })
464 }
465 }
466
467 func TestTraceFlagsWithRandom(t *testing.T) {
468 for _, testcase := range []struct {
469 name string
470 start TraceFlags
471 random bool
472 want TraceFlags
473 }{
474 {
475 name: "random unchanged",
476 start: FlagsRandom,
477 want: FlagsRandom,
478 random: true,
479 }, {
480 name: "become random",
481 want: FlagsRandom,
482 random: true,
483 }, {
484 name: "unused bits are ignored, still not random",
485 start: ^FlagsRandom,
486 want: ^FlagsRandom,
487 random: false,
488 }, {
489 name: "unused bits are ignored, becomes random",
490 start: ^FlagsRandom,
491 want: FlagsRandom | ^FlagsRandom,
492 random: true,
493 }, {
494 name: "not random/default",
495 random: false,
496 },
497 } {
498 t.Run(testcase.name, func(t *testing.T) {
499 have := testcase.start.WithRandom(testcase.random)
500 if have != testcase.want {
501 t.Errorf("Want: %v, but have: %v", testcase.want, have)
502 }
503 })
504 }
505 }
506
507 func TestStringTraceID(t *testing.T) {
508 for _, testcase := range []struct {
509 name string
510 tid TraceID
511 want string
512 }{
513 {
514 name: "TraceID.String returns string representation of self.TraceID values > 0",
515 tid: TraceID([16]byte{255}),
516 want: "ff000000000000000000000000000000",
517 },
518 {
519 name: "TraceID.String returns string representation of self.TraceID values == 0",
520 tid: TraceID([16]byte{}),
521 want: "00000000000000000000000000000000",
522 },
523 } {
524 t.Run(testcase.name, func(t *testing.T) {
525 // proto: func (t TraceID) String() string {}
526 have := testcase.tid.String()
527 if have != testcase.want {
528 t.Errorf("Want: %s, but have: %s", testcase.want, have)
529 }
530 })
531 }
532 }
533
534 func TestStringSpanID(t *testing.T) {
535 for _, testcase := range []struct {
536 name string
537 sid SpanID
538 want string
539 }{
540 {
541 name: "SpanID.String returns string representation of self.SpanID values > 0",
542 sid: SpanID([8]byte{255}),
543 want: "ff00000000000000",
544 },
545 {
546 name: "SpanID.String returns string representation of self.SpanID values == 0",
547 sid: SpanID([8]byte{}),
548 want: "0000000000000000",
549 },
550 } {
551 t.Run(testcase.name, func(t *testing.T) {
552 // proto: func (t TraceID) String() string {}
553 have := testcase.sid.String()
554 if have != testcase.want {
555 t.Errorf("Want: %s, but have: %s", testcase.want, have)
556 }
557 })
558 }
559 }
560
561 func assertSpanContextEqual(got, want SpanContext) bool {
562 return got.spanID == want.spanID &&
563 got.traceID == want.traceID &&
564 got.traceFlags == want.traceFlags &&
565 got.remote == want.remote &&
566 got.traceState.String() == want.traceState.String()
567 }
568
569 func TestNewSpanContext(t *testing.T) {
570 testCases := []struct {
571 name string
572 config SpanContextConfig
573 expectedSpanContext SpanContext
574 }{
575 {
576 name: "Complete SpanContext",
577 config: SpanContextConfig{
578 TraceID: TraceID([16]byte{1}),
579 SpanID: SpanID([8]byte{42}),
580 TraceFlags: 0x1,
581 TraceState: TraceState{list: []member{
582 {"foo", "bar"},
583 }},
584 },
585 expectedSpanContext: SpanContext{
586 traceID: TraceID([16]byte{1}),
587 spanID: SpanID([8]byte{42}),
588 traceFlags: 0x1,
589 traceState: TraceState{list: []member{
590 {"foo", "bar"},
591 }},
592 },
593 },
594 {
595 name: "Empty SpanContext",
596 config: SpanContextConfig{},
597 expectedSpanContext: SpanContext{},
598 },
599 {
600 name: "Partial SpanContext",
601 config: SpanContextConfig{
602 TraceID: TraceID([16]byte{1}),
603 SpanID: SpanID([8]byte{42}),
604 },
605 expectedSpanContext: SpanContext{
606 traceID: TraceID([16]byte{1}),
607 spanID: SpanID([8]byte{42}),
608 traceFlags: 0x0,
609 traceState: TraceState{},
610 },
611 },
612 }
613
614 for _, tc := range testCases {
615 t.Run(tc.name, func(t *testing.T) {
616 sctx := NewSpanContext(tc.config)
617 if !assertSpanContextEqual(sctx, tc.expectedSpanContext) {
618 t.Fatalf("%s: Unexpected context created: %s", tc.name, cmp.Diff(sctx, tc.expectedSpanContext))
619 }
620 })
621 }
622 }
623
624 func TestSpanContextDerivation(t *testing.T) {
625 from := SpanContext{}
626 to := SpanContext{traceID: TraceID([16]byte{1})}
627
628 modified := from.WithTraceID(to.TraceID())
629 if !assertSpanContextEqual(modified, to) {
630 t.Fatalf("WithTraceID: Unexpected context created: %s", cmp.Diff(modified, to))
631 }
632
633 from = to
634 to.spanID = SpanID([8]byte{42})
635
636 modified = from.WithSpanID(to.SpanID())
637 if !assertSpanContextEqual(modified, to) {
638 t.Fatalf("WithSpanID: Unexpected context created: %s", cmp.Diff(modified, to))
639 }
640
641 from = to
642 to.traceFlags = 0x13
643
644 modified = from.WithTraceFlags(to.TraceFlags())
645 if !assertSpanContextEqual(modified, to) {
646 t.Fatalf("WithTraceFlags: Unexpected context created: %s", cmp.Diff(modified, to))
647 }
648
649 from = to
650 to.traceState = TraceState{list: []member{{"foo", "bar"}}}
651
652 modified = from.WithTraceState(to.TraceState())
653 if !assertSpanContextEqual(modified, to) {
654 t.Fatalf("WithTraceState: Unexpected context created: %s", cmp.Diff(modified, to))
655 }
656 }
657
658 func TestConfigLinkMutability(t *testing.T) {
659 sc0 := NewSpanContext(SpanContextConfig{TraceID: [16]byte{1}})
660 sc1 := NewSpanContext(SpanContextConfig{TraceID: [16]byte{2}})
661 sc2 := NewSpanContext(SpanContextConfig{TraceID: [16]byte{3}})
662 l0 := Link{SpanContext: sc0}
663 l1 := Link{SpanContext: sc1}
664 l2 := Link{SpanContext: sc2}
665
666 links := []Link{l0, l1}
667 conf := NewSpanStartConfig(WithLinks(links...))
668
669 // Mutating passed arg should not change configured links.
670 links[0] = l2
671
672 want := SpanConfig{links: []Link{l0, l1}}
673 assert.Equal(t, want, conf)
674 }