Skip to content

Commit

Permalink
Add IsSampled to OpenTracing bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
Pijus Navickas committed Jan 6, 2023
1 parent 0851690 commit 752c12a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions bridge/opentracing/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func (c *bridgeSpanContext) ForeachBaggageItem(handler func(k, v string) bool) {
}
}

func (c *bridgeSpanContext) IsSampled() bool {
return c.otelSpanContext.IsSampled()
}

func (c *bridgeSpanContext) setBaggageItem(restrictedKey, value string) {
crk := http.CanonicalHeaderKey(restrictedKey)
m, err := baggage.NewMember(crk, value)
Expand Down
36 changes: 36 additions & 0 deletions bridge/opentracing/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ func (t *testTextMapWriter) Set(key, val string) {
(*t.m)[key] = val
}

type samplable interface {
IsSampled() bool
}

func TestBridgeTracer_ExtractAndInject(t *testing.T) {
bridge := NewBridgeTracer()
bridge.SetTextMapPropagator(new(testTextMapPropagator))
Expand Down Expand Up @@ -503,3 +507,35 @@ func Test_otTagsToOTelAttributesKindAndError(t *testing.T) {
})
}
}

func TestBridge_SpanContext_IsSampled(t *testing.T) {
testCases := []struct {
name string
flags trace.TraceFlags
expected bool
}{
{
name: "not sampled",
flags: 0,
expected: false,
},
{
name: "sampled",
flags: trace.FlagsSampled,
expected: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tracer := internal.NewMockTracer()
tracer.TraceFlags = tc.flags

b, _ := NewTracerPair(tracer)
s := b.StartSpan("abc")
sc := s.Context()

assert.Equal(t, tc.expected, sc.(samplable).IsSampled())
})
}
}
3 changes: 2 additions & 1 deletion bridge/opentracing/internal/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type MockTracer struct {
SpareTraceIDs []trace.TraceID
SpareSpanIDs []trace.SpanID
SpareContextKeyValues []MockContextKeyValue
TraceFlags trace.TraceFlags

randLock sync.Mutex
rand *rand.Rand
Expand Down Expand Up @@ -76,7 +77,7 @@ func (t *MockTracer) Start(ctx context.Context, name string, opts ...trace.SpanS
spanContext := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: t.getTraceID(ctx, &config),
SpanID: t.getSpanID(),
TraceFlags: 0,
TraceFlags: t.TraceFlags,
})
span := &MockSpan{
mockTracer: t,
Expand Down

0 comments on commit 752c12a

Please sign in to comment.