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