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

[OpenTracing Bridge] Support baggage-only propagation without trace context #4000

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- `metric.NewNoopMeterProvider` is replaced with `noop.NewMeterProvider`
- Wrap `UploadMetrics` error in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/` to improve error message when encountering generic grpc errors. (#3974)
- Move global metric back to `go.opentelemetry.io/otel/metric/global` from `go.opentelemetry.io/otel`. (#3986)
- Support baggage-only propagation without trace context in OpenTracing Bridge (#4000).

### Fixed

Expand Down
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
}