Skip to content

Commit

Permalink
Avoid getting a new Tracer for every 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 6, 2022
1 parent 167b53f commit c8a5eda
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]


### Changed

- google.golang.org/grpc/otelgrpc: Avoid getting a new Tracer for every RPC.

## [0.36.1]

### Changed
Expand Down
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 c8a5eda

Please sign in to comment.