Skip to content

Commit 775150f

Browse files
stats/opentelemetry: use TextMapProvider and TracerProvider from TraceOptions instead of otel global (#8166)
1 parent d860daa commit 775150f

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

stats/opentelemetry/client_tracing.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,24 @@ import (
2020
"context"
2121
"strings"
2222

23-
"go.opentelemetry.io/otel"
2423
"go.opentelemetry.io/otel/trace"
24+
"google.golang.org/grpc"
2525
otelinternaltracing "google.golang.org/grpc/stats/opentelemetry/internal/tracing"
2626
)
2727

28+
const tracerName = "grpc-go"
29+
2830
// traceTagRPC populates provided context with a new span using the
2931
// TextMapPropagator supplied in trace options and internal itracing.carrier.
3032
// It creates a new outgoing carrier which serializes information about this
3133
// span into gRPC Metadata, if TextMapPropagator is provided in the trace
3234
// options. if TextMapPropagator is not provided, it returns the context as is.
3335
func (h *clientStatsHandler) traceTagRPC(ctx context.Context, ai *attemptInfo) (context.Context, *attemptInfo) {
3436
mn := "Attempt." + strings.Replace(ai.method, "/", ".", -1)
35-
tracer := otel.Tracer("grpc-open-telemetry")
37+
tracer := h.options.TraceOptions.TracerProvider.Tracer(tracerName, trace.WithInstrumentationVersion(grpc.Version))
3638
ctx, span := tracer.Start(ctx, mn)
3739
carrier := otelinternaltracing.NewOutgoingCarrier(ctx)
38-
otel.GetTextMapPropagator().Inject(ctx, carrier)
40+
h.options.TraceOptions.TextMapPropagator.Inject(ctx, carrier)
3941
ai.traceSpan = span
4042
return carrier.Context(), ai
4143
}
@@ -48,7 +50,7 @@ func (h *clientStatsHandler) createCallTraceSpan(ctx context.Context, method str
4850
return ctx, nil
4951
}
5052
mn := strings.Replace(removeLeadingSlash(method), "/", ".", -1)
51-
tracer := otel.Tracer("grpc-open-telemetry")
53+
tracer := h.options.TraceOptions.TracerProvider.Tracer(tracerName, trace.WithInstrumentationVersion(grpc.Version))
5254
ctx, span := tracer.Start(ctx, mn, trace.WithSpanKind(trace.SpanKindClient))
5355
return ctx, span
5456
}

stats/opentelemetry/e2e_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"testing"
2525
"time"
2626

27-
"go.opentelemetry.io/otel"
2827
otelcodes "go.opentelemetry.io/otel/codes"
2928
oteltrace "go.opentelemetry.io/otel/trace"
3029

@@ -110,8 +109,6 @@ func defaultTraceOptions(_ *testing.T) (*experimental.TraceOptions, *tracetest.I
110109
spanProcessor := trace.NewSimpleSpanProcessor(spanExporter)
111110
tracerProvider := trace.NewTracerProvider(trace.WithSpanProcessor(spanProcessor))
112111
textMapPropagator := propagation.NewCompositeTextMapPropagator(opentelemetry.GRPCTraceBinPropagator{})
113-
otel.SetTextMapPropagator(textMapPropagator)
114-
otel.SetTracerProvider(tracerProvider)
115112
traceOptions := &experimental.TraceOptions{
116113
TracerProvider: tracerProvider,
117114
TextMapPropagator: textMapPropagator,

stats/opentelemetry/server_tracing.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"context"
2121
"strings"
2222

23-
"go.opentelemetry.io/otel"
2423
"go.opentelemetry.io/otel/trace"
24+
"google.golang.org/grpc"
2525
otelinternaltracing "google.golang.org/grpc/stats/opentelemetry/internal/tracing"
2626
)
2727

@@ -35,8 +35,8 @@ import (
3535
func (h *serverStatsHandler) traceTagRPC(ctx context.Context, ai *attemptInfo) (context.Context, *attemptInfo) {
3636
mn := strings.Replace(ai.method, "/", ".", -1)
3737
var span trace.Span
38-
tracer := otel.Tracer("grpc-open-telemetry")
39-
ctx = otel.GetTextMapPropagator().Extract(ctx, otelinternaltracing.NewIncomingCarrier(ctx))
38+
tracer := h.options.TraceOptions.TracerProvider.Tracer(tracerName, trace.WithInstrumentationVersion(grpc.Version))
39+
ctx = h.options.TraceOptions.TextMapPropagator.Extract(ctx, otelinternaltracing.NewIncomingCarrier(ctx))
4040
// If the context.Context provided in `ctx` to tracer.Start(), contains a
4141
// span then the newly-created Span will be a child of that span,
4242
// otherwise it will be a root span.

0 commit comments

Comments
 (0)