Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle tracestate in TraceContext propagator #1447

Merged
merged 4 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -31,6 +31,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Metric aggregator Count() and histogram Bucket.Counts are consistently `uint64`. (1430)
- `SamplingResult` now passed a `Tracestate` from the parent `SpanContext` (#1432)
- Moved gRPC driver for OTLP exporter to `exporters/otlp/otlpgrpc`. (#1420)
- The `TraceContext` propagator now correctly propagates `TraceState` through the `SpanContext`. (#1447)

### Removed

Expand Down
40 changes: 24 additions & 16 deletions propagation/trace_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
"encoding/hex"
"fmt"
"regexp"
"strings"

"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/trace"
)

Expand All @@ -30,12 +32,6 @@ const (
tracestateHeader = "tracestate"
)

type traceContextPropagatorKeyType uint

const (
tracestateKey traceContextPropagatorKeyType = 0
)

// TraceContext is a propagator that supports the W3C Trace Context format
// (https://www.w3.org/TR/trace-context/)
//
Expand All @@ -51,15 +47,13 @@ var traceCtxRegExp = regexp.MustCompile("^(?P<version>[0-9a-f]{2})-(?P<traceID>[

// Inject set tracecontext from the Context into the carrier.
func (tc TraceContext) Inject(ctx context.Context, carrier TextMapCarrier) {
tracestate := ctx.Value(tracestateKey)
if state, ok := tracestate.(string); tracestate != nil && ok {
carrier.Set(tracestateHeader, state)
}

sc := trace.SpanContextFromContext(ctx)
if !sc.IsValid() {
return
}

carrier.Set(tracestateHeader, sc.TraceState.String())

h := fmt.Sprintf("%.2x-%s-%s-%.2x",
supportedVersion,
sc.TraceID,
Expand All @@ -70,11 +64,6 @@ func (tc TraceContext) Inject(ctx context.Context, carrier TextMapCarrier) {

// Extract reads tracecontext from the carrier into a returned Context.
func (tc TraceContext) Extract(ctx context.Context, carrier TextMapCarrier) context.Context {
state := carrier.Get(tracestateHeader)
if state != "" {
ctx = context.WithValue(ctx, tracestateKey, state)
}

sc := tc.extract(carrier)
if !sc.IsValid() {
return ctx
Expand Down Expand Up @@ -143,6 +132,12 @@ func (tc TraceContext) extract(carrier TextMapCarrier) trace.SpanContext {
// Clear all flags other than the trace-context supported sampling bit.
sc.TraceFlags = opts[0] & trace.FlagsSampled

ts, err := parseTraceState(carrier.Get(tracestateHeader))
if err != nil {
return trace.SpanContext{}
Aneurysm9 marked this conversation as resolved.
Show resolved Hide resolved
}
sc.TraceState = ts

if !sc.IsValid() {
return trace.SpanContext{}
}
Expand All @@ -154,3 +149,16 @@ func (tc TraceContext) extract(carrier TextMapCarrier) trace.SpanContext {
func (tc TraceContext) Fields() []string {
return []string{traceparentHeader, tracestateHeader}
}

func parseTraceState(in string) (trace.TraceState, error) {
if in == "" {
return trace.TraceState{}, nil
}

kvs := []label.KeyValue{}
for _, entry := range strings.Split(in, ",") {
parts := strings.SplitN(entry, "=", 2)
kvs = append(kvs, label.String(parts[0], parts[1]))
}
return trace.TraceStateFromKeyValues(kvs...)
}
69 changes: 60 additions & 9 deletions propagation/trace_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/google/go-cmp/cmp"

"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/oteltest"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -277,17 +278,67 @@ func TestTraceContextPropagator_GetAllKeys(t *testing.T) {

func TestTraceStatePropagation(t *testing.T) {
prop := propagation.TraceContext{}
want := "opaquevalue"
headerName := "tracestate"
stateHeader := "tracestate"
parentHeader := "traceparent"
state, _ := trace.TraceStateFromKeyValues(label.String("key1", "value1"), label.String("key2", "value2"))
Aneurysm9 marked this conversation as resolved.
Show resolved Hide resolved

inReq, _ := http.NewRequest(http.MethodGet, "http://example.com", nil)
inReq.Header.Add(headerName, want)
ctx := prop.Extract(context.Background(), inReq.Header)
tests := []struct {
name string
headers map[string]string
valid bool
wantSc trace.SpanContext
}{
{
name: "valid parent and state",
headers: map[string]string{
parentHeader: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
stateHeader: "key1=value1,key2=value2",
},
valid: true,
wantSc: trace.SpanContext{
TraceID: traceID,
SpanID: spanID,
TraceState: state,
},
},
{
name: "valid parent, invalid state",
headers: map[string]string{
parentHeader: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
stateHeader: "key1=value1,invalid$@#=invalid",
},
valid: false,
wantSc: trace.SpanContext{},
},
}

outReq, _ := http.NewRequest(http.MethodGet, "http://www.example.com", nil)
prop.Inject(ctx, outReq.Header)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inReq, _ := http.NewRequest(http.MethodGet, "http://example.com", nil)
for hk, hv := range tt.headers {
inReq.Header.Add(hk, hv)
}

if diff := cmp.Diff(outReq.Header.Get(headerName), want); diff != "" {
t.Errorf("Propagate tracestate: -got +want %s", diff)
ctx := prop.Extract(context.Background(), inReq.Header)
if diff := cmp.Diff(
trace.RemoteSpanContextFromContext(ctx),
tt.wantSc,
cmp.AllowUnexported(label.Value{}),
cmp.AllowUnexported(trace.TraceState{}),
); diff != "" {
t.Errorf("Extracted tracestate: -got +want %s", diff)
}

if tt.valid {
mockTracer := oteltest.DefaultTracer()
ctx, _ = mockTracer.Start(ctx, "inject")
outReq, _ := http.NewRequest(http.MethodGet, "http://www.example.com", nil)
prop.Inject(ctx, outReq.Header)

if diff := cmp.Diff(outReq.Header.Get(stateHeader), tt.headers[stateHeader]); diff != "" {
t.Errorf("Propagated tracestate: -got +want %s", diff)
}
}
})
}
}