Skip to content

Commit 05c5509

Browse files
dmathieuMrAlias
andauthored
Add tests and fix opentracing bridge defer warning (#3029)
* add tests and fix opentracing bridge defer warning * add changelog entry * Update CHANGELOG.md Co-authored-by: Tyler Yahn <[email protected]> * Update bridge/opentracing/bridge_test.go Co-authored-by: Tyler Yahn <[email protected]> Co-authored-by: Tyler Yahn <[email protected]>
1 parent 096a162 commit 05c5509

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1616
The package contains semantic conventions from the `v1.11.0` version of the OpenTelemetry specification. (#3009)
1717
- Add http.method attribute to http server metric. (#3018)
1818

19+
### Fixed
20+
21+
- Invalid warning for context setup being deferred in OpenTracing bridge (#3029).
22+
1923
## [1.8.0/0.31.0] - 2022-07-08
2024

2125
### Added

bridge/opentracing/bridge.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ var _ ot.TracerContextWithSpanExtension = &BridgeTracer{}
328328
func NewBridgeTracer() *BridgeTracer {
329329
return &BridgeTracer{
330330
setTracer: bridgeSetTracer{
331-
otelTracer: noopTracer,
331+
warningHandler: func(msg string) {},
332+
otelTracer: noopTracer,
332333
},
333334
warningHandler: func(msg string) {},
334335
propagator: nil,
@@ -434,7 +435,7 @@ func (t *BridgeTracer) StartSpan(operationName string, opts ...ot.StartSpanOptio
434435
trace.WithLinks(links...),
435436
trace.WithSpanKind(kind),
436437
)
437-
if checkCtx != checkCtx2 {
438+
if ot.SpanFromContext(checkCtx2) != nil {
438439
t.warnOnce.Do(func() {
439440
t.warningHandler("SDK should have deferred the context setup, see the documentation of go.opentelemetry.io/otel/bridge/opentracing/migration\n")
440441
})

bridge/opentracing/bridge_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
ot "github.com/opentracing/opentracing-go"
2525
"github.com/stretchr/testify/assert"
2626

27+
"go.opentelemetry.io/otel"
2728
"go.opentelemetry.io/otel/propagation"
2829
"go.opentelemetry.io/otel/trace"
2930
)
@@ -360,3 +361,67 @@ func TestBridgeTracer_ExtractAndInject(t *testing.T) {
360361
})
361362
}
362363
}
364+
365+
type nonDeferWrapperTracer struct {
366+
*WrapperTracer
367+
}
368+
369+
func (t *nonDeferWrapperTracer) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
370+
// Run start on the parent wrapper with a brand new context
371+
// so `WithDeferredSetup` hasn't been called, and the OpenTracing context is injected.
372+
return t.WrapperTracer.Start(context.Background(), name, opts...)
373+
}
374+
375+
func TestBridgeTracer_StartSpan(t *testing.T) {
376+
testCases := []struct {
377+
name string
378+
before func(*testing.T, *BridgeTracer)
379+
expectWarnings []string
380+
}{
381+
{
382+
name: "with no option set",
383+
expectWarnings: []string{
384+
"The OpenTelemetry tracer is not set, default no-op tracer is used! Call SetOpenTelemetryTracer to set it up.\n",
385+
},
386+
},
387+
{
388+
name: "with wrapper tracer set",
389+
before: func(t *testing.T, bridge *BridgeTracer) {
390+
wTracer := NewWrapperTracer(bridge, otel.Tracer("test"))
391+
bridge.SetOpenTelemetryTracer(wTracer)
392+
},
393+
expectWarnings: []string(nil),
394+
},
395+
{
396+
name: "with a non-defered wrapper tracer",
397+
before: func(t *testing.T, bridge *BridgeTracer) {
398+
wTracer := &nonDeferWrapperTracer{
399+
NewWrapperTracer(bridge, otel.Tracer("test")),
400+
}
401+
bridge.SetOpenTelemetryTracer(wTracer)
402+
},
403+
expectWarnings: []string{
404+
"SDK should have deferred the context setup, see the documentation of go.opentelemetry.io/otel/bridge/opentracing/migration\n",
405+
},
406+
},
407+
}
408+
409+
for _, tc := range testCases {
410+
t.Run(tc.name, func(t *testing.T) {
411+
var warningMessages []string
412+
bridge := NewBridgeTracer()
413+
bridge.SetWarningHandler(func(msg string) {
414+
warningMessages = append(warningMessages, msg)
415+
})
416+
417+
if tc.before != nil {
418+
tc.before(t, bridge)
419+
}
420+
421+
span := bridge.StartSpan("test")
422+
assert.NotNil(t, span)
423+
424+
assert.Equal(t, tc.expectWarnings, warningMessages)
425+
})
426+
}
427+
}

0 commit comments

Comments
 (0)