Skip to content

Commit

Permalink
Add BridgeOpenTracingToOtel, to wrap spans for Otel code
Browse files Browse the repository at this point in the history
  • Loading branch information
bboreham committed May 31, 2024
1 parent 5706b43 commit 1566732
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/util/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"github.com/uber/jaeger-client-go"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
Expand All @@ -30,6 +31,21 @@ func NewOpenTelemetryProviderBridge(tracer opentracing.Tracer) *OpenTelemetryPro
}
}

// BridgeOpenTracingToOtel wraps ctx so that Otel-using code can link to an OpenTracing span.
func BridgeOpenTracingToOtel(ctx context.Context) context.Context {
span := opentracing.SpanFromContext(ctx)
if span == nil {
return ctx
}
tracerBridge, ok := otel.Tracer("").(*OpenTelemetryTracerBridge)
if !ok {
return ctx

}
ctx, _ = tracerBridge.openTracingToOtel(ctx, span)
return ctx
}

// Tracer creates an implementation of the Tracer interface.
// The instrumentationName must be the name of the library providing
// instrumentation. This name may be the same as the instrumented code
Expand Down Expand Up @@ -117,6 +133,10 @@ func (t *OpenTelemetryTracerBridge) Start(ctx context.Context, spanName string,
}

span, ctx := opentracing.StartSpanFromContextWithTracer(ctx, t.tracer, spanName, mappedOptions...)
return t.openTracingToOtel(ctx, span)
}

func (t *OpenTelemetryTracerBridge) openTracingToOtel(ctx context.Context, span opentracing.Span) (context.Context, trace.Span) {
otelSpan := NewOpenTelemetrySpanBridge(span, t.provider)
return trace.ContextWithSpan(ctx, otelSpan), otelSpan
}
Expand Down

0 comments on commit 1566732

Please sign in to comment.