From 1cb5bc4b595dcb2f6671dbec6055c6fb8d5a9065 Mon Sep 17 00:00:00 2001 From: Robert Lin Date: Thu, 23 Feb 2023 11:07:39 -0800 Subject: [PATCH] internal/tracer: correctly set Tracer names in OpenTracing bridge (#40945) The existing wrapped OpenTelemetry TracerProvider for bridging OpenTracing API calls to OpenTelemetryAPI calls discards the name provided to the `Tracer()` constructor on it, since it uses a fixed tracer that we provide it. This change leverages an upstream patch, https://github.com/open-telemetry/opentelemetry-go/pull/3116 (dependency update: https://github.com/sourcegraph/sourcegraph/pull/47126), that allows wrapped tracers to be created on the fly with the provided parameters, so that we can more accurately see the instrumentation source. ## Test plan Via the bridge, we can see that the Tracer used is the bridge tracer: image Via direct usage of OpenTelemetry, we can see that the Tracer used has the custom name set: image --- internal/trace/tracer.go | 2 +- internal/tracer/tracer.go | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/internal/trace/tracer.go b/internal/trace/tracer.go index d915ecf9f7f11..76d80f04cf2aa 100644 --- a/internal/trace/tracer.go +++ b/internal/trace/tracer.go @@ -24,7 +24,7 @@ func (t Tracer) New(ctx context.Context, family, title string, tags ...Tag) (*Tr var otelSpan oteltrace.Span ctx, otelSpan = t.TracerProvider. - Tracer("internal/trace"). + Tracer("sourcegraph/internal/trace"). Start(ctx, family, oteltrace.WithAttributes(attribute.String("title", title)), oteltrace.WithAttributes(tagSet(tags).toAttributes()...)) diff --git a/internal/tracer/tracer.go b/internal/tracer/tracer.go index 7595fb714a8c4..27f31ee800950 100644 --- a/internal/tracer/tracer.go +++ b/internal/tracer/tracer.go @@ -130,12 +130,11 @@ func newBridgeTracers(logger log.Logger, provider *oteltracesdk.TracerProvider, // Set up otBridgeTracer for converting OpenTracing API calls to OpenTelemetry, and // otelTracerProvider for the inverse. - // - // TODO: Unfortunately, this wrapped tracer provider discards the name provided to - // the Tracer() constructor on it, since it uses a fixed tracer that we provide it. - // This is implemented in https://github.com/sourcegraph/sourcegraph/pull/40945 - otBridgeTracer, otelTracerProvider := otelbridge.NewTracerPair(provider.Tracer("internal/tracer/otel")) + otBridgeTracer := otelbridge.NewBridgeTracer() otBridgeTracer.SetTextMapPropagator(compositePropagator) + otelTracerProvider := otelbridge.NewTracerProvider(otBridgeTracer, provider) + otBridgeTracer.SetOpenTelemetryTracer( + otelTracerProvider.Tracer("sourcegraph/internal/tracer.opentracing-bridge")) // Set up logging otelLogger := logger.AddCallerSkip(2).Scoped("otel", "OpenTelemetry library")