Debian Codegraph @

bridge/opentracing/mix_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package opentracing
5
6 import (
7 "context"
8 "fmt"
9 "maps"
10 "testing"
11
12 ot "github.com/opentracing/opentracing-go"
13
14 "go.opentelemetry.io/otel"
15 "go.opentelemetry.io/otel/attribute"
16 "go.opentelemetry.io/otel/baggage"
17 "go.opentelemetry.io/otel/trace"
18 )
19
20 type mixedAPIsTestCase struct {
21 desc string
22
23 setup func(*testing.T, *mockTracer)
24 run func(*testing.T, context.Context)
25 check func(*testing.T, *mockTracer)
26 }
27
28 func getMixedAPIsTestCases() []mixedAPIsTestCase {
29 st := newSimpleTest()
30 cast := newCurrentActiveSpanTest()
31 coin := newContextIntactTest()
32 bip := newBaggageItemsPreservationTest()
33 bio := newBaggageInteroperationTest()
34
35 return []mixedAPIsTestCase{
36 {
37 desc: "simple otel -> ot -> otel",
38 setup: st.setup,
39 run: st.runOtelOTOtel,
40 check: st.check,
41 },
42 {
43 desc: "simple ot -> otel -> ot",
44 setup: st.setup,
45 run: st.runOTOtelOT,
46 check: st.check,
47 },
48 {
49 desc: "current/active span otel -> ot -> otel",
50 setup: cast.setup,
51 run: cast.runOtelOTOtel,
52 check: cast.check,
53 },
54 {
55 desc: "current/active span ot -> otel -> ot",
56 setup: cast.setup,
57 run: cast.runOTOtelOT,
58 check: cast.check,
59 },
60 {
61 desc: "context intact otel -> ot -> otel",
62 setup: coin.setup,
63 run: coin.runOtelOTOtel,
64 check: coin.check,
65 },
66 {
67 desc: "context intact ot -> otel -> ot",
68 setup: coin.setup,
69 run: coin.runOTOtelOT,
70 check: coin.check,
71 },
72 {
73 desc: "baggage items preservation across layers otel -> ot -> otel",
74 setup: bip.setup,
75 run: bip.runOtelOTOtel,
76 check: bip.check,
77 },
78 {
79 desc: "baggage items preservation across layers ot -> otel -> ot",
80 setup: bip.setup,
81 run: bip.runOTOtelOT,
82 check: bip.check,
83 },
84 {
85 desc: "baggage items interoperation across layers ot -> otel -> ot",
86 setup: bio.setup,
87 run: bio.runOTOtelOT,
88 check: bio.check,
89 },
90 {
91 desc: "baggage items interoperation across layers otel -> ot -> otel",
92 setup: bio.setup,
93 run: bio.runOtelOTOtel,
94 check: bio.check,
95 },
96 }
97 }
98
99 func TestMixedAPIs(t *testing.T) {
100 for idx, tc := range getMixedAPIsTestCases() {
101 t.Logf("Running test case %d: %s", idx, tc.desc)
102 mockOtelTracer := newMockTracer()
103 ctx, otTracer, otelProvider := NewTracerPairWithContext(t.Context(), mockOtelTracer)
104 otTracer.SetWarningHandler(func(msg string) {
105 t.Log(msg)
106 })
107
108 otel.SetTracerProvider(otelProvider)
109 ot.SetGlobalTracer(otTracer)
110
111 tc.setup(t, mockOtelTracer)
112 tc.run(t, ctx)
113 tc.check(t, mockOtelTracer)
114 }
115 }
116
117 // simple test
118
119 type simpleTest struct {
120 traceID trace.TraceID
121 spanIDs []trace.SpanID
122 }
123
124 func newSimpleTest() *simpleTest {
125 return &simpleTest{
126 traceID: simpleTraceID(),
127 spanIDs: simpleSpanIDs(3),
128 }
129 }
130
131 func (st *simpleTest) setup(_ *testing.T, tracer *mockTracer) {
132 tracer.SpareTraceIDs = append(tracer.SpareTraceIDs, st.traceID)
133 tracer.SpareSpanIDs = append(tracer.SpareSpanIDs, st.spanIDs...)
134 }
135
136 func (st *simpleTest) check(t *testing.T, tracer *mockTracer) {
137 checkTraceAndSpans(t, tracer, st.traceID, st.spanIDs)
138 }
139
140 func (st *simpleTest) runOtelOTOtel(t *testing.T, ctx context.Context) {
141 runOtelOTOtel(t, ctx, "simple", st.noop)
142 }
143
144 func (st *simpleTest) runOTOtelOT(t *testing.T, ctx context.Context) {
145 runOTOtelOT(t, ctx, "simple", st.noop)
146 }
147
148 func (*simpleTest) noop(_ *testing.T, ctx context.Context) context.Context {
149 return ctx
150 }
151
152 // current/active span test
153
154 type currentActiveSpanTest struct {
155 traceID trace.TraceID
156 spanIDs []trace.SpanID
157
158 recordedCurrentOtelSpanIDs []trace.SpanID
159 recordedActiveOTSpanIDs []trace.SpanID
160 }
161
162 func newCurrentActiveSpanTest() *currentActiveSpanTest {
163 return &currentActiveSpanTest{
164 traceID: simpleTraceID(),
165 spanIDs: simpleSpanIDs(3),
166 }
167 }
168
169 func (cast *currentActiveSpanTest) setup(_ *testing.T, tracer *mockTracer) {
170 tracer.SpareTraceIDs = append(tracer.SpareTraceIDs, cast.traceID)
171 tracer.SpareSpanIDs = append(tracer.SpareSpanIDs, cast.spanIDs...)
172
173 cast.recordedCurrentOtelSpanIDs = nil
174 cast.recordedActiveOTSpanIDs = nil
175 }
176
177 func (cast *currentActiveSpanTest) check(t *testing.T, tracer *mockTracer) {
178 checkTraceAndSpans(t, tracer, cast.traceID, cast.spanIDs)
179 if len(cast.recordedCurrentOtelSpanIDs) != len(cast.spanIDs) {
180 t.Errorf(
181 "Expected to have %d recorded Otel current spans, got %d",
182 len(cast.spanIDs),
183 len(cast.recordedCurrentOtelSpanIDs),
184 )
185 }
186 if len(cast.recordedActiveOTSpanIDs) != len(cast.spanIDs) {
187 t.Errorf(
188 "Expected to have %d recorded OT active spans, got %d",
189 len(cast.spanIDs),
190 len(cast.recordedActiveOTSpanIDs),
191 )
192 }
193
194 minLen := min(len(cast.recordedCurrentOtelSpanIDs), len(cast.spanIDs))
195 minLen = min(minLen, len(cast.recordedActiveOTSpanIDs))
196 for i := 0; i < minLen; i++ {
197 if cast.recordedCurrentOtelSpanIDs[i] != cast.spanIDs[i] {
198 t.Errorf(
199 "Expected span idx %d (%d) to be recorded as current span in Otel, got %d",
200 i,
201 cast.spanIDs[i],
202 cast.recordedCurrentOtelSpanIDs[i],
203 )
204 }
205 if cast.recordedActiveOTSpanIDs[i] != cast.spanIDs[i] {
206 t.Errorf(
207 "Expected span idx %d (%d) to be recorded as active span in OT, got %d",
208 i,
209 cast.spanIDs[i],
210 cast.recordedActiveOTSpanIDs[i],
211 )
212 }
213 }
214 }
215
216 func (cast *currentActiveSpanTest) runOtelOTOtel(t *testing.T, ctx context.Context) {
217 runOtelOTOtel(t, ctx, "cast", cast.recordSpans)
218 }
219
220 func (cast *currentActiveSpanTest) runOTOtelOT(t *testing.T, ctx context.Context) {
221 runOTOtelOT(t, ctx, "cast", cast.recordSpans)
222 }
223
224 func (cast *currentActiveSpanTest) recordSpans(_ *testing.T, ctx context.Context) context.Context {
225 spanID := trace.SpanContextFromContext(ctx).SpanID()
226 cast.recordedCurrentOtelSpanIDs = append(cast.recordedCurrentOtelSpanIDs, spanID)
227
228 spanID = trace.SpanID{}
229 if bridgeSpan, ok := ot.SpanFromContext(ctx).(*bridgeSpan); ok {
230 spanID = bridgeSpan.otelSpan.SpanContext().SpanID()
231 }
232 cast.recordedActiveOTSpanIDs = append(cast.recordedActiveOTSpanIDs, spanID)
233 return ctx
234 }
235
236 // context intact test
237
238 type contextIntactTest struct {
239 contextKeyValues []mockContextKeyValue
240
241 recordedContextValues []any
242 recordIdx int
243 }
244
245 type coin1Key struct{}
246
247 type coin1Value struct{}
248
249 type coin2Key struct{}
250
251 type coin2Value struct{}
252
253 type coin3Key struct{}
254
255 type coin3Value struct{}
256
257 func newContextIntactTest() *contextIntactTest {
258 return &contextIntactTest{
259 contextKeyValues: []mockContextKeyValue{
260 {
261 Key: coin1Key{},
262 Value: coin1Value{},
263 },
264 {
265 Key: coin2Key{},
266 Value: coin2Value{},
267 },
268 {
269 Key: coin3Key{},
270 Value: coin3Value{},
271 },
272 },
273 }
274 }
275
276 func (coin *contextIntactTest) setup(_ *testing.T, tracer *mockTracer) {
277 tracer.SpareContextKeyValues = append(tracer.SpareContextKeyValues, coin.contextKeyValues...)
278
279 coin.recordedContextValues = nil
280 coin.recordIdx = 0
281 }
282
283 func (coin *contextIntactTest) check(t *testing.T, _ *mockTracer) {
284 if len(coin.recordedContextValues) != len(coin.contextKeyValues) {
285 t.Errorf(
286 "Expected to have %d recorded context values, got %d",
287 len(coin.contextKeyValues),
288 len(coin.recordedContextValues),
289 )
290 }
291
292 minLen := min(len(coin.recordedContextValues), len(coin.contextKeyValues))
293 for i := range minLen {
294 key := coin.contextKeyValues[i].Key
295 value := coin.contextKeyValues[i].Value
296 gotValue := coin.recordedContextValues[i]
297 if value != gotValue {
298 t.Errorf("Expected value %#v for key %#v, got %#v", value, key, gotValue)
299 }
300 }
301 }
302
303 func (coin *contextIntactTest) runOtelOTOtel(t *testing.T, ctx context.Context) {
304 runOtelOTOtel(t, ctx, "coin", coin.recordValue)
305 }
306
307 func (coin *contextIntactTest) runOTOtelOT(t *testing.T, ctx context.Context) {
308 runOTOtelOT(t, ctx, "coin", coin.recordValue)
309 }
310
311 func (coin *contextIntactTest) recordValue(t *testing.T, ctx context.Context) context.Context {
312 if coin.recordIdx >= len(coin.contextKeyValues) {
313 t.Errorf("Too many steps?")
314 return ctx
315 }
316 key := coin.contextKeyValues[coin.recordIdx].Key
317 coin.recordIdx++
318 coin.recordedContextValues = append(coin.recordedContextValues, ctx.Value(key))
319 return ctx
320 }
321
322 // baggage items preservation test
323
324 type bipBaggage struct {
325 key string
326 value string
327 }
328
329 type baggageItemsPreservationTest struct {
330 baggageItems []bipBaggage
331
332 step int
333 recordedBaggage []map[string]string
334 }
335
336 func newBaggageItemsPreservationTest() *baggageItemsPreservationTest {
337 return &baggageItemsPreservationTest{
338 baggageItems: []bipBaggage{
339 {
340 key: "First",
341 value: "one",
342 },
343 {
344 key: "Second",
345 value: "two",
346 },
347 {
348 key: "third",
349 value: "three",
350 },
351 },
352 }
353 }
354
355 func (bip *baggageItemsPreservationTest) setup(*testing.T, *mockTracer) {
356 bip.step = 0
357 bip.recordedBaggage = nil
358 }
359
360 func (bip *baggageItemsPreservationTest) check(t *testing.T, _ *mockTracer) {
361 if len(bip.recordedBaggage) != len(bip.baggageItems) {
362 t.Errorf("Expected %d recordings, got %d", len(bip.baggageItems), len(bip.recordedBaggage))
363 }
364 minLen := min(len(bip.recordedBaggage), len(bip.baggageItems))
365
366 for i := range minLen {
367 recordedItems := bip.recordedBaggage[i]
368 if len(recordedItems) != i+1 {
369 t.Errorf(
370 "Expected %d recorded baggage items in recording %d, got %d",
371 i+1,
372 i+1,
373 len(bip.recordedBaggage[i]),
374 )
375 }
376 minItemLen := min(len(bip.baggageItems), i+1)
377 for j := range minItemLen {
378 expectedItem := bip.baggageItems[j]
379 if gotValue, ok := recordedItems[expectedItem.key]; !ok {
380 t.Errorf("Missing baggage item %q in recording %d", expectedItem.key, i+1)
381 } else if gotValue != expectedItem.value {
382 t.Errorf(
383 "Expected recorded baggage item %q in recording %d + 1to be %q, got %q",
384 expectedItem.key,
385 i,
386 expectedItem.value,
387 gotValue,
388 )
389 } else {
390 delete(recordedItems, expectedItem.key)
391 }
392 }
393 for key, value := range recordedItems {
394 t.Errorf("Unexpected baggage item in recording %d: %q -> %q", i+1, key, value)
395 }
396 }
397 }
398
399 func (bip *baggageItemsPreservationTest) runOtelOTOtel(t *testing.T, ctx context.Context) {
400 runOtelOTOtel(t, ctx, "bip", bip.addAndRecordBaggage)
401 }
402
403 func (bip *baggageItemsPreservationTest) runOTOtelOT(t *testing.T, ctx context.Context) {
404 runOTOtelOT(t, ctx, "bip", bip.addAndRecordBaggage)
405 }
406
407 func (bip *baggageItemsPreservationTest) addAndRecordBaggage(t *testing.T, ctx context.Context) context.Context {
408 if bip.step >= len(bip.baggageItems) {
409 t.Errorf("Too many steps?")
410 return ctx
411 }
412 span := ot.SpanFromContext(ctx)
413 if span == nil {
414 t.Errorf("No active OpenTracing span")
415 return ctx
416 }
417 idx := bip.step
418 bip.step++
419 span.SetBaggageItem(bip.baggageItems[idx].key, bip.baggageItems[idx].value)
420 sctx := span.Context()
421 recording := make(map[string]string)
422 sctx.ForeachBaggageItem(func(key, value string) bool {
423 recording[key] = value
424 return true
425 })
426 bip.recordedBaggage = append(bip.recordedBaggage, recording)
427 return ctx
428 }
429
430 // baggage interoperation test
431
432 type baggageInteroperationTest struct {
433 baggageItems []bipBaggage
434
435 step int
436 recordedOTBaggage []map[string]string
437 recordedOtelBaggage []map[string]string
438 }
439
440 func newBaggageInteroperationTest() *baggageInteroperationTest {
441 return &baggageInteroperationTest{
442 baggageItems: []bipBaggage{
443 {
444 key: "First",
445 value: "one",
446 },
447 {
448 key: "Second",
449 value: "two",
450 },
451 {
452 key: "third",
453 value: "three",
454 },
455 },
456 }
457 }
458
459 func (bio *baggageInteroperationTest) setup(*testing.T, *mockTracer) {
460 bio.step = 0
461 bio.recordedOTBaggage = nil
462 bio.recordedOtelBaggage = nil
463 }
464
465 func (bio *baggageInteroperationTest) check(t *testing.T, _ *mockTracer) {
466 checkBIORecording(t, "OT", bio.baggageItems, bio.recordedOTBaggage)
467 checkBIORecording(t, "Otel", bio.baggageItems, bio.recordedOtelBaggage)
468 }
469
470 func checkBIORecording(t *testing.T, apiDesc string, initialItems []bipBaggage, recordings []map[string]string) {
471 // expect recordings count to equal the number of initial
472 // items
473
474 // each recording should have a duplicated item from initial
475 // items, one with OT suffix, another one with Otel suffix
476
477 // expect each subsequent recording to have two more items, up
478 // to double of the count of the initial items
479
480 if len(initialItems) != len(recordings) {
481 t.Errorf("Expected %d recordings from %s, got %d", len(initialItems), apiDesc, len(recordings))
482 }
483 minRecLen := min(len(initialItems), len(recordings))
484 for i := range minRecLen {
485 recordedItems := recordings[i]
486 expectedItemsInStep := (i + 1) * 2
487 if expectedItemsInStep != len(recordedItems) {
488 t.Errorf(
489 "Expected %d recorded items in recording %d from %s, got %d",
490 expectedItemsInStep,
491 i,
492 apiDesc,
493 len(recordedItems),
494 )
495 }
496 recordedItemsCopy := make(map[string]string, len(recordedItems))
497 maps.Copy(recordedItemsCopy, recordedItems)
498 for j := 0; j < i+1; j++ {
499 otKey, otelKey := generateBaggageKeys(initialItems[j].key)
500 value := initialItems[j].value
501 for _, k := range []string{otKey, otelKey} {
502 if v, ok := recordedItemsCopy[k]; ok {
503 if value != v {
504 t.Errorf(
505 "Expected value %s under key %s in recording %d from %s, got %s",
506 value,
507 k,
508 i,
509 apiDesc,
510 v,
511 )
512 }
513 delete(recordedItemsCopy, k)
514 } else {
515 t.Errorf("Missing key %s in recording %d from %s", k, i, apiDesc)
516 }
517 }
518 }
519 for k, v := range recordedItemsCopy {
520 t.Errorf("Unexpected key-value pair %s = %s in recording %d from %s", k, v, i, apiDesc)
521 }
522 }
523 }
524
525 func (bio *baggageInteroperationTest) runOtelOTOtel(t *testing.T, ctx context.Context) {
526 runOtelOTOtel(t, ctx, "bio", bio.addAndRecordBaggage)
527 }
528
529 func (bio *baggageInteroperationTest) runOTOtelOT(t *testing.T, ctx context.Context) {
530 runOTOtelOT(t, ctx, "bio", bio.addAndRecordBaggage)
531 }
532
533 func (bio *baggageInteroperationTest) addAndRecordBaggage(t *testing.T, ctx context.Context) context.Context {
534 if bio.step >= len(bio.baggageItems) {
535 t.Errorf("Too many steps?")
536 return ctx
537 }
538 otSpan := ot.SpanFromContext(ctx)
539 if otSpan == nil {
540 t.Errorf("No active OpenTracing span")
541 return ctx
542 }
543 idx := bio.step
544 bio.step++
545 key := bio.baggageItems[idx].key
546 otKey, otelKey := generateBaggageKeys(key)
547 value := bio.baggageItems[idx].value
548
549 otSpan.SetBaggageItem(otKey, value)
550
551 m, err := baggage.NewMemberRaw(otelKey, value)
552 if err != nil {
553 t.Error(err)
554 return ctx
555 }
556 b, err := baggage.FromContext(ctx).SetMember(m)
557 if err != nil {
558 t.Error(err)
559 return ctx
560 }
561 ctx = baggage.ContextWithBaggage(ctx, b)
562
563 otRecording := make(map[string]string)
564 otSpan.Context().ForeachBaggageItem(func(key, value string) bool {
565 otRecording[key] = value
566 return true
567 })
568 otelRecording := make(map[string]string)
569 for _, m := range baggage.FromContext(ctx).Members() {
570 otelRecording[m.Key()] = m.Value()
571 }
572 bio.recordedOTBaggage = append(bio.recordedOTBaggage, otRecording)
573 bio.recordedOtelBaggage = append(bio.recordedOtelBaggage, otelRecording)
574 return ctx
575 }
576
577 func generateBaggageKeys(key string) (string, string) {
578 return key + "-Ot", key + "-Otel"
579 }
580
581 // helpers
582
583 func checkTraceAndSpans(
584 t *testing.T,
585 tracer *mockTracer,
586 expectedTraceID trace.TraceID,
587 expectedSpanIDs []trace.SpanID,
588 ) {
589 expectedSpanCount := len(expectedSpanIDs)
590
591 // reverse spanIDs, since first span ID belongs to root, that
592 // finishes last
593 spanIDs := make([]trace.SpanID, len(expectedSpanIDs))
594 copy(spanIDs, expectedSpanIDs)
595 reverse(len(spanIDs), func(i, j int) {
596 spanIDs[i], spanIDs[j] = spanIDs[j], spanIDs[i]
597 })
598 // the last finished span has no parent
599 parentSpanIDs := append(spanIDs[1:], trace.SpanID{})
600
601 sks := map[trace.SpanID]trace.SpanKind{
602 {125}: trace.SpanKindProducer,
603 {124}: trace.SpanKindInternal,
604 {123}: trace.SpanKindClient,
605 }
606
607 if len(tracer.FinishedSpans) != expectedSpanCount {
608 t.Errorf("Expected %d finished spans, got %d", expectedSpanCount, len(tracer.FinishedSpans))
609 }
610 for idx, span := range tracer.FinishedSpans {
611 sctx := span.SpanContext()
612 if sctx.TraceID() != expectedTraceID {
613 t.Errorf(
614 "Expected trace ID %v in span %d (%d), got %v",
615 expectedTraceID,
616 idx,
617 sctx.SpanID(),
618 sctx.TraceID(),
619 )
620 }
621 expectedSpanID := spanIDs[idx]
622 expectedParentSpanID := parentSpanIDs[idx]
623 if sctx.SpanID() != expectedSpanID {
624 t.Errorf("Expected finished span %d to have span ID %d, but got %d", idx, expectedSpanID, sctx.SpanID())
625 }
626 if span.ParentSpanID != expectedParentSpanID {
627 t.Errorf(
628 "Expected finished span %d (span ID: %d) to have parent span ID %d, but got %d",
629 idx,
630 sctx.SpanID(),
631 expectedParentSpanID,
632 span.ParentSpanID,
633 )
634 }
635 if span.SpanKind != sks[span.SpanContext().SpanID()] {
636 t.Errorf(
637 "Expected finished span %d (span ID: %d) to have span.kind to be '%v' but was '%v'",
638 idx,
639 sctx.SpanID(),
640 sks[span.SpanContext().SpanID()],
641 span.SpanKind,
642 )
643 }
644 }
645 }
646
647 func reverse(length int, swap func(i, j int)) {
648 for left, right := 0, length-1; left < right; left, right = left+1, right-1 {
649 swap(left, right)
650 }
651 }
652
653 func simpleTraceID() trace.TraceID {
654 return [16]byte{123, 42}
655 }
656
657 func simpleSpanIDs(count int) []trace.SpanID {
658 base := []trace.SpanID{
659 {123},
660 {124},
661 {125},
662 {126},
663 {127},
664 {128},
665 }
666 return base[:count]
667 }
668
669 func runOtelOTOtel(
670 t *testing.T,
671 ctx context.Context,
672 name string,
673 callback func(*testing.T, context.Context) context.Context,
674 ) {
675 tr := otel.Tracer("")
676 ctx, span := tr.Start(ctx, fmt.Sprintf("%s_Otel_OTOtel", name), trace.WithSpanKind(trace.SpanKindClient))
677 defer span.End()
678 ctx = callback(t, ctx)
679 func(ctx2 context.Context) {
680 span, ctx2 := ot.StartSpanFromContext(ctx2, fmt.Sprintf("%sOtel_OT_Otel", name))
681 defer span.Finish()
682 ctx2 = callback(t, ctx2)
683 func(ctx3 context.Context) {
684 ctx3, span := tr.Start(
685 ctx3,
686 fmt.Sprintf("%sOtelOT_Otel_", name),
687 trace.WithSpanKind(trace.SpanKindProducer),
688 )
689 defer span.End()
690 _ = callback(t, ctx3)
691 }(ctx2)
692 }(ctx)
693 }
694
695 func runOTOtelOT(
696 t *testing.T,
697 ctx context.Context,
698 name string,
699 callback func(*testing.T, context.Context) context.Context,
700 ) {
701 tr := otel.Tracer("")
702 span, ctx := ot.StartSpanFromContext(
703 ctx,
704 fmt.Sprintf("%s_OT_OtelOT", name),
705 ot.Tag{Key: "span.kind", Value: "client"},
706 )
707 defer span.Finish()
708 ctx = callback(t, ctx)
709 func(ctx2 context.Context) {
710 ctx2, span := tr.Start(ctx2, fmt.Sprintf("%sOT_Otel_OT", name))
711 defer span.End()
712 ctx2 = callback(t, ctx2)
713 func(ctx3 context.Context) {
714 span, ctx3 := ot.StartSpanFromContext(
715 ctx3,
716 fmt.Sprintf("%sOTOtel_OT_", name),
717 ot.Tag{Key: "span.kind", Value: "producer"},
718 )
719 defer span.Finish()
720 _ = callback(t, ctx3)
721 }(ctx2)
722 }(ctx)
723 }
724
725 func TestOtTagToOTelAttrCheckTypeConversions(t *testing.T) {
726 tableTest := []struct {
727 key string
728 value any
729 expectedValueType attribute.Type
730 }{
731 {
732 key: "bool to bool",
733 value: true,
734 expectedValueType: attribute.BOOL,
735 },
736 {
737 key: "int to int64",
738 value: 123,
739 expectedValueType: attribute.INT64,
740 },
741 {
742 key: "uint to string",
743 value: uint(1234),
744 expectedValueType: attribute.STRING,
745 },
746 {
747 key: "int32 to int64",
748 value: int32(12345),
749 expectedValueType: attribute.INT64,
750 },
751 {
752 key: "uint32 to int64",
753 value: uint32(123456),
754 expectedValueType: attribute.INT64,
755 },
756 {
757 key: "int64 to int64",
758 value: int64(1234567),
759 expectedValueType: attribute.INT64,
760 },
761 {
762 key: "uint64 to string",
763 value: uint64(12345678),
764 expectedValueType: attribute.STRING,
765 },
766 {
767 key: "float32 to float64",
768 value: float32(3.14),
769 expectedValueType: attribute.FLOAT64,
770 },
771 {
772 key: "float64 to float64",
773 value: float64(3.14),
774 expectedValueType: attribute.FLOAT64,
775 },
776 {
777 key: "string to string",
778 value: "string_value",
779 expectedValueType: attribute.STRING,
780 },
781 {
782 key: "unexpected type to string",
783 value: struct{}{},
784 expectedValueType: attribute.STRING,
785 },
786 }
787
788 for _, test := range tableTest {
789 got := otTagToOTelAttr(test.key, test.value)
790 if test.expectedValueType != got.Value.Type() {
791 t.Errorf("Expected type %s, but got %s after conversion '%v' value",
792 test.expectedValueType,
793 got.Value.Type(),
794 test.value)
795 }
796 }
797 }