From ed920d20bd5ea4e9573b8922129287b310c1cbba Mon Sep 17 00:00:00 2001 From: Chen Xu Date: Thu, 13 Apr 2023 23:40:24 -0700 Subject: [PATCH] [ot bridge] support baggage-only propagation without trace context Signed-off-by: Chen Xu --- bridge/opentracing/bridge.go | 2 +- bridge/opentracing/bridge_test.go | 27 +++++++++++++++++++++++++++ bridge/opentracing/internal/mock.go | 26 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/bridge/opentracing/bridge.go b/bridge/opentracing/bridge.go index 867a2a8c387a..6b75c8d083c9 100644 --- a/bridge/opentracing/bridge.go +++ b/bridge/opentracing/bridge.go @@ -732,7 +732,7 @@ func (t *BridgeTracer) Extract(format interface{}, carrier interface{}) (ot.Span bag: bag, otelSpanContext: trace.SpanContextFromContext(ctx), } - if !bridgeSC.otelSpanContext.IsValid() { + if !bridgeSC.otelSpanContext.IsValid() && bridgeSC.bag.Len() == 0 { return nil, ot.ErrSpanContextNotFound } return bridgeSC, nil diff --git a/bridge/opentracing/bridge_test.go b/bridge/opentracing/bridge_test.go index 20c21a875650..c29346ec6bdd 100644 --- a/bridge/opentracing/bridge_test.go +++ b/bridge/opentracing/bridge_test.go @@ -27,6 +27,7 @@ import ( "github.com/opentracing/opentracing-go/ext" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/baggage" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" @@ -378,6 +379,32 @@ func TestBridgeTracer_ExtractAndInject(t *testing.T) { } } +func TestBridgeTracer_ExtractOnlyBaggage(t *testing.T) { + testMember, err := baggage.NewMember("key", "val") + require.NoError(t, err) + testBaggage, err := baggage.New(testMember) + require.NoError(t, err) + + customExtract := func(ctx context.Context, carrier propagation.TextMapCarrier) context.Context { + return baggage.ContextWithBaggage(ctx, testBaggage) + } + mockPropagator := &internal.MockTextMapPropagator{ExtractFunc: customExtract} + + bridge := NewBridgeTracer() + bridge.SetTextMapPropagator(mockPropagator) + spanContext, err := bridge.Extract(ot.TextMap, ot.TextMapCarrier{}) + assert.NoError(t, err) + + bridgeSpanContext, ok := spanContext.(*bridgeSpanContext) + assert.True(t, ok) + require.NotNil(t, bridgeSpanContext.otelSpanContext) + assert.False(t, bridgeSpanContext.otelSpanContext.IsValid()) + assert.False(t, bridgeSpanContext.otelSpanContext.IsSampled()) + assert.Equal(t, 0, bridgeSpanContext.otelSpanContext.TraceState().Len()) + assert.Equal(t, 1, bridgeSpanContext.bag.Len()) + assert.Equal(t, testMember, bridgeSpanContext.bag.Member("key")) +} + type nonDeferWrapperTracer struct { *WrapperTracer } diff --git a/bridge/opentracing/internal/mock.go b/bridge/opentracing/internal/mock.go index bb44e93d1f34..330249773cfd 100644 --- a/bridge/opentracing/internal/mock.go +++ b/bridge/opentracing/internal/mock.go @@ -24,6 +24,7 @@ import ( "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/bridge/opentracing/migration" "go.opentelemetry.io/otel/codes" + "go.opentelemetry.io/otel/propagation" semconv "go.opentelemetry.io/otel/semconv/v1.17.0" "go.opentelemetry.io/otel/trace" ) @@ -292,3 +293,28 @@ func (s *MockSpan) OverrideTracer(tracer trace.Tracer) { } func (s *MockSpan) TracerProvider() trace.TracerProvider { return trace.NewNoopTracerProvider() } + +type MockTextMapPropagator struct { + FieldsToReturn []string + InjectFunc func(ctx context.Context, carrier propagation.TextMapCarrier) + ExtractFunc func(ctx context.Context, carrier propagation.TextMapCarrier) context.Context +} + +var _ propagation.TextMapPropagator = (*MockTextMapPropagator)(nil) + +func (p *MockTextMapPropagator) Inject(ctx context.Context, carrier propagation.TextMapCarrier) { + if p.InjectFunc != nil { + p.InjectFunc(ctx, carrier) + } +} + +func (p *MockTextMapPropagator) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context { + if p.ExtractFunc == nil { + return ctx + } + return p.ExtractFunc(ctx, carrier) +} + +func (p *MockTextMapPropagator) Fields() []string { + return p.FieldsToReturn +}