diff --git a/pkg/tools/tracing/grpcoptions.go b/pkg/tools/tracing/grpcoptions.go index 01a6e5436..d765d2ab4 100644 --- a/pkg/tools/tracing/grpcoptions.go +++ b/pkg/tools/tracing/grpcoptions.go @@ -20,60 +20,26 @@ package tracing import ( - "context" - "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" - "go.opentelemetry.io/otel" "google.golang.org/grpc" - "google.golang.org/protobuf/proto" "github.com/networkservicemesh/sdk/pkg/tools/opentelemetry" ) // WithTracing - returns array of grpc.ServerOption that should be passed to grpc.Dial to enable opentelemetry tracing func WithTracing() []grpc.ServerOption { + opts := []grpc.ServerOption{} if opentelemetry.IsEnabled() { - interceptor := func( - ctx context.Context, - req interface{}, - info *grpc.UnaryServerInfo, - handler grpc.UnaryHandler, - ) (resp interface{}, err error) { - return otelgrpc.UnaryServerInterceptor(otelgrpc.WithTracerProvider(otel.GetTracerProvider()))(ctx, proto.Clone(req.(proto.Message)), info, handler) - } - return []grpc.ServerOption{ - grpc.ChainUnaryInterceptor( - interceptor), - grpc.ChainStreamInterceptor( - otelgrpc.StreamServerInterceptor(otelgrpc.WithTracerProvider(otel.GetTracerProvider()))), - } - } - return []grpc.ServerOption{ - grpc.EmptyServerOption{}, + opts = append(opts, grpc.StatsHandler(otelgrpc.NewServerHandler())) } + return opts } // WithTracingDial returns array of grpc.DialOption that should be passed to grpc.Dial to enable opentelemetry tracing func WithTracingDial() []grpc.DialOption { + opts := []grpc.DialOption{} if opentelemetry.IsEnabled() { - interceptor := func( - ctx context.Context, - method string, - req, reply interface{}, - cc *grpc.ClientConn, - invoker grpc.UnaryInvoker, - opts ...grpc.CallOption, - ) error { - return otelgrpc.UnaryClientInterceptor(otelgrpc.WithTracerProvider(otel.GetTracerProvider()))(ctx, method, proto.Clone(req.(proto.Message)), reply, cc, invoker, opts...) - } - return []grpc.DialOption{ - grpc.WithChainUnaryInterceptor( - interceptor), - grpc.WithChainStreamInterceptor( - otelgrpc.StreamClientInterceptor(otelgrpc.WithTracerProvider(otel.GetTracerProvider()))), - } - } - return []grpc.DialOption{ - grpc.EmptyDialOption{}, + opts = append(opts, grpc.WithStatsHandler(otelgrpc.NewClientHandler())) } + return opts }