diff --git a/CHANGELOG.md b/CHANGELOG.md index 952beab76c2..179b1f96306 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Add a `WithNamespace` option to `go.opentelemetry.io/otel/exporters/prometheus` to allow users to prefix metrics with a namespace. (#3970) - The `Version` function to `go.opentelemetry.io/otel/exporters/otlp/otlpmetric` to return the OTLP metrics client version. (#3956) - The `Version` function to `go.opentelemetry.io/otel/exporters/otlp/otlptrace` to return the OTLP trace client version. (#3956) +- Added baggage to propagate through opentracing bridge. (#4021) ### Changed diff --git a/bridge/opentracing/bridge_test.go b/bridge/opentracing/bridge_test.go index 20c21a87565..fda7d78c174 100644 --- a/bridge/opentracing/bridge_test.go +++ b/bridge/opentracing/bridge_test.go @@ -546,3 +546,34 @@ func TestBridge_SpanContext_IsSampled(t *testing.T) { }) } } + +func TestOTHTTPHeadersCarrier(t *testing.T) { + carrier := ot.HTTPHeadersCarrier{} + testBridgeWithCarrier(t, carrier) +} + +func TestOTTextMapCarrier(t *testing.T) { + carrier := ot.TextMapCarrier{} + testBridgeWithCarrier(t, carrier) +} + +func testBridgeWithCarrier(t *testing.T, carrier interface{}) { + mockOtelTracer := internal.NewMockTracer() + bridgeTracer, _ := NewTracerPair(mockOtelTracer) + otel.SetTextMapPropagator(propagation.TraceContext{}) + span := bridgeTracer.StartSpan("testSpan1") + // adding data to baggage + span.SetBaggageItem("foo", "bar") + defer span.Finish() + + err := bridgeTracer.Inject(span.Context(), ot.HTTPHeaders, carrier) + assert.NoError(t, err, "Inject error") + + extractedSpanContext, err := bridgeTracer.Extract(ot.HTTPHeaders, carrier) + assert.NoError(t, err, "Extract error") + + originalSpanContext := span.Context().(*bridgeSpanContext) + copySpanContext := *originalSpanContext + copySpanContext.otelSpanContext = copySpanContext.otelSpanContext.WithRemote(true) + assert.Equal(t, ©SpanContext, extractedSpanContext, "Extracted span context should match with original span context") +} diff --git a/propagation/trace_context.go b/propagation/trace_context.go index 902692da082..4f154e52d49 100644 --- a/propagation/trace_context.go +++ b/propagation/trace_context.go @@ -20,6 +20,7 @@ import ( "fmt" "regexp" + "go.opentelemetry.io/otel/baggage" "go.opentelemetry.io/otel/trace" ) @@ -63,6 +64,11 @@ func (tc TraceContext) Inject(ctx context.Context, carrier TextMapCarrier) { sc.SpanID(), flags) carrier.Set(traceparentHeader, h) + + bStr := baggage.FromContext(ctx).String() + if bStr != "" { + carrier.Set(baggageHeader, bStr) + } } // Extract reads tracecontext from the carrier into a returned Context. @@ -75,7 +81,18 @@ func (tc TraceContext) Extract(ctx context.Context, carrier TextMapCarrier) cont if !sc.IsValid() { return ctx } - return trace.ContextWithRemoteSpanContext(ctx, sc) + + parent := trace.ContextWithRemoteSpanContext(ctx, sc) + + bStr := carrier.Get(baggageHeader) + if bStr == "" { + return parent + } + bag, err := baggage.Parse(bStr) + if err != nil { + return parent + } + return baggage.ContextWithBaggage(parent, bag) } func (tc TraceContext) extract(carrier TextMapCarrier) trace.SpanContext {