2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-09-25 08:12:22 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-11-04 19:10:58 +02:00
|
|
|
package opentracing // import "go.opentelemetry.io/otel/bridge/opentracing"
|
2019-09-25 08:12:22 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-12-09 23:03:11 +02:00
|
|
|
"go.opentelemetry.io/otel/bridge/opentracing/migration"
|
2021-11-13 18:35:04 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2019-09-25 08:12:22 +02:00
|
|
|
)
|
|
|
|
|
2020-09-24 00:16:13 +02:00
|
|
|
type WrapperTracerProvider struct {
|
2019-10-22 22:19:11 +02:00
|
|
|
wTracer *WrapperTracer
|
|
|
|
}
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
var _ trace.TracerProvider = (*WrapperTracerProvider)(nil)
|
2019-10-22 22:19:11 +02:00
|
|
|
|
2020-09-24 00:16:13 +02:00
|
|
|
// Tracer returns the WrapperTracer associated with the WrapperTracerProvider.
|
2020-11-07 00:13:31 +02:00
|
|
|
func (p *WrapperTracerProvider) Tracer(_ string, _ ...trace.TracerOption) trace.Tracer {
|
2019-10-22 22:19:11 +02:00
|
|
|
return p.wTracer
|
|
|
|
}
|
|
|
|
|
2020-09-24 00:16:13 +02:00
|
|
|
// NewWrappedTracerProvider creates a new trace provider that creates a single
|
2019-10-22 22:19:11 +02:00
|
|
|
// instance of WrapperTracer that wraps OpenTelemetry tracer.
|
2020-11-07 00:13:31 +02:00
|
|
|
func NewWrappedTracerProvider(bridge *BridgeTracer, tracer trace.Tracer) *WrapperTracerProvider {
|
2020-09-24 00:16:13 +02:00
|
|
|
return &WrapperTracerProvider{
|
2019-10-22 22:19:11 +02:00
|
|
|
wTracer: NewWrapperTracer(bridge, tracer),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 08:12:22 +02:00
|
|
|
// 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
|
2020-11-07 00:13:31 +02:00
|
|
|
tracer trace.Tracer
|
2019-09-25 08:12:22 +02:00
|
|
|
}
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
var _ trace.Tracer = &WrapperTracer{}
|
2019-09-25 08:12:22 +02:00
|
|
|
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.
|
2020-11-07 00:13:31 +02:00
|
|
|
func NewWrapperTracer(bridge *BridgeTracer, tracer trace.Tracer) *WrapperTracer {
|
2019-09-25 08:12:22 +02:00
|
|
|
return &WrapperTracer{
|
|
|
|
bridge: bridge,
|
|
|
|
tracer: tracer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
func (t *WrapperTracer) otelTracer() trace.Tracer {
|
2019-09-25 08:12:22 +02:00
|
|
|
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.
|
2021-05-27 16:53:56 +02:00
|
|
|
func (t *WrapperTracer) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
|
2019-09-25 08:12:22 +02:00
|
|
|
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.
|
2020-11-07 00:13:31 +02:00
|
|
|
func (t *WrapperTracer) DeferredContextSetupHook(ctx context.Context, span trace.Span) context.Context {
|
2019-09-25 08:12:22 +02:00
|
|
|
if tracerWithExtension, ok := t.otelTracer().(migration.DeferredContextSetupTracerExtension); ok {
|
|
|
|
ctx = tracerWithExtension.DeferredContextSetupHook(ctx, span)
|
|
|
|
}
|
2020-11-07 00:13:31 +02:00
|
|
|
ctx = trace.ContextWithSpan(ctx, span)
|
2019-09-25 08:12:22 +02:00
|
|
|
return ctx
|
|
|
|
}
|