Skip to content

Commit

Permalink
Implement IsSampled for OpenTelemetry bridgeSpanContext (#3570)
Browse files Browse the repository at this point in the history
* Add IsSampled to OpenTracing bridge

* Add entry to CHANGELOG

* Add note to the README

* Update CHANGELOG.md

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* Update bridge/opentracing/README.md

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* Update bridge/opentracing/README.md

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* Update bridge/opentracing/README.md

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* Update bridge/opentracing/README.md

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* Add PR ID to changelog note

---------

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
pijusn and MrAlias committed Feb 23, 2023
1 parent 99ec432 commit 69d0946
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
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)
- The `WithInstrumentationAttributes` option to `go.opentelemetry.io/otel/trace`. (#3739)

Expand Down
19 changes: 19 additions & 0 deletions bridge/opentracing/README.md
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
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
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
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 69d0946

Please sign in to comment.