Debian Codegraph @

propagation/propagators_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package propagation_test
5
6 import (
7 "context"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12
13 "go.opentelemetry.io/otel/propagation"
14 "go.opentelemetry.io/otel/trace"
15 )
16
17 const (
18 traceIDStr = "4bf92f3577b34da6a3ce929d0e0e4736"
19 spanIDStr = "00f067aa0ba902b7"
20 )
21
22 var (
23 traceID = mustTraceIDFromHex(traceIDStr)
24 spanID = mustSpanIDFromHex(spanIDStr)
25 )
26
27 func mustTraceIDFromHex(s string) trace.TraceID {
28 t, err := trace.TraceIDFromHex(s)
29 if err != nil {
30 panic(err)
31 }
32 return t
33 }
34
35 func mustSpanIDFromHex(s string) trace.SpanID {
36 t, err := trace.SpanIDFromHex(s)
37 if err != nil {
38 panic(err)
39 }
40 return t
41 }
42
43 type outOfThinAirPropagator struct {
44 t *testing.T
45 }
46
47 var _ propagation.TextMapPropagator = outOfThinAirPropagator{}
48
49 func (p outOfThinAirPropagator) Extract(ctx context.Context, _ propagation.TextMapCarrier) context.Context {
50 sc := trace.NewSpanContext(trace.SpanContextConfig{
51 TraceID: traceID,
52 SpanID: spanID,
53 TraceFlags: 0,
54 })
55 require.True(p.t, sc.IsValid())
56 return trace.ContextWithRemoteSpanContext(ctx, sc)
57 }
58
59 func (outOfThinAirPropagator) Inject(context.Context, propagation.TextMapCarrier) {}
60
61 func (outOfThinAirPropagator) Fields() []string {
62 return nil
63 }
64
65 type nilCarrier struct{}
66
67 var _ propagation.TextMapCarrier = nilCarrier{}
68
69 func (nilCarrier) Keys() []string {
70 return nil
71 }
72
73 func (nilCarrier) Get(string) string {
74 return ""
75 }
76
77 func (nilCarrier) Set(string, string) {}
78
79 func TestMultiplePropagators(t *testing.T) {
80 ootaProp := outOfThinAirPropagator{t: t}
81 ns := nilCarrier{}
82 testProps := []propagation.TextMapPropagator{
83 propagation.TraceContext{},
84 }
85 bg := t.Context()
86 // sanity check of oota propagator, ensuring that it really
87 // generates the valid span context out of thin air
88 {
89 ctx := ootaProp.Extract(bg, ns)
90 sc := trace.SpanContextFromContext(ctx)
91 require.True(t, sc.IsValid(), "oota prop failed sanity check")
92 require.True(t, sc.IsRemote(), "oota prop is remote")
93 }
94 // sanity check for real propagators, ensuring that they
95 // really are not putting any valid span context into an empty
96 // go context in absence of the HTTP headers.
97 for _, prop := range testProps {
98 ctx := prop.Extract(bg, ns)
99 sc := trace.SpanContextFromContext(ctx)
100 require.Falsef(t, sc.IsValid(), "%#v failed sanity check", prop)
101 require.Falsef(t, sc.IsRemote(), "%#v prop set a remote", prop)
102 }
103 for _, prop := range testProps {
104 props := propagation.NewCompositeTextMapPropagator(ootaProp, prop)
105 ctx := props.Extract(bg, ns)
106 sc := trace.SpanContextFromContext(ctx)
107 assert.Truef(t, sc.IsRemote(), "%#v prop is remote", prop)
108 assert.Truef(t, sc.IsValid(), "%#v clobbers span context", prop)
109 }
110 }