Skip to content

Commit 3235f89

Browse files
committed
tracer autodetection for client interceptors
Ability to automatically get tracer from request context if no tracer were passed to constructor.
1 parent cfaf568 commit 3235f89

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

tracing/opentracing/client_interceptors.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func UnaryClientInterceptor(opts ...Option) grpc.UnaryClientInterceptor {
2525
return invoker(parentCtx, method, req, reply, cc, opts...)
2626
}
2727
newCtx, clientSpan := newClientSpanFromContext(parentCtx, o.tracer, method)
28+
if clientSpan == nil {
29+
return invoker(parentCtx, method, req, reply, cc, opts...)
30+
}
2831
err := invoker(newCtx, method, req, reply, cc, opts...)
2932
finishClientSpan(clientSpan, err)
3033
return err
@@ -39,6 +42,9 @@ func StreamClientInterceptor(opts ...Option) grpc.StreamClientInterceptor {
3942
return streamer(parentCtx, desc, cc, method, opts...)
4043
}
4144
newCtx, clientSpan := newClientSpanFromContext(parentCtx, o.tracer, method)
45+
if clientSpan == nil {
46+
return streamer(parentCtx, desc, cc, method, opts...)
47+
}
4248
clientStream, err := streamer(newCtx, desc, cc, method, opts...)
4349
if err != nil {
4450
finishClientSpan(clientSpan, err)
@@ -112,6 +118,15 @@ func newClientSpanFromContext(ctx context.Context, tracer opentracing.Tracer, fu
112118
var parentSpanCtx opentracing.SpanContext
113119
if parent := opentracing.SpanFromContext(ctx); parent != nil {
114120
parentSpanCtx = parent.Context()
121+
if tracer == nil {
122+
tracer = parent.Tracer()
123+
}
124+
}
125+
if tracer == nil {
126+
tracer = opentracing.GlobalTracer()
127+
if _, ok := tracer.(opentracing.NoopTracer); ok {
128+
return ctx, nil
129+
}
115130
}
116131
opts := []opentracing.StartSpanOption{
117132
opentracing.ChildOf(parentSpanCtx),

tracing/opentracing/interceptors_test.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,19 @@ func TestTaggingSuite(t *testing.T) {
8282
}
8383
s := &OpentracingSuite{
8484
mockTracer: mockTracer,
85-
InterceptorTestSuite: makeInterceptorTestSuite(t, opts),
85+
InterceptorTestSuite: makeInterceptorTestSuite(t, opts, opts),
86+
}
87+
suite.Run(t, s)
88+
}
89+
90+
func TestTaggingSuite2(t *testing.T) {
91+
mockTracer := mocktracer.New()
92+
opts := []grpc_opentracing.Option{
93+
grpc_opentracing.WithTracer(mockTracer),
94+
}
95+
s := &OpentracingSuite{
96+
mockTracer: mockTracer,
97+
InterceptorTestSuite: makeInterceptorTestSuite(t, nil, opts),
8698
}
8799
suite.Run(t, s)
88100
}
@@ -96,26 +108,26 @@ func TestTaggingSuiteJaeger(t *testing.T) {
96108
}
97109
s := &OpentracingSuite{
98110
mockTracer: mockTracer,
99-
InterceptorTestSuite: makeInterceptorTestSuite(t, opts),
111+
InterceptorTestSuite: makeInterceptorTestSuite(t, opts, opts),
100112
}
101113
suite.Run(t, s)
102114
}
103115

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

106118
return &grpc_testing.InterceptorTestSuite{
107119
TestService: &tracingAssertService{TestServiceServer: &grpc_testing.TestPingService{T: t}, T: t},
108120
ClientOpts: []grpc.DialOption{
109-
grpc.WithUnaryInterceptor(grpc_opentracing.UnaryClientInterceptor(opts...)),
110-
grpc.WithStreamInterceptor(grpc_opentracing.StreamClientInterceptor(opts...)),
121+
grpc.WithUnaryInterceptor(grpc_opentracing.UnaryClientInterceptor(clientOpts...)),
122+
grpc.WithStreamInterceptor(grpc_opentracing.StreamClientInterceptor(clientOpts...)),
111123
},
112124
ServerOpts: []grpc.ServerOption{
113125
grpc_middleware.WithStreamServerChain(
114126
grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
115-
grpc_opentracing.StreamServerInterceptor(opts...)),
127+
grpc_opentracing.StreamServerInterceptor(serverOpts...)),
116128
grpc_middleware.WithUnaryServerChain(
117129
grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
118-
grpc_opentracing.UnaryServerInterceptor(opts...)),
130+
grpc_opentracing.UnaryServerInterceptor(serverOpts...)),
119131
},
120132
}
121133
}

tracing/opentracing/options.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ func evaluateOptions(opts []Option) *options {
3232
for _, o := range opts {
3333
o(optCopy)
3434
}
35-
if optCopy.tracer == nil {
36-
optCopy.tracer = opentracing.GlobalTracer()
37-
}
3835
return optCopy
3936
}
4037

@@ -50,6 +47,10 @@ func WithFilterFunc(f FilterFunc) Option {
5047
// WithTracer sets a custom tracer to be used for this middleware, otherwise the opentracing.GlobalTracer is used.
5148
func WithTracer(tracer opentracing.Tracer) Option {
5249
return func(o *options) {
53-
o.tracer = tracer
50+
if tracer == nil {
51+
o.tracer = opentracing.GlobalTracer()
52+
} else {
53+
o.tracer = tracer
54+
}
5455
}
5556
}

tracing/opentracing/server_interceptors.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ func newServerSpanFromInbound(ctx context.Context, tracer opentracing.Tracer, fu
5656
grpclog.Printf("grpc_opentracing: failed parsing trace information: %v", err)
5757
}
5858

59+
if tracer == nil {
60+
tracer = opentracing.GlobalTracer()
61+
}
62+
5963
serverSpan := tracer.StartSpan(
6064
fullMethodName,
6165
// this is magical, it attaches the new span to the parent parentSpanContext, and creates an unparented one if empty.

0 commit comments

Comments
 (0)