| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package propagation // import "go.opentelemetry.io/otel/propagation" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"encoding/hex" |
| 9 |
"fmt" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel/trace" |
| 13 |
) |
| 14 |
|
| 15 |
const ( |
| 16 |
supportedVersion = 0 |
| 17 |
maxVersion = 254 |
| 18 |
traceparentHeader = "traceparent" |
| 19 |
tracestateHeader = "tracestate" |
| 20 |
delimiter = "-" |
| 21 |
) |
| 22 |
|
| 23 |
// TraceContext is a propagator that supports the W3C Trace Context format |
| 24 |
// (https://www.w3.org/TR/trace-context/) |
| 25 |
// |
| 26 |
// This propagator will propagate the traceparent and tracestate headers to |
| 27 |
// guarantee traces are not broken. It is up to the users of this propagator |
| 28 |
// to choose if they want to participate in a trace by modifying the |
| 29 |
// traceparent header and relevant parts of the tracestate header containing |
| 30 |
// their proprietary information. |
| 31 |
type TraceContext struct{} |
| 32 |
|
| 33 |
var ( |
| 34 |
_ TextMapPropagator = TraceContext{} |
| 35 |
versionPart = fmt.Sprintf("%.2X", supportedVersion) |
| 36 |
) |
| 37 |
|
| 38 |
// Inject injects the trace context from ctx into carrier. |
| 39 |
func (TraceContext) Inject(ctx context.Context, carrier TextMapCarrier) { |
| 40 |
sc := trace.SpanContextFromContext(ctx) |
| 41 |
if !sc.IsValid() { |
| 42 |
return |
| 43 |
} |
| 44 |
|
| 45 |
if ts := sc.TraceState().String(); ts != "" { |
| 46 |
carrier.Set(tracestateHeader, ts) |
| 47 |
} |
| 48 |
|
| 49 |
// Preserve only the spec-defined flags: sampled (0x01) and random (0x02). |
| 50 |
flags := sc.TraceFlags() & (trace.FlagsSampled | trace.FlagsRandom) |
| 51 |
|
| 52 |
var sb strings.Builder |
| 53 |
sb.Grow(2 + 32 + 16 + 2 + 3) |
| 54 |
_, _ = sb.WriteString(versionPart) |
| 55 |
traceID := sc.TraceID() |
| 56 |
spanID := sc.SpanID() |
| 57 |
flagByte := [1]byte{byte(flags)} |
| 58 |
var buf [32]byte |
| 59 |
for _, src := range [][]byte{traceID[:], spanID[:], flagByte[:]} { |
| 60 |
_ = sb.WriteByte(delimiter[0]) |
| 61 |
n := hex.Encode(buf[:], src) |
| 62 |
_, _ = sb.Write(buf[:n]) |
| 63 |
} |
| 64 |
carrier.Set(traceparentHeader, sb.String()) |
| 65 |
} |
| 66 |
|
| 67 |
// Extract reads tracecontext from the carrier into a returned Context. |
| 68 |
// |
| 69 |
// The returned Context will be a copy of ctx and contain the extracted |
| 70 |
// tracecontext as the remote SpanContext. If the extracted tracecontext is |
| 71 |
// invalid, the passed ctx will be returned directly instead. |
| 72 |
func (tc TraceContext) Extract(ctx context.Context, carrier TextMapCarrier) context.Context { |
| 73 |
sc := tc.extract(carrier) |
| 74 |
if !sc.IsValid() { |
| 75 |
return ctx |
| 76 |
} |
| 77 |
return trace.ContextWithRemoteSpanContext(ctx, sc) |
| 78 |
} |
| 79 |
|
| 80 |
func (TraceContext) extract(carrier TextMapCarrier) trace.SpanContext { |
| 81 |
h := carrier.Get(traceparentHeader) |
| 82 |
if h == "" { |
| 83 |
return trace.SpanContext{} |
| 84 |
} |
| 85 |
|
| 86 |
var ver [1]byte |
| 87 |
if !extractPart(ver[:], &h, 2) { |
| 88 |
return trace.SpanContext{} |
| 89 |
} |
| 90 |
version := int(ver[0]) |
| 91 |
if version > maxVersion { |
| 92 |
return trace.SpanContext{} |
| 93 |
} |
| 94 |
|
| 95 |
var scc trace.SpanContextConfig |
| 96 |
if !extractPart(scc.TraceID[:], &h, 32) { |
| 97 |
return trace.SpanContext{} |
| 98 |
} |
| 99 |
if !extractPart(scc.SpanID[:], &h, 16) { |
| 100 |
return trace.SpanContext{} |
| 101 |
} |
| 102 |
|
| 103 |
var opts [1]byte |
| 104 |
if !extractPart(opts[:], &h, 2) { |
| 105 |
return trace.SpanContext{} |
| 106 |
} |
| 107 |
if version == 0 && (h != "" || opts[0] > 3) { |
| 108 |
// version 0 does not allow extra fields or reserved flag bits. |
| 109 |
return trace.SpanContext{} |
| 110 |
} |
| 111 |
|
| 112 |
scc.TraceFlags = trace.TraceFlags(opts[0]) & //nolint:gosec // slice size already checked. |
| 113 |
(trace.FlagsSampled | trace.FlagsRandom) |
| 114 |
|
| 115 |
// Ignore the error returned here. Failure to parse tracestate MUST NOT |
| 116 |
// affect the parsing of traceparent according to the W3C tracecontext |
| 117 |
// specification. |
| 118 |
scc.TraceState, _ = trace.ParseTraceState(carrier.Get(tracestateHeader)) |
| 119 |
scc.Remote = true |
| 120 |
|
| 121 |
sc := trace.NewSpanContext(scc) |
| 122 |
if !sc.IsValid() { |
| 123 |
return trace.SpanContext{} |
| 124 |
} |
| 125 |
|
| 126 |
return sc |
| 127 |
} |
| 128 |
|
| 129 |
// upperHex detect hex is upper case Unicode characters. |
| 130 |
func upperHex(v string) bool { |
| 131 |
for _, c := range v { |
| 132 |
if c >= 'A' && c <= 'F' { |
| 133 |
return true |
| 134 |
} |
| 135 |
} |
| 136 |
return false |
| 137 |
} |
| 138 |
|
| 139 |
func extractPart(dst []byte, h *string, n int) bool { |
| 140 |
part, left, _ := strings.Cut(*h, delimiter) |
| 141 |
*h = left |
| 142 |
// hex.Decode decodes unsupported upper-case characters, so exclude explicitly. |
| 143 |
if len(part) != n || upperHex(part) { |
| 144 |
return false |
| 145 |
} |
| 146 |
if p, err := hex.Decode(dst, []byte(part)); err != nil || p != n/2 { |
| 147 |
return false |
| 148 |
} |
| 149 |
return true |
| 150 |
} |
| 151 |
|
| 152 |
// Fields returns the keys who's values are set with Inject. |
| 153 |
func (TraceContext) Fields() []string { |
| 154 |
return []string{traceparentHeader, tracestateHeader} |
| 155 |
} |