Skip to content

Commit

Permalink
changes to propogate baggage by open tracing bridge
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Bansal <[email protected]>
  • Loading branch information
shubham-bansal96 committed Apr 20, 2023
1 parent eb2b89f commit 2ec2755
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 31 additions & 0 deletions bridge/opentracing/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, &copySpanContext, extractedSpanContext, "Extracted span context should match with original span context")
}
19 changes: 18 additions & 1 deletion propagation/trace_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"regexp"

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

Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand Down

0 comments on commit 2ec2755

Please sign in to comment.