Debian Codegraph @

propagation_ot.go

raw symbols SCIP
1 package basictracer
2
3 import (
4 "encoding/binary"
5 "io"
6 "strconv"
7 "strings"
8
9 "github.com/gogo/protobuf/proto"
10 "github.com/opentracing/basictracer-go/wire"
11 opentracing "github.com/opentracing/opentracing-go"
12 )
13
14 type textMapPropagator struct {
15 tracer *tracerImpl
16 }
17 type binaryPropagator struct {
18 tracer *tracerImpl
19 }
20
21 const (
22 prefixTracerState = "ot-tracer-"
23 prefixBaggage = "ot-baggage-"
24
25 tracerStateFieldCount = 3
26 fieldNameTraceID = prefixTracerState + "traceid"
27 fieldNameSpanID = prefixTracerState + "spanid"
28 fieldNameSampled = prefixTracerState + "sampled"
29 )
30
31 func (p *textMapPropagator) Inject(
32 spanContext opentracing.SpanContext,
33 opaqueCarrier interface{},
34 ) error {
35 sc, ok := spanContext.(SpanContext)
36 if !ok {
37 return opentracing.ErrInvalidSpanContext
38 }
39 carrier, ok := opaqueCarrier.(opentracing.TextMapWriter)
40 if !ok {
41 return opentracing.ErrInvalidCarrier
42 }
43 carrier.Set(fieldNameTraceID, strconv.FormatUint(sc.TraceID, 16))
44 carrier.Set(fieldNameSpanID, strconv.FormatUint(sc.SpanID, 16))
45 carrier.Set(fieldNameSampled, strconv.FormatBool(sc.Sampled))
46
47 for k, v := range sc.Baggage {
48 carrier.Set(prefixBaggage+k, v)
49 }
50 return nil
51 }
52
53 func (p *textMapPropagator) Extract(
54 opaqueCarrier interface{},
55 ) (opentracing.SpanContext, error) {
56 carrier, ok := opaqueCarrier.(opentracing.TextMapReader)
57 if !ok {
58 return nil, opentracing.ErrInvalidCarrier
59 }
60 requiredFieldCount := 0
61 var traceID, spanID uint64
62 var sampled bool
63 var err error
64 decodedBaggage := make(map[string]string)
65 err = carrier.ForeachKey(func(k, v string) error {
66 switch strings.ToLower(k) {
67 case fieldNameTraceID:
68 traceID, err = strconv.ParseUint(v, 16, 64)
69 if err != nil {
70 return opentracing.ErrSpanContextCorrupted
71 }
72 case fieldNameSpanID:
73 spanID, err = strconv.ParseUint(v, 16, 64)
74 if err != nil {
75 return opentracing.ErrSpanContextCorrupted
76 }
77 case fieldNameSampled:
78 sampled, err = strconv.ParseBool(v)
79 if err != nil {
80 return opentracing.ErrSpanContextCorrupted
81 }
82 default:
83 lowercaseK := strings.ToLower(k)
84 if strings.HasPrefix(lowercaseK, prefixBaggage) {
85 decodedBaggage[strings.TrimPrefix(lowercaseK, prefixBaggage)] = v
86 }
87 // Balance off the requiredFieldCount++ just below...
88 requiredFieldCount--
89 }
90 requiredFieldCount++
91 return nil
92 })
93 if err != nil {
94 return nil, err
95 }
96 if requiredFieldCount < tracerStateFieldCount {
97 if requiredFieldCount == 0 {
98 return nil, opentracing.ErrSpanContextNotFound
99 }
100 return nil, opentracing.ErrSpanContextCorrupted
101 }
102
103 return SpanContext{
104 TraceID: traceID,
105 SpanID: spanID,
106 Sampled: sampled,
107 Baggage: decodedBaggage,
108 }, nil
109 }
110
111 func (p *binaryPropagator) Inject(
112 spanContext opentracing.SpanContext,
113 opaqueCarrier interface{},
114 ) error {
115 sc, ok := spanContext.(SpanContext)
116 if !ok {
117 return opentracing.ErrInvalidSpanContext
118 }
119 carrier, ok := opaqueCarrier.(io.Writer)
120 if !ok {
121 return opentracing.ErrInvalidCarrier
122 }
123
124 state := wire.TracerState{}
125 state.TraceId = sc.TraceID
126 state.SpanId = sc.SpanID
127 state.Sampled = sc.Sampled
128 state.BaggageItems = sc.Baggage
129
130 b, err := proto.Marshal(&state)
131 if err != nil {
132 return err
133 }
134
135 // Write the length of the marshalled binary to the writer.
136 length := uint32(len(b))
137 if err := binary.Write(carrier, binary.BigEndian, &length); err != nil {
138 return err
139 }
140
141 _, err = carrier.Write(b)
142 return err
143 }
144
145 func (p *binaryPropagator) Extract(
146 opaqueCarrier interface{},
147 ) (opentracing.SpanContext, error) {
148 carrier, ok := opaqueCarrier.(io.Reader)
149 if !ok {
150 return nil, opentracing.ErrInvalidCarrier
151 }
152
153 // Read the length of marshalled binary. io.ReadAll isn't that performant
154 // since it keeps resizing the underlying buffer as it encounters more bytes
155 // to read. By reading the length, we can allocate a fixed sized buf and read
156 // the exact amount of bytes into it.
157 var length uint32
158 if err := binary.Read(carrier, binary.BigEndian, &length); err != nil {
159 return nil, opentracing.ErrSpanContextCorrupted
160 }
161 buf := make([]byte, length)
162 if n, err := carrier.Read(buf); err != nil {
163 if n > 0 {
164 return nil, opentracing.ErrSpanContextCorrupted
165 }
166 return nil, opentracing.ErrSpanContextNotFound
167 }
168
169 ctx := wire.TracerState{}
170 if err := proto.Unmarshal(buf, &ctx); err != nil {
171 return nil, opentracing.ErrSpanContextCorrupted
172 }
173
174 return SpanContext{
175 TraceID: ctx.TraceId,
176 SpanID: ctx.SpanId,
177 Sampled: ctx.Sampled,
178 Baggage: ctx.BaggageItems,
179 }, nil
180 }