| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package b3 // import "go.opentelemetry.io/contrib/propagators/b3" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"go.opentelemetry.io/otel/propagation" |
| 12 |
"go.opentelemetry.io/otel/trace" |
| 13 |
) |
| 14 |
|
| 15 |
const ( |
| 16 |
// Default B3 Header names. |
| 17 |
b3ContextHeader = "b3" |
| 18 |
b3DebugFlagHeader = "x-b3-flags" |
| 19 |
b3TraceIDHeader = "x-b3-traceid" |
| 20 |
b3SpanIDHeader = "x-b3-spanid" |
| 21 |
b3SampledHeader = "x-b3-sampled" |
| 22 |
b3ParentSpanIDHeader = "x-b3-parentspanid" |
| 23 |
|
| 24 |
b3TraceIDPadding = "0000000000000000" |
| 25 |
|
| 26 |
// B3 Single Header encoding widths. |
| 27 |
separatorWidth = 1 // Single "-" character. |
| 28 |
samplingWidth = 1 // Single hex character. |
| 29 |
traceID64BitsWidth = 64 / 4 // 16 hex character Trace ID. |
| 30 |
traceID128BitsWidth = 128 / 4 // 32 hex character Trace ID. |
| 31 |
spanIDWidth = 16 // 16 hex character ID. |
| 32 |
parentSpanIDWidth = 16 // 16 hex character ID. |
| 33 |
) |
| 34 |
|
| 35 |
var ( |
| 36 |
empty = trace.SpanContext{} |
| 37 |
|
| 38 |
errInvalidSampledByte = errors.New("invalid B3 Sampled found") |
| 39 |
errInvalidSampledHeader = errors.New("invalid B3 Sampled header found") |
| 40 |
errInvalidTraceIDHeader = errors.New("invalid B3 traceID header found") |
| 41 |
errInvalidSpanIDHeader = errors.New("invalid B3 spanID header found") |
| 42 |
errInvalidParentSpanIDHeader = errors.New("invalid B3 ParentSpanID header found") |
| 43 |
errInvalidScope = errors.New("require either both traceID and spanID or none") |
| 44 |
errInvalidScopeParent = errors.New("traceID and spanID required for ParentSpanID") |
| 45 |
errInvalidScopeParentSingle = errors.New("traceID, spanID and Sampled required for ParentSpanID") |
| 46 |
errEmptyContext = errors.New("empty request context") |
| 47 |
errInvalidTraceIDValue = errors.New("invalid B3 traceID value found") |
| 48 |
errInvalidSpanIDValue = errors.New("invalid B3 spanID value found") |
| 49 |
errInvalidParentSpanIDValue = errors.New("invalid B3 ParentSpanID value found") |
| 50 |
) |
| 51 |
|
| 52 |
type propagator struct { |
| 53 |
cfg config |
| 54 |
} |
| 55 |
|
| 56 |
var _ propagation.TextMapPropagator = propagator{} |
| 57 |
|
| 58 |
// New creates a B3 implementation of propagation.TextMapPropagator. |
| 59 |
// B3 propagator serializes SpanContext to/from B3 Headers. |
| 60 |
// This propagator supports both versions of B3 headers, |
| 61 |
// 1. Single Header: |
| 62 |
// b3: {TraceId}-{SpanId}-{SamplingState}-{ParentSpanId} |
| 63 |
// 2. Multiple Headers: |
| 64 |
// x-b3-traceid: {TraceId} |
| 65 |
// x-b3-parentspanid: {ParentSpanId} |
| 66 |
// x-b3-spanid: {SpanId} |
| 67 |
// x-b3-sampled: {SamplingState} |
| 68 |
// x-b3-flags: {DebugFlag} |
| 69 |
// |
| 70 |
// The Single Header propagator is used by default. |
| 71 |
func New(opts ...Option) propagation.TextMapPropagator { |
| 72 |
cfg := newConfig(opts...) |
| 73 |
return propagator{ |
| 74 |
cfg: *cfg, |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// Inject injects a context into the carrier as B3 headers. |
| 79 |
// The parent span ID is omitted because it is not tracked in the |
| 80 |
// SpanContext. |
| 81 |
func (b3 propagator) Inject(ctx context.Context, carrier propagation.TextMapCarrier) { |
| 82 |
sc := trace.SpanFromContext(ctx).SpanContext() |
| 83 |
|
| 84 |
if b3.cfg.InjectEncoding.supports(B3SingleHeader) || b3.cfg.InjectEncoding == B3Unspecified { |
| 85 |
header := []string{} |
| 86 |
if sc.TraceID().IsValid() && sc.SpanID().IsValid() { |
| 87 |
header = append(header, sc.TraceID().String(), sc.SpanID().String()) |
| 88 |
} |
| 89 |
|
| 90 |
if debugFromContext(ctx) { |
| 91 |
header = append(header, "d") |
| 92 |
} else if !(deferredFromContext(ctx)) { |
| 93 |
if sc.IsSampled() { |
| 94 |
header = append(header, "1") |
| 95 |
} else { |
| 96 |
header = append(header, "0") |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
carrier.Set(b3ContextHeader, strings.Join(header, "-")) |
| 101 |
} |
| 102 |
|
| 103 |
if b3.cfg.InjectEncoding.supports(B3MultipleHeader) { |
| 104 |
if sc.TraceID().IsValid() && sc.SpanID().IsValid() { |
| 105 |
carrier.Set(b3TraceIDHeader, sc.TraceID().String()) |
| 106 |
carrier.Set(b3SpanIDHeader, sc.SpanID().String()) |
| 107 |
} |
| 108 |
|
| 109 |
if debugFromContext(ctx) { |
| 110 |
// Since Debug implies deferred, don't also send "X-B3-Sampled". |
| 111 |
carrier.Set(b3DebugFlagHeader, "1") |
| 112 |
} else if !(deferredFromContext(ctx)) { |
| 113 |
if sc.IsSampled() { |
| 114 |
carrier.Set(b3SampledHeader, "1") |
| 115 |
} else { |
| 116 |
carrier.Set(b3SampledHeader, "0") |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// Extract extracts a context from the carrier if it contains B3 headers. |
| 123 |
func (propagator) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context { |
| 124 |
var ( |
| 125 |
sc trace.SpanContext |
| 126 |
err error |
| 127 |
) |
| 128 |
|
| 129 |
// Default to Single Header if a valid value exists. |
| 130 |
if h := carrier.Get(b3ContextHeader); h != "" { |
| 131 |
ctx, sc, err = extractSingle(ctx, h) |
| 132 |
if err == nil && sc.IsValid() { |
| 133 |
return trace.ContextWithRemoteSpanContext(ctx, sc) |
| 134 |
} |
| 135 |
// The Single Header value was invalid, fallback to Multiple Header. |
| 136 |
} |
| 137 |
|
| 138 |
var ( |
| 139 |
traceID = carrier.Get(b3TraceIDHeader) |
| 140 |
spanID = carrier.Get(b3SpanIDHeader) |
| 141 |
parentSpanID = carrier.Get(b3ParentSpanIDHeader) |
| 142 |
sampled = carrier.Get(b3SampledHeader) |
| 143 |
debugFlag = carrier.Get(b3DebugFlagHeader) |
| 144 |
) |
| 145 |
ctx, sc, err = extractMultiple(ctx, traceID, spanID, parentSpanID, sampled, debugFlag) |
| 146 |
if err != nil || !sc.IsValid() { |
| 147 |
// clear the deferred flag if we don't have a valid SpanContext |
| 148 |
return withDeferred(ctx, false) |
| 149 |
} |
| 150 |
return trace.ContextWithRemoteSpanContext(ctx, sc) |
| 151 |
} |
| 152 |
|
| 153 |
func (b3 propagator) Fields() []string { |
| 154 |
header := []string{} |
| 155 |
if b3.cfg.InjectEncoding.supports(B3SingleHeader) { |
| 156 |
header = append(header, b3ContextHeader) |
| 157 |
} |
| 158 |
if b3.cfg.InjectEncoding.supports(B3MultipleHeader) || b3.cfg.InjectEncoding == B3Unspecified { |
| 159 |
header = append(header, b3TraceIDHeader, b3SpanIDHeader, b3SampledHeader, b3DebugFlagHeader) |
| 160 |
} |
| 161 |
return header |
| 162 |
} |
| 163 |
|
| 164 |
// extractMultiple reconstructs a SpanContext from header values based on B3 |
| 165 |
// Multiple header. It is based on the implementation found here: |
| 166 |
// https://github.com/openzipkin/zipkin-go/blob/v0.2.2/propagation/b3/spancontext.go |
| 167 |
// and adapted to support a SpanContext. |
| 168 |
func extractMultiple(ctx context.Context, traceID, spanID, parentSpanID, sampled, flags string) (context.Context, trace.SpanContext, error) { |
| 169 |
var ( |
| 170 |
err error |
| 171 |
requiredCount int |
| 172 |
scc = trace.SpanContextConfig{} |
| 173 |
) |
| 174 |
|
| 175 |
// correct values for an existing sampled header are "0" and "1". |
| 176 |
// For legacy support and being lenient to other tracing implementations we |
| 177 |
// allow "true" and "false" as inputs for interop purposes. |
| 178 |
switch strings.ToLower(sampled) { |
| 179 |
case "0", "false": |
| 180 |
// Zero value for TraceFlags sample bit is unset. |
| 181 |
case "1", "true": |
| 182 |
scc.TraceFlags = trace.FlagsSampled |
| 183 |
case "": |
| 184 |
ctx = withDeferred(ctx, true) |
| 185 |
default: |
| 186 |
return ctx, empty, errInvalidSampledHeader |
| 187 |
} |
| 188 |
|
| 189 |
// The only accepted value for Flags is "1". This will set Debug bitmask and |
| 190 |
// sampled bitmask to 1 since debug implicitly means sampled. All other |
| 191 |
// values and omission of header will be ignored. According to the spec. User |
| 192 |
// shouldn't send X-B3-Sampled header along with X-B3-Flags header. Thus we will |
| 193 |
// ignore X-B3-Sampled header when X-B3-Flags header is sent and valid. |
| 194 |
if flags == "1" { |
| 195 |
ctx = withDeferred(ctx, false) |
| 196 |
ctx = withDebug(ctx, true) |
| 197 |
scc.TraceFlags |= trace.FlagsSampled |
| 198 |
} |
| 199 |
|
| 200 |
if traceID != "" { |
| 201 |
requiredCount++ |
| 202 |
id := traceID |
| 203 |
if len(traceID) == 16 { |
| 204 |
// Pad 64-bit trace IDs. |
| 205 |
id = b3TraceIDPadding + traceID |
| 206 |
} |
| 207 |
if scc.TraceID, err = trace.TraceIDFromHex(id); err != nil { |
| 208 |
return ctx, empty, errInvalidTraceIDHeader |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if spanID != "" { |
| 213 |
requiredCount++ |
| 214 |
if scc.SpanID, err = trace.SpanIDFromHex(spanID); err != nil { |
| 215 |
return ctx, empty, errInvalidSpanIDHeader |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
if requiredCount != 0 && requiredCount != 2 { |
| 220 |
return ctx, empty, errInvalidScope |
| 221 |
} |
| 222 |
|
| 223 |
if parentSpanID != "" { |
| 224 |
if requiredCount == 0 { |
| 225 |
return ctx, empty, errInvalidScopeParent |
| 226 |
} |
| 227 |
// Validate parent span ID but we do not use it so do not save it. |
| 228 |
if _, err = trace.SpanIDFromHex(parentSpanID); err != nil { |
| 229 |
return ctx, empty, errInvalidParentSpanIDHeader |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
return ctx, trace.NewSpanContext(scc), nil |
| 234 |
} |
| 235 |
|
| 236 |
// extractSingle reconstructs a SpanContext from contextHeader based on a B3 |
| 237 |
// Single header. It is based on the implementation found here: |
| 238 |
// https://github.com/openzipkin/zipkin-go/blob/v0.2.2/propagation/b3/spancontext.go |
| 239 |
// and adapted to support a SpanContext. |
| 240 |
func extractSingle(ctx context.Context, contextHeader string) (context.Context, trace.SpanContext, error) { |
| 241 |
if contextHeader == "" { |
| 242 |
return ctx, empty, errEmptyContext |
| 243 |
} |
| 244 |
|
| 245 |
var ( |
| 246 |
scc = trace.SpanContextConfig{} |
| 247 |
sampling string |
| 248 |
) |
| 249 |
|
| 250 |
headerLen := len(contextHeader) |
| 251 |
|
| 252 |
switch { |
| 253 |
case headerLen == samplingWidth: |
| 254 |
sampling = contextHeader |
| 255 |
case headerLen == traceID64BitsWidth || headerLen == traceID128BitsWidth: |
| 256 |
// Trace ID by itself is invalid. |
| 257 |
return ctx, empty, errInvalidScope |
| 258 |
case headerLen >= traceID64BitsWidth+spanIDWidth+separatorWidth: |
| 259 |
pos := 0 |
| 260 |
var traceID string |
| 261 |
switch { |
| 262 |
case string(contextHeader[traceID64BitsWidth]) == "-": |
| 263 |
// traceID must be 64 bits |
| 264 |
pos += traceID64BitsWidth // {traceID} |
| 265 |
traceID = b3TraceIDPadding + contextHeader[0:pos] |
| 266 |
case string(contextHeader[32]) == "-": |
| 267 |
// traceID must be 128 bits |
| 268 |
pos += traceID128BitsWidth // {traceID} |
| 269 |
traceID = contextHeader[0:pos] |
| 270 |
default: |
| 271 |
return ctx, empty, errInvalidTraceIDValue |
| 272 |
} |
| 273 |
var err error |
| 274 |
scc.TraceID, err = trace.TraceIDFromHex(traceID) |
| 275 |
if err != nil { |
| 276 |
return ctx, empty, errInvalidTraceIDValue |
| 277 |
} |
| 278 |
pos += separatorWidth // {traceID}- |
| 279 |
|
| 280 |
if headerLen < pos+spanIDWidth { |
| 281 |
return ctx, empty, errInvalidSpanIDValue |
| 282 |
} |
| 283 |
scc.SpanID, err = trace.SpanIDFromHex(contextHeader[pos : pos+spanIDWidth]) |
| 284 |
if err != nil { |
| 285 |
return ctx, empty, errInvalidSpanIDValue |
| 286 |
} |
| 287 |
pos += spanIDWidth // {traceID}-{spanID} |
| 288 |
|
| 289 |
if headerLen > pos { |
| 290 |
if headerLen == pos+separatorWidth { |
| 291 |
// {traceID}-{spanID}- is invalid. |
| 292 |
return ctx, empty, errInvalidSampledByte |
| 293 |
} |
| 294 |
pos += separatorWidth // {traceID}-{spanID}- |
| 295 |
|
| 296 |
switch headerLen { |
| 297 |
case pos + samplingWidth: |
| 298 |
sampling = string(contextHeader[pos]) |
| 299 |
case pos + parentSpanIDWidth: |
| 300 |
// {traceID}-{spanID}-{parentSpanID} is invalid. |
| 301 |
return ctx, empty, errInvalidScopeParentSingle |
| 302 |
case pos + samplingWidth + separatorWidth + parentSpanIDWidth: |
| 303 |
sampling = string(contextHeader[pos]) |
| 304 |
pos += samplingWidth + separatorWidth // {traceID}-{spanID}-{sampling}- |
| 305 |
|
| 306 |
// Validate parent span ID but we do not use it so do not |
| 307 |
// save it. |
| 308 |
_, err = trace.SpanIDFromHex(contextHeader[pos:]) |
| 309 |
if err != nil { |
| 310 |
return ctx, empty, errInvalidParentSpanIDValue |
| 311 |
} |
| 312 |
default: |
| 313 |
return ctx, empty, errInvalidParentSpanIDValue |
| 314 |
} |
| 315 |
} |
| 316 |
default: |
| 317 |
return ctx, empty, errInvalidTraceIDValue |
| 318 |
} |
| 319 |
switch sampling { |
| 320 |
case "": |
| 321 |
ctx = withDeferred(ctx, true) |
| 322 |
case "d": |
| 323 |
ctx = withDebug(ctx, true) |
| 324 |
scc.TraceFlags = trace.FlagsSampled |
| 325 |
case "1": |
| 326 |
scc.TraceFlags = trace.FlagsSampled |
| 327 |
case "0": |
| 328 |
// Zero value for TraceFlags sample bit is unset. |
| 329 |
default: |
| 330 |
return ctx, empty, errInvalidSampledByte |
| 331 |
} |
| 332 |
|
| 333 |
return ctx, trace.NewSpanContext(scc), nil |
| 334 |
} |