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

bridge/opentracing: introduce NewTracerProvider that wraps TracerProvider instead of Tracer #3116

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions bridge/opentracing/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package opentracing // import "go.opentelemetry.io/otel/bridge/opentracing"

import (
"context"
"sync"

"go.opentelemetry.io/otel/bridge/opentracing/migration"
"go.opentelemetry.io/otel/trace"
Expand All @@ -24,21 +25,62 @@ import (
// WrapperTracerProvider is an OpenTelemetry TracerProvider that wraps an
// OpenTracing Tracer.
type WrapperTracerProvider struct {
wTracer *WrapperTracer
getWrappedTracer func(name string, opts ...trace.TracerOption) *WrapperTracer
}

var _ trace.TracerProvider = (*WrapperTracerProvider)(nil)

// Tracer returns the WrapperTracer associated with the WrapperTracerProvider.
func (p *WrapperTracerProvider) Tracer(_ string, _ ...trace.TracerOption) trace.Tracer {
return p.wTracer
// Tracer creates a WrapperTracer associated with the WrapperTracerProvider.
func (p *WrapperTracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.Tracer {
return p.getWrappedTracer(name, opts...)
}

// Deprecated: Use NewTracerProvider(...) instead.
//
bobheadxi marked this conversation as resolved.
Show resolved Hide resolved
// NewWrappedTracerProvider creates a new trace provider that creates a single
// instance of WrapperTracer that wraps OpenTelemetry tracer.
// instance of WrapperTracer that wraps OpenTelemetry tracer, and always returns
// it unmodified from Tracer().
func NewWrappedTracerProvider(bridge *BridgeTracer, tracer trace.Tracer) *WrapperTracerProvider {
wTracer := NewWrapperTracer(bridge, tracer)
return &WrapperTracerProvider{
wTracer: NewWrapperTracer(bridge, tracer),
getWrappedTracer: func(_ string, _ ...trace.TracerOption) *WrapperTracer {
return wTracer
},
}
}

type wrappedTracerKey struct {
name string
version string
}

// NewTracerProvider creates a new trace provider that creates new instances of
bobheadxi marked this conversation as resolved.
Show resolved Hide resolved
// WrapperTracer that wraps OpenTelemetry tracer for each call to Tracer().
func NewTracerProvider(bridge *BridgeTracer, provider trace.TracerProvider) *WrapperTracerProvider {
bobheadxi marked this conversation as resolved.
Show resolved Hide resolved
var (
mtx sync.Mutex
tracers = make(map[wrappedTracerKey]*WrapperTracer)
)

return &WrapperTracerProvider{
getWrappedTracer: func(name string, opts ...trace.TracerOption) *WrapperTracer {
mtx.Lock()
defer mtx.Unlock()

c := trace.NewTracerConfig(opts...)
key := wrappedTracerKey{
name: name,
version: c.InstrumentationVersion(),
}

if t, ok := tracers[key]; ok {
return t
}

wrapper := NewWrapperTracer(bridge, provider.Tracer(name, opts...))
tracers[key] = wrapper
return wrapper
},
}
}

Expand Down
85 changes: 85 additions & 0 deletions bridge/opentracing/wrapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package opentracing

import (
"testing"

"go.opentelemetry.io/otel/bridge/opentracing/internal"
"go.opentelemetry.io/otel/trace"
)

type namedMockTracer struct {
name string
*internal.MockTracer
}

type namedMockTracerProvider struct{}

var _ trace.TracerProvider = (*namedMockTracerProvider)(nil)

// Tracer returns the WrapperTracer associated with the WrapperTracerProvider.
func (p *namedMockTracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.Tracer {
return &namedMockTracer{
name: name,
MockTracer: internal.NewMockTracer(),
}
}

func TestTracerProvider(t *testing.T) {
// assertMockTracerName casts tracer into a named mock tracer provided by
// namedMockTracerProvider, and asserts against its name
assertMockTracerName := func(t *testing.T, tracer trace.Tracer, name string) {
// Unwrap the tracer
wrapped := tracer.(*WrapperTracer)
tracer = wrapped.tracer

// Cast into the underlying type and assert
if mock, ok := tracer.(*namedMockTracer); ok {
if name != mock.name {
t.Errorf("expected name %q, got %q", name, mock.name)
}
} else if !ok {
t.Errorf("expected *namedMockTracer, got %T", mock)
}
}

var (
foobar = "foobar"
bazbar = "bazbar"
provider = NewTracerProvider(nil, &namedMockTracerProvider{})
)

t.Run("Tracers should be created with foobar from provider", func(t *testing.T) {
tracer := provider.Tracer(foobar)
assertMockTracerName(t, tracer, foobar)
})

t.Run("Repeated requests to create a tracer should provide the existing tracer", func(t *testing.T) {
tracer1 := provider.Tracer(foobar)
assertMockTracerName(t, tracer1, foobar)
tracer2 := provider.Tracer(foobar)
assertMockTracerName(t, tracer2, foobar)
tracer3 := provider.Tracer(bazbar)
assertMockTracerName(t, tracer3, bazbar)

if tracer1 != tracer2 {
t.Errorf("expected the same tracer, got different tracers")
}
if tracer1 == tracer3 || tracer2 == tracer3 {
t.Errorf("expected different tracers, got the same tracer")
}
})
}