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

Added methods for SpanID and TraceID on bridgeSpanContext #3966

Merged
merged 14 commits into from
Apr 25, 2023
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- The `go.opentelemetry.io/otel/metric/embedded` package. (#3916)
- The `Version` function to `go.opentelemetry.io/otel/sdk` to return the SDK version. (#3949)
- Methods `bridgeSpanContext.TraceID` and `bridgeSpanContext.SpanID` to `go.opentelemetry.io/otel/bridget/opentracing` to expose traceID and spanID for a span. (#3966)

### Changed

Expand Down
30 changes: 30 additions & 0 deletions bridge/opentracing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,33 @@ if s, ok := sc.(samplable); ok && s.IsSampled() {
// Do something with sc knowing it is sampled.
}
```

### `SpanContext.TraceID`

Return the underlying OpenTelemetry `Span.TraceID` value by converting a `bridgeSpanContext`.

```go
type traceId interface {
TraceID() bool
}

var sc opentracing.SpanContext = ...
if s, ok := sc.(traceId); ok {
// Use TraceID by s.TraceID()
}
```

### `SpanContext.SpanID`

Return the underlying OpenTelemetry `Span.SpanID` value by converting a `bridgeSpanContext`.

```go
type spanId interface {
SpanID() bool
}

var sc opentracing.SpanContext = ...
if s, ok := sc.(spanId); ok {
// Use SpanID by s.SpanID()
}
```
10 changes: 10 additions & 0 deletions bridge/opentracing/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ func (c *bridgeSpanContext) IsSampled() bool {
return c.otelSpanContext.IsSampled()
}

// TraceID returns TraceID of underlying span
func (c *bridgeSpanContext) TraceID() trace.TraceID {
return c.otelSpanContext.TraceID()
}

// SpanID returns SpanID of underlying span
func (c *bridgeSpanContext) SpanID() trace.SpanID {
return c.otelSpanContext.SpanID()
}

func (c *bridgeSpanContext) setBaggageItem(restrictedKey, value string) {
crk := http.CanonicalHeaderKey(restrictedKey)
m, err := baggage.NewMember(crk, value)
Expand Down
43 changes: 43 additions & 0 deletions bridge/opentracing/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,14 @@ type samplable interface {
IsSampled() bool
}

type traceId interface {
TraceID() trace.TraceID
}

type spanId interface {
SpanID() trace.SpanID
}
Kaushal28 marked this conversation as resolved.
Show resolved Hide resolved

func TestBridgeTracer_ExtractAndInject(t *testing.T) {
bridge := NewBridgeTracer()
bridge.SetTextMapPropagator(new(testTextMapPropagator))
Expand Down Expand Up @@ -546,3 +554,38 @@ func TestBridge_SpanContext_IsSampled(t *testing.T) {
})
}
}

func TestBridge_SpanContext_TraceID_SpanID(t *testing.T) {
bridge := NewBridgeTracer()
bridge.SetTextMapPropagator(new(testTextMapPropagator))

tmc := newTextCarrier()

testCases := []struct {
name string
traceID trace.TraceID
spanID trace.SpanID
}{
{
name: "support for getting spanID and traceID",
traceID: [16]byte{byte(1)},
spanID: [8]byte{byte(2)},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Kaushal28 marked this conversation as resolved.
Show resolved Hide resolved
err := bridge.Inject(newBridgeSpanContext(trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tc.traceID,
SpanID: tc.spanID,
}), nil), ot.TextMap, tmc)
assert.NoError(t, err)

spanContext, err := bridge.Extract(ot.TextMap, tmc)
assert.NoError(t, err)

assert.Equal(t, spanID.String(), spanContext.(spanId).SpanID().String())
assert.Equal(t, traceID.String(), spanContext.(traceId).TraceID().String())
Kaushal28 marked this conversation as resolved.
Show resolved Hide resolved
})
}
}