Skip to content

Commit

Permalink
[ot bridge] support baggage-only propagation without trace context
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Xu <[email protected]>
  • Loading branch information
ChenX1993 committed Apr 14, 2023
1 parent eb2b89f commit ed920d2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bridge/opentracing/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions bridge/opentracing/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
26 changes: 26 additions & 0 deletions bridge/opentracing/internal/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}

0 comments on commit ed920d2

Please sign in to comment.