Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tracing/opentracing/client_interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func UnaryClientInterceptor(opts ...Option) grpc.UnaryClientInterceptor {
return invoker(parentCtx, method, req, reply, cc, opts...)
}
newCtx, clientSpan := newClientSpanFromContext(parentCtx, o.tracer, method)
if clientSpan == nil {
return invoker(parentCtx, method, req, reply, cc, opts...)
}
err := invoker(newCtx, method, req, reply, cc, opts...)
finishClientSpan(clientSpan, err)
return err
Expand All @@ -39,6 +42,9 @@ func StreamClientInterceptor(opts ...Option) grpc.StreamClientInterceptor {
return streamer(parentCtx, desc, cc, method, opts...)
}
newCtx, clientSpan := newClientSpanFromContext(parentCtx, o.tracer, method)
if clientSpan == nil {
return streamer(parentCtx, desc, cc, method, opts...)
}
clientStream, err := streamer(newCtx, desc, cc, method, opts...)
if err != nil {
finishClientSpan(clientSpan, err)
Expand Down Expand Up @@ -112,6 +118,15 @@ func newClientSpanFromContext(ctx context.Context, tracer opentracing.Tracer, fu
var parentSpanCtx opentracing.SpanContext
if parent := opentracing.SpanFromContext(ctx); parent != nil {
parentSpanCtx = parent.Context()
if tracer == nil {
tracer = parent.Tracer()
}
}
if tracer == nil {
tracer = opentracing.GlobalTracer()
if _, ok := tracer.(opentracing.NoopTracer); ok {
return ctx, nil
}
}
opts := []opentracing.StartSpanOption{
opentracing.ChildOf(parentSpanCtx),
Expand Down
26 changes: 19 additions & 7 deletions tracing/opentracing/interceptors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,19 @@ func TestTaggingSuite(t *testing.T) {
}
s := &OpentracingSuite{
mockTracer: mockTracer,
InterceptorTestSuite: makeInterceptorTestSuite(t, opts),
InterceptorTestSuite: makeInterceptorTestSuite(t, opts, opts),
}
suite.Run(t, s)
}

func TestTaggingSuite2(t *testing.T) {
mockTracer := mocktracer.New()
opts := []grpc_opentracing.Option{
grpc_opentracing.WithTracer(mockTracer),
}
s := &OpentracingSuite{
mockTracer: mockTracer,
InterceptorTestSuite: makeInterceptorTestSuite(t, nil, opts),
}
suite.Run(t, s)
}
Expand All @@ -96,26 +108,26 @@ func TestTaggingSuiteJaeger(t *testing.T) {
}
s := &OpentracingSuite{
mockTracer: mockTracer,
InterceptorTestSuite: makeInterceptorTestSuite(t, opts),
InterceptorTestSuite: makeInterceptorTestSuite(t, opts, opts),
}
suite.Run(t, s)
}

func makeInterceptorTestSuite(t *testing.T, opts []grpc_opentracing.Option) *grpc_testing.InterceptorTestSuite {
func makeInterceptorTestSuite(t *testing.T, clientOpts, serverOpts []grpc_opentracing.Option) *grpc_testing.InterceptorTestSuite {

return &grpc_testing.InterceptorTestSuite{
TestService: &tracingAssertService{TestServiceServer: &grpc_testing.TestPingService{T: t}, T: t},
ClientOpts: []grpc.DialOption{
grpc.WithUnaryInterceptor(grpc_opentracing.UnaryClientInterceptor(opts...)),
grpc.WithStreamInterceptor(grpc_opentracing.StreamClientInterceptor(opts...)),
grpc.WithUnaryInterceptor(grpc_opentracing.UnaryClientInterceptor(clientOpts...)),
grpc.WithStreamInterceptor(grpc_opentracing.StreamClientInterceptor(clientOpts...)),
},
ServerOpts: []grpc.ServerOption{
grpc_middleware.WithStreamServerChain(
grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_opentracing.StreamServerInterceptor(opts...)),
grpc_opentracing.StreamServerInterceptor(serverOpts...)),
grpc_middleware.WithUnaryServerChain(
grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_opentracing.UnaryServerInterceptor(opts...)),
grpc_opentracing.UnaryServerInterceptor(serverOpts...)),
},
}
}
Expand Down
9 changes: 5 additions & 4 deletions tracing/opentracing/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ func evaluateOptions(opts []Option) *options {
for _, o := range opts {
o(optCopy)
}
if optCopy.tracer == nil {
optCopy.tracer = opentracing.GlobalTracer()
}
return optCopy
}

Expand All @@ -50,6 +47,10 @@ func WithFilterFunc(f FilterFunc) Option {
// WithTracer sets a custom tracer to be used for this middleware, otherwise the opentracing.GlobalTracer is used.
func WithTracer(tracer opentracing.Tracer) Option {
return func(o *options) {
o.tracer = tracer
if tracer == nil {
o.tracer = opentracing.GlobalTracer()
} else {
o.tracer = tracer
}
}
}
4 changes: 4 additions & 0 deletions tracing/opentracing/server_interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func newServerSpanFromInbound(ctx context.Context, tracer opentracing.Tracer, fu
grpclog.Printf("grpc_opentracing: failed parsing trace information: %v", err)
}

if tracer == nil {
tracer = opentracing.GlobalTracer()
}

serverSpan := tracer.StartSpan(
fullMethodName,
// this is magical, it attaches the new span to the parent parentSpanContext, and creates an unparented one if empty.
Expand Down