1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-12 10:04:29 +02:00
opentelemetry-go/bridge/opentracing/wrapper.go
Robert Lin 640a0cd8bc
bridge/opentracing: introduce NewTracerProvider that wraps TracerProvider instead of Tracer (#3116)
* 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>
2023-01-11 16:59:06 -08:00

105 lines
3.7 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 "go.opentelemetry.io/otel/bridge/opentracing"
import (
"context"
"go.opentelemetry.io/otel/bridge/opentracing/migration"
"go.opentelemetry.io/otel/trace"
)
// WrapperTracerProvider is an OpenTelemetry TracerProvider that wraps an
// OpenTracing Tracer, created by the deprecated NewWrappedTracerProvider.
//
// Deprecated: Use the TracerProvider from NewTracerProvider(...) instead.
type WrapperTracerProvider struct {
wTracer *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
}
// NewWrappedTracerProvider creates a new trace provider that creates a single
// instance of WrapperTracer that wraps OpenTelemetry tracer, and always returns
// it unmodified from Tracer().
//
// Deprecated: Use NewTracerProvider(...) instead.
func NewWrappedTracerProvider(bridge *BridgeTracer, tracer trace.Tracer) *WrapperTracerProvider {
return &WrapperTracerProvider{
wTracer: NewWrapperTracer(bridge, tracer),
}
}
// WrapperTracer is a wrapper around an OpenTelemetry tracer. It
// mostly forwards the calls to the wrapped tracer, but also does some
// extra steps like setting up a context with the active OpenTracing
// span.
//
// It does not need to be used when the OpenTelemetry tracer is also
// aware how to operate in environment where OpenTracing API is also
// used.
type WrapperTracer struct {
bridge *BridgeTracer
tracer trace.Tracer
}
var _ trace.Tracer = &WrapperTracer{}
var _ migration.DeferredContextSetupTracerExtension = &WrapperTracer{}
// NewWrapperTracer wraps the passed tracer and also talks to the
// passed bridge tracer when setting up the context with the new
// active OpenTracing span.
func NewWrapperTracer(bridge *BridgeTracer, tracer trace.Tracer) *WrapperTracer {
return &WrapperTracer{
bridge: bridge,
tracer: tracer,
}
}
func (t *WrapperTracer) otelTracer() trace.Tracer {
return t.tracer
}
// Start forwards the call to the wrapped tracer. It also tries to
// override the tracer of the returned span if the span implements the
// OverrideTracerSpanExtension interface.
func (t *WrapperTracer) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
ctx, span := t.otelTracer().Start(ctx, name, opts...)
if spanWithExtension, ok := span.(migration.OverrideTracerSpanExtension); ok {
spanWithExtension.OverrideTracer(t)
}
if !migration.SkipContextSetup(ctx) {
ctx = t.bridge.ContextWithBridgeSpan(ctx, span)
}
return ctx, span
}
// DeferredContextSetupHook is a part of the implementation of the
// DeferredContextSetupTracerExtension interface. It will try to
// forward the call to the wrapped tracer if it implements the
// interface.
func (t *WrapperTracer) DeferredContextSetupHook(ctx context.Context, span trace.Span) context.Context {
if tracerWithExtension, ok := t.otelTracer().(migration.DeferredContextSetupTracerExtension); ok {
ctx = tracerWithExtension.DeferredContextSetupHook(ctx, span)
}
ctx = trace.ContextWithSpan(ctx, span)
return ctx
}