@@ -13,6 +13,7 @@ import (
13
13
"github.com/cockroachdb/cockroach/pkg/util/ctxutil"
14
14
"github.com/cockroachdb/cockroach/pkg/util/grpcutil"
15
15
"github.com/cockroachdb/cockroach/pkg/util/tracing"
16
+ "github.com/cockroachdb/cockroach/pkg/util/tracing/tracingutil"
16
17
"github.com/cockroachdb/errors"
17
18
"go.opentelemetry.io/otel/attribute"
18
19
"go.opentelemetry.io/otel/codes"
@@ -33,43 +34,6 @@ func ExtractSpanMetaFromGRPCCtx(
33
34
return tracer .ExtractMetaFrom (tracing.MetadataCarrier {MD : md })
34
35
}
35
36
36
- // setGRPCErrorTag sets an error tag on the span.
37
- func setGRPCErrorTag (sp * tracing.Span , err error ) {
38
- if err == nil {
39
- return
40
- }
41
- s , _ := status .FromError (err )
42
- sp .SetTag ("response_code" , attribute .IntValue (int (codes .Error )))
43
- sp .SetOtelStatus (codes .Error , s .Message ())
44
- }
45
-
46
- // BatchMethodName is the method name of Internal.Batch RPC.
47
- const BatchMethodName = "/cockroach.roachpb.Internal/Batch"
48
-
49
- // BatchStreamMethodName is the method name of the Internal.BatchStream RPC.
50
- const BatchStreamMethodName = "/cockroach.roachpb.Internal/BatchStream"
51
-
52
- // sendKVBatchMethodName is the method name for adminServer.SendKVBatch.
53
- const sendKVBatchMethodName = "/cockroach.server.serverpb.Admin/SendKVBatch"
54
-
55
- // SetupFlowMethodName is the method name of DistSQL.SetupFlow RPC.
56
- const SetupFlowMethodName = "/cockroach.sql.distsqlrun.DistSQL/SetupFlow"
57
- const flowStreamMethodName = "/cockroach.sql.distsqlrun.DistSQL/FlowStream"
58
-
59
- // methodExcludedFromTracing returns true if a call to the given RPC method does
60
- // not need to propagate tracing info. Some RPCs (Internal.Batch,
61
- // DistSQL.SetupFlow) have dedicated fields for passing along the tracing
62
- // context in the request, which is more efficient than letting the RPC
63
- // interceptors deal with it. Others (DistSQL.FlowStream) are simply exempt from
64
- // tracing because it's not worth it.
65
- func methodExcludedFromTracing (method string ) bool {
66
- return method == BatchMethodName ||
67
- method == BatchStreamMethodName ||
68
- method == sendKVBatchMethodName ||
69
- method == SetupFlowMethodName ||
70
- method == flowStreamMethodName
71
- }
72
-
73
37
// ServerInterceptor returns a grpc.UnaryServerInterceptor suitable
74
38
// for use in a grpc.NewServer call.
75
39
//
@@ -92,7 +56,7 @@ func ServerInterceptor(tracer *tracing.Tracer) grpc.UnaryServerInterceptor {
92
56
info * grpc.UnaryServerInfo ,
93
57
handler grpc.UnaryHandler ,
94
58
) (interface {}, error ) {
95
- if methodExcludedFromTracing (info .FullMethod ) {
59
+ if tracingutil . MethodExcludedFromTracing (info .FullMethod ) {
96
60
return handler (ctx , req )
97
61
}
98
62
@@ -139,7 +103,7 @@ func ServerInterceptor(tracer *tracing.Tracer) grpc.UnaryServerInterceptor {
139
103
// application-specific gRPC handler(s) to access.
140
104
func StreamServerInterceptor (tracer * tracing.Tracer ) grpc.StreamServerInterceptor {
141
105
return func (srv interface {}, ss grpc.ServerStream , info * grpc.StreamServerInfo , handler grpc.StreamHandler ) error {
142
- if methodExcludedFromTracing (info .FullMethod ) {
106
+ if tracingutil . MethodExcludedFromTracing (info .FullMethod ) {
143
107
return handler (srv , ss )
144
108
}
145
109
spanMeta , err := ExtractSpanMetaFromGRPCCtx (ss .Context (), tracer )
@@ -188,19 +152,6 @@ func (ss *tracingServerStream) Context() context.Context {
188
152
return ss .ctx
189
153
}
190
154
191
- func injectSpanMeta (
192
- ctx context.Context , tracer * tracing.Tracer , clientSpan * tracing.Span ,
193
- ) context.Context {
194
- md , ok := metadata .FromOutgoingContext (ctx )
195
- if ! ok {
196
- md = metadata .New (nil )
197
- } else {
198
- md = md .Copy ()
199
- }
200
- tracer .InjectMetaInto (clientSpan .Meta (), tracing.MetadataCarrier {MD : md })
201
- return metadata .NewOutgoingContext (ctx , md )
202
- }
203
-
204
155
// ClientInterceptor returns a grpc.UnaryClientInterceptor suitable
205
156
// for use in a grpc.Dial call.
206
157
//
@@ -229,17 +180,13 @@ func ClientInterceptor(
229
180
invoker grpc.UnaryInvoker ,
230
181
opts ... grpc.CallOption ,
231
182
) error {
232
- // Local RPCs don't need any special tracing, since the caller's context
233
- // will be used on the "server".
234
- _ , localRequest := grpcutil .IsLocalRequestContext (ctx )
235
- if localRequest {
236
- return invoker (ctx , method , req , resp , cc , opts ... )
237
- }
238
- parent := tracing .SpanFromContext (ctx )
239
- if ! tracing .SpanInclusionFuncForClient (parent ) {
183
+ skipTracing := tracingutil .ShouldSkipClientTracing (ctx )
184
+ if skipTracing {
240
185
return invoker (ctx , method , req , resp , cc , opts ... )
241
186
}
242
187
188
+ // Create clientSpan here after determining that we shouldn't skip tracing
189
+ parent := tracing .SpanFromContext (ctx )
243
190
clientSpan := tracer .StartSpan (
244
191
method ,
245
192
tracing .WithParent (parent ),
@@ -248,11 +195,11 @@ func ClientInterceptor(
248
195
init (clientSpan )
249
196
defer clientSpan .Finish ()
250
197
251
- // For most RPCs we pass along tracing info as gRPC metadata. Some select
252
- // RPCs carry the tracing in the request protos, which is more efficient.
253
- if ! methodExcludedFromTracing (method ) {
254
- ctx = injectSpanMeta (ctx , tracer , clientSpan )
198
+ // For most RPCs we pass along tracing info as metadata
199
+ if ! tracingutil .MethodExcludedFromTracing (method ) {
200
+ ctx = tracingutil .InjectSpanMeta (ctx , tracer , clientSpan )
255
201
}
202
+
256
203
if invoker != nil {
257
204
err := invoker (ctx , method , req , resp , cc , opts ... )
258
205
if err != nil {
@@ -294,27 +241,23 @@ func StreamClientInterceptor(
294
241
streamer grpc.Streamer ,
295
242
opts ... grpc.CallOption ,
296
243
) (grpc.ClientStream , error ) {
297
- // Local RPCs don't need any special tracing, since the caller's context
298
- // will be used on the "server".
299
- _ , localRequest := grpcutil .IsLocalRequestContext (ctx )
300
- if localRequest {
301
- return streamer (ctx , desc , cc , method , opts ... )
302
- }
303
- parent := tracing .SpanFromContext (ctx )
304
- if ! tracing .SpanInclusionFuncForClient (parent ) {
244
+ skipTracing := tracingutil .ShouldSkipClientTracing (ctx )
245
+ if skipTracing {
305
246
return streamer (ctx , desc , cc , method , opts ... )
306
247
}
307
248
308
- // Create a span that will live for the life of the stream.
249
+ // Create clientSpan here after determining that we shouldn't skip tracing
250
+ parent := tracing .SpanFromContext (ctx )
309
251
clientSpan := tracer .StartSpan (
310
252
method ,
311
253
tracing .WithParent (parent ),
312
254
tracing .WithClientSpanKind ,
313
255
)
314
256
init (clientSpan )
315
257
316
- if ! methodExcludedFromTracing (method ) {
317
- ctx = injectSpanMeta (ctx , tracer , clientSpan )
258
+ // For most RPCs we pass along tracing info as metadata
259
+ if ! tracingutil .MethodExcludedFromTracing (method ) {
260
+ ctx = tracingutil .InjectSpanMeta (ctx , tracer , clientSpan )
318
261
}
319
262
320
263
cs , err := streamer (ctx , desc , cc , method , opts ... )
@@ -421,3 +364,13 @@ func (cs *tracingClientStream) CloseSend() error {
421
364
}
422
365
return errors .Wrap (err , "close send error" )
423
366
}
367
+
368
+ // setGRPCErrorTag sets an error tag on the span.
369
+ func setGRPCErrorTag (sp * tracing.Span , err error ) {
370
+ if err == nil {
371
+ return
372
+ }
373
+ s , _ := status .FromError (err )
374
+ sp .SetTag ("response_code" , attribute .IntValue (int (codes .Error )))
375
+ sp .SetOtelStatus (codes .Error , s .Message ())
376
+ }
0 commit comments