b3_propagator.go
4.8 KB
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
209
210
211
212
213
214
215
216
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package trace
import (
"context"
"fmt"
"strings"
"go.opentelemetry.io/otel/api/propagation"
)
const (
B3SingleHeader = "X-B3"
B3DebugFlagHeader = "X-B3-Flags"
B3TraceIDHeader = "X-B3-TraceId"
B3SpanIDHeader = "X-B3-SpanId"
B3SampledHeader = "X-B3-Sampled"
B3ParentSpanIDHeader = "X-B3-ParentSpanId"
b3TraceIDPadding = "0000000000000000"
)
// B3 propagator serializes SpanContext to/from B3 Headers.
// This propagator supports both version of B3 headers,
// 1. Single Header :
// X-B3: {TraceId}-{SpanId}-{SamplingState}-{ParentSpanId}
// 2. Multiple Headers:
// X-B3-TraceId: {TraceId}
// X-B3-ParentSpanId: {ParentSpanId}
// X-B3-SpanId: {SpanId}
// X-B3-Sampled: {SamplingState}
// X-B3-Flags: {DebugFlag}
//
// If SingleHeader is set to true then X-B3 header is used to inject and extract. Otherwise,
// separate headers are used to inject and extract.
type B3 struct {
SingleHeader bool
}
var _ propagation.HTTPPropagator = B3{}
func (b3 B3) Inject(ctx context.Context, supplier propagation.HTTPSupplier) {
sc := SpanFromContext(ctx).SpanContext()
if !sc.IsValid() {
return
}
if b3.SingleHeader {
sampled := sc.TraceFlags & FlagsSampled
supplier.Set(B3SingleHeader,
fmt.Sprintf("%s-%s-%.1d", sc.TraceID, sc.SpanID, sampled))
} else {
supplier.Set(B3TraceIDHeader, sc.TraceID.String())
supplier.Set(B3SpanIDHeader, sc.SpanID.String())
var sampled string
if sc.IsSampled() {
sampled = "1"
} else {
sampled = "0"
}
supplier.Set(B3SampledHeader, sampled)
}
}
// Extract retrieves B3 Headers from the supplier
func (b3 B3) Extract(ctx context.Context, supplier propagation.HTTPSupplier) context.Context {
var sc SpanContext
if b3.SingleHeader {
sc = b3.extractSingleHeader(supplier)
} else {
sc = b3.extract(supplier)
}
if !sc.IsValid() {
return ctx
}
return ContextWithRemoteSpanContext(ctx, sc)
}
func fixB3TID(in string) string {
if len(in) == 16 {
in = b3TraceIDPadding + in
}
return in
}
func (b3 B3) extract(supplier propagation.HTTPSupplier) SpanContext {
tid, err := IDFromHex(fixB3TID(supplier.Get(B3TraceIDHeader)))
if err != nil {
return EmptySpanContext()
}
sid, err := SpanIDFromHex(supplier.Get(B3SpanIDHeader))
if err != nil {
return EmptySpanContext()
}
sampled, ok := b3.extractSampledState(supplier.Get(B3SampledHeader))
if !ok {
return EmptySpanContext()
}
debug, ok := b3.extracDebugFlag(supplier.Get(B3DebugFlagHeader))
if !ok {
return EmptySpanContext()
}
if debug == FlagsSampled {
sampled = FlagsSampled
}
sc := SpanContext{
TraceID: tid,
SpanID: sid,
TraceFlags: sampled,
}
if !sc.IsValid() {
return EmptySpanContext()
}
return sc
}
func (b3 B3) extractSingleHeader(supplier propagation.HTTPSupplier) SpanContext {
h := supplier.Get(B3SingleHeader)
if h == "" || h == "0" {
return EmptySpanContext()
}
sc := SpanContext{}
parts := strings.Split(h, "-")
l := len(parts)
if l > 4 {
return EmptySpanContext()
}
if l < 2 {
return EmptySpanContext()
}
var err error
sc.TraceID, err = IDFromHex(fixB3TID(parts[0]))
if err != nil {
return EmptySpanContext()
}
sc.SpanID, err = SpanIDFromHex(parts[1])
if err != nil {
return EmptySpanContext()
}
if l > 2 {
var ok bool
sc.TraceFlags, ok = b3.extractSampledState(parts[2])
if !ok {
return EmptySpanContext()
}
}
if l == 4 {
_, err = SpanIDFromHex(parts[3])
if err != nil {
return EmptySpanContext()
}
}
if !sc.IsValid() {
return EmptySpanContext()
}
return sc
}
// extractSampledState parses the value of the X-B3-Sampled b3Header.
func (b3 B3) extractSampledState(sampled string) (flag byte, ok bool) {
switch sampled {
case "", "0":
return 0, true
case "1":
return FlagsSampled, true
case "true":
if !b3.SingleHeader {
return FlagsSampled, true
}
case "d":
if b3.SingleHeader {
return FlagsSampled, true
}
}
return 0, false
}
// extracDebugFlag parses the value of the X-B3-Sampled b3Header.
func (b3 B3) extracDebugFlag(debug string) (flag byte, ok bool) {
switch debug {
case "", "0":
return 0, true
case "1":
return FlagsSampled, true
}
return 0, false
}
func (b3 B3) GetAllKeys() []string {
if b3.SingleHeader {
return []string{B3SingleHeader}
}
return []string{B3TraceIDHeader, B3SpanIDHeader, B3SampledHeader}
}