mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-10 09:50:58 +02:00
640a0cd8bc
* bridge/opentracing: add NewDynamicWrappedTracerProvider for named tracers * bridge/opentelmeetry: cache created Tracers, add tests * bridge/opentracing: rename constructor to NewTracerProvider * add license header * Update docstring Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * fix deprecated docstring * rewrite new lookup TracerProvider as separate type * update docstring * add changelog entries * Update bridge/opentracing/provider.go Co-authored-by: Damien Mathieu <42@dmathieu.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Damien Mathieu <42@dmathieu.com>
86 lines
2.6 KiB
Go
86 lines
2.6 KiB
Go
// 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")
|
|
}
|
|
})
|
|
}
|