Skip to content

Commit

Permalink
Avoid getting a new Tracer for ever RPC
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Oct 5, 2022
1 parent 4e0d97e commit f258c1c
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go
Expand Up @@ -66,6 +66,11 @@ var (
// for use in a grpc.Dial call.
func UnaryClientInterceptor(opts ...Option) grpc.UnaryClientInterceptor {
cfg := newConfig(opts)
tracer := cfg.TracerProvider.Tracer(
instrumentationName,
trace.WithInstrumentationVersion(SemVersion()),
)

return func(
ctx context.Context,
method string,
Expand All @@ -85,11 +90,6 @@ func UnaryClientInterceptor(opts ...Option) grpc.UnaryClientInterceptor {
requestMetadata, _ := metadata.FromOutgoingContext(ctx)
metadataCopy := requestMetadata.Copy()

tracer := cfg.TracerProvider.Tracer(
instrumentationName,
trace.WithInstrumentationVersion(SemVersion()),
)

name, attr := spanInfo(method, cc.Target())
var span trace.Span
ctx, span = tracer.Start(
Expand Down Expand Up @@ -245,6 +245,11 @@ func (w *clientStream) sendStreamEvent(eventType streamEventType, err error) {
// for use in a grpc.Dial call.
func StreamClientInterceptor(opts ...Option) grpc.StreamClientInterceptor {
cfg := newConfig(opts)
tracer := cfg.TracerProvider.Tracer(
instrumentationName,
trace.WithInstrumentationVersion(SemVersion()),
)

return func(
ctx context.Context,
desc *grpc.StreamDesc,
Expand All @@ -264,11 +269,6 @@ func StreamClientInterceptor(opts ...Option) grpc.StreamClientInterceptor {
requestMetadata, _ := metadata.FromOutgoingContext(ctx)
metadataCopy := requestMetadata.Copy()

tracer := cfg.TracerProvider.Tracer(
instrumentationName,
trace.WithInstrumentationVersion(SemVersion()),
)

name, attr := spanInfo(method, cc.Target())
var span trace.Span
ctx, span = tracer.Start(
Expand Down Expand Up @@ -313,6 +313,11 @@ func StreamClientInterceptor(opts ...Option) grpc.StreamClientInterceptor {
// for use in a grpc.NewServer call.
func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
cfg := newConfig(opts)
tracer := cfg.TracerProvider.Tracer(
instrumentationName,
trace.WithInstrumentationVersion(SemVersion()),
)

return func(
ctx context.Context,
req interface{},
Expand All @@ -333,11 +338,6 @@ func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
bags, spanCtx := Extract(ctx, &metadataCopy, opts...)
ctx = baggage.ContextWithBaggage(ctx, bags)

tracer := cfg.TracerProvider.Tracer(
instrumentationName,
trace.WithInstrumentationVersion(SemVersion()),
)

name, attr := spanInfo(info.FullMethod, peerFromCtx(ctx))
ctx, span := tracer.Start(
trace.ContextWithRemoteSpanContext(ctx, spanCtx),
Expand Down Expand Up @@ -409,6 +409,11 @@ func wrapServerStream(ctx context.Context, ss grpc.ServerStream) *serverStream {
// for use in a grpc.NewServer call.
func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
cfg := newConfig(opts)
tracer := cfg.TracerProvider.Tracer(
instrumentationName,
trace.WithInstrumentationVersion(SemVersion()),
)

return func(
srv interface{},
ss grpc.ServerStream,
Expand All @@ -430,11 +435,6 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
bags, spanCtx := Extract(ctx, &metadataCopy, opts...)
ctx = baggage.ContextWithBaggage(ctx, bags)

tracer := cfg.TracerProvider.Tracer(
instrumentationName,
trace.WithInstrumentationVersion(SemVersion()),
)

name, attr := spanInfo(info.FullMethod, peerFromCtx(ctx))
ctx, span := tracer.Start(
trace.ContextWithRemoteSpanContext(ctx, spanCtx),
Expand Down

0 comments on commit f258c1c

Please sign in to comment.