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

Implement IsSampled for OpenTelemetry bridgeSpanContext #3570

Merged
merged 14 commits into from
Feb 23, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- `OtelLibraryName` -> `OTelLibraryName`
- `OtelLibraryVersion` -> `OTelLibraryVersion`
- `OtelStatusDescription` -> `OTelStatusDescription`
- Add `bridgetSpanContext.IsSampled` to `go.opentelemetry.io/otel/bridget/opentracing` to expose whether span is sampled or not. (#3570)
- The `WithInstrumentationAttributes` option to `go.opentelemetry.io/otel/metric`. (#3738)

### Changed
Expand Down
19 changes: 19 additions & 0 deletions bridge/opentracing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,22 @@ When you have started an OpenTracing Span, make sure the OpenTelemetry knows abo
// Propagate the otSpan to both OpenTracing and OpenTelemetry
// instrumentation by using the ctxWithOTAndOTelSpan context.
```

## Extended Functionality

The bridge functionality can be extended beyond the OpenTracing API.

### `SpanContext.IsSampled`

Return the underlying OpenTelemetry [`Span.IsSampled`](https://pkg.go.dev/go.opentelemetry.io/otel/trace#SpanContext.IsSampled) value by converting a `bridgeSpanContext`.

```go
type samplable interface {
IsSampled() bool
}

var sc opentracing.SpanContext = ...
if s, ok := sc.(samplable); ok && s.IsSampled() {
// Do something with sc knowing it is sampled.
}
```
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 @@ -257,6 +257,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 @@ -510,3 +514,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