Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Fix bad log message when key-value pairs are dropped because of key duplication in `go.opentelemetry.io/otel/sdk/log`. (#7662)
- Fix `DroppedAttributes` on `Record` in `go.opentelemetry.io/otel/sdk/log` to not count the non-attribute key-value pairs dropped because of key duplication. (#7662)
- Fix `SetAttributes` on `Record` in `go.opentelemetry.io/otel/sdk/log` to not log that attributes are dropped when they are actually not dropped. (#7662)
- Preserve W3C TraceFlags bitmask (including the random Trace ID flag) during trace context extraction and injection in `go.opentelemetry.io/otel/propagation`. (#7635)
- `WithHostID` detector in `go.opentelemetry.io/otel/sdk/resource` to use full path for `ioreg` command on Darwin (macOS). (#7818)
- Fix missing `request.GetBody` in `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp` to correctly handle HTTP2 GOAWAY frame. (#7794)

Expand Down
9 changes: 3 additions & 6 deletions propagation/trace_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ func (TraceContext) Inject(ctx context.Context, carrier TextMapCarrier) {
carrier.Set(tracestateHeader, ts)
}

// Clear all flags other than the trace-context supported sampling bit.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we were doing this intentionally before. I'm hesitant to remove this behavior without understanding why it was added in the first place.

flags := sc.TraceFlags() & trace.FlagsSampled
flags := sc.TraceFlags()

var sb strings.Builder
sb.Grow(2 + 32 + 16 + 2 + 3)
Expand Down Expand Up @@ -104,14 +103,12 @@ func (TraceContext) extract(carrier TextMapCarrier) trace.SpanContext {
if !extractPart(opts[:], &h, 2) {
return trace.SpanContext{}
}
if version == 0 && (h != "" || opts[0] > 2) {
if version == 0 && h != "" {
// version 0 not allow extra
// version 0 not allow other flag
return trace.SpanContext{}
}

// Clear all flags other than the trace-context supported sampling bit.
scc.TraceFlags = trace.TraceFlags(opts[0]) & trace.FlagsSampled // nolint:gosec // slice size already checked.
scc.TraceFlags = trace.TraceFlags(opts[0]) // nolint:gosec // slice size already checked.

// Ignore the error returned here. Failure to parse tracestate MUST NOT
// affect the parsing of traceparent according to the W3C tracecontext
Expand Down
83 changes: 72 additions & 11 deletions propagation/trace_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,42 @@ func TestExtractValidTraceContext(t *testing.T) {
Remote: true,
}),
},
{
name: "random",
header: http.Header{
traceparent: []string{"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-02"},
},
sc: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
TraceFlags: trace.TraceFlags(0x02),
Remote: true,
}),
},
{
name: "sampled and random",
header: http.Header{
traceparent: []string{"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-03"},
},
sc: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
TraceFlags: trace.TraceFlags(0x03),
Remote: true,
}),
},
{
name: "additional flag bits preserved",
header: http.Header{
traceparent: []string{"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-09"},
},
sc: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
TraceFlags: trace.TraceFlags(0x09),
Remote: true,
}),
},
{
name: "valid tracestate",
header: http.Header{
Expand Down Expand Up @@ -112,7 +148,7 @@ func TestExtractValidTraceContext(t *testing.T) {
sc: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
TraceFlags: trace.FlagsSampled,
TraceFlags: trace.TraceFlags(0x09),
Remote: true,
}),
},
Expand All @@ -122,9 +158,10 @@ func TestExtractValidTraceContext(t *testing.T) {
traceparent: []string{"02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-08"},
},
sc: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
Remote: true,
TraceID: traceID,
SpanID: spanID,
TraceFlags: trace.TraceFlags(0x08),
Remote: true,
}),
},
{
Expand Down Expand Up @@ -228,10 +265,6 @@ func TestExtractInvalidTraceContextFromHTTPReq(t *testing.T) {
name: "zero trace ID and span ID",
header: "00-00000000000000000000000000000000-0000000000000000-01",
},
{
name: "trace-flag unused bits set",
header: "00-ab000000000000000000000000000000-cd00000000000000-09",
},
{
name: "missing options",
header: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7",
Expand All @@ -240,6 +273,10 @@ func TestExtractInvalidTraceContextFromHTTPReq(t *testing.T) {
name: "empty options",
header: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-",
},
{
name: "version 0 with extra content",
header: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01-extra",
},
}

empty := trace.SpanContext{}
Expand Down Expand Up @@ -287,14 +324,38 @@ func TestInjectValidTraceContext(t *testing.T) {
}),
},
{
name: "unsupported trace flag bits dropped",
name: "trace flag bits preserved",
header: http.Header{
traceparent: []string{"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"},
traceparent: []string{"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-ff"},
},
sc: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
TraceFlags: trace.TraceFlags(0xff),
Remote: true,
}),
},
{
name: "random",
header: http.Header{
traceparent: []string{"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-02"},
},
sc: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
TraceFlags: trace.TraceFlags(0x02),
Remote: true,
}),
},
{
name: "sampled and random",
header: http.Header{
traceparent: []string{"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-03"},
},
sc: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
TraceFlags: 0xff,
TraceFlags: trace.TraceFlags(0x03),
Remote: true,
}),
},
Expand Down