-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
grpc_interceptor.go
412 lines (380 loc) · 12 KB
/
grpc_interceptor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package grpcinterceptor
import (
"context"
"io"
"sync/atomic"
"github.com/cockroachdb/cockroach/pkg/util/grpcutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/errors"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
// ExtractSpanMetaFromGRPCCtx retrieves a SpanMeta carried as gRPC metadata by
// an RPC.
func ExtractSpanMetaFromGRPCCtx(
ctx context.Context, tracer *tracing.Tracer,
) (tracing.SpanMeta, error) {
md, ok := grpcutil.FastFromIncomingContext(ctx)
if !ok {
return tracing.SpanMeta{}, nil
}
return tracer.ExtractMetaFrom(tracing.MetadataCarrier{MD: md})
}
// setGRPCErrorTag sets an error tag on the span.
func setGRPCErrorTag(sp *tracing.Span, err error) {
if err == nil {
return
}
s, _ := status.FromError(err)
sp.SetTag("response_code", attribute.IntValue(int(codes.Error)))
sp.SetOtelStatus(codes.Error, s.Message())
}
// BatchMethodName is the method name of Internal.Batch RPC.
const BatchMethodName = "/cockroach.roachpb.Internal/Batch"
// sendKVBatchMethodName is the method name for adminServer.SendKVBatch.
const sendKVBatchMethodName = "/cockroach.server.serverpb.Admin/SendKVBatch"
// SetupFlowMethodName is the method name of DistSQL.SetupFlow RPC.
const SetupFlowMethodName = "/cockroach.sql.distsqlrun.DistSQL/SetupFlow"
const flowStreamMethodName = "/cockroach.sql.distsqlrun.DistSQL/FlowStream"
// methodExcludedFromTracing returns true if a call to the given RPC method does
// not need to propagate tracing info. Some RPCs (Internal.Batch,
// DistSQL.SetupFlow) have dedicated fields for passing along the tracing
// context in the request, which is more efficient than letting the RPC
// interceptors deal with it. Others (DistSQL.FlowStream) are simply exempt from
// tracing because it's not worth it.
func methodExcludedFromTracing(method string) bool {
return method == BatchMethodName ||
method == sendKVBatchMethodName ||
method == SetupFlowMethodName ||
method == flowStreamMethodName
}
// ServerInterceptor returns a grpc.UnaryServerInterceptor suitable
// for use in a grpc.NewServer call.
//
// For example:
//
// s := grpcutil.NewServer(
// ..., // (existing ServerOptions)
// grpc.UnaryInterceptor(ServerInterceptor(tracer)))
//
// All gRPC server spans will look for an tracing SpanMeta in the gRPC
// metadata; if found, the server span will act as the ChildOf that RPC
// SpanMeta.
//
// Root or not, the server Span will be embedded in the context.Context for the
// application-specific gRPC handler(s) to access.
func ServerInterceptor(tracer *tracing.Tracer) grpc.UnaryServerInterceptor {
return func(
ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
if methodExcludedFromTracing(info.FullMethod) {
return handler(ctx, req)
}
spanMeta, err := ExtractSpanMetaFromGRPCCtx(ctx, tracer)
if err != nil {
return nil, err
}
if !tracing.SpanInclusionFuncForServer(tracer, spanMeta) {
return handler(ctx, req)
}
ctx, serverSpan := tracer.StartSpanCtx(
ctx,
info.FullMethod,
tracing.WithRemoteParentFromSpanMeta(spanMeta),
tracing.WithServerSpanKind,
)
defer serverSpan.Finish()
resp, err := handler(ctx, req)
if err != nil {
setGRPCErrorTag(serverSpan, err)
serverSpan.Recordf("error: %s", err)
}
return resp, err
}
}
// StreamServerInterceptor returns a grpc.StreamServerInterceptor suitable
// for use in a grpc.NewServer call. The interceptor instruments streaming RPCs by
// creating a single span to correspond to the lifetime of the RPC's stream.
//
// For example:
//
// s := grpcutil.NewServer(
// ..., // (existing ServerOptions)
// grpc.StreamInterceptor(StreamServerInterceptor(tracer)))
//
// All gRPC server spans will look for a SpanMeta in the gRPC
// metadata; if found, the server span will act as the ChildOf that RPC
// SpanMeta.
//
// Root or not, the server Span will be embedded in the context.Context for the
// application-specific gRPC handler(s) to access.
func StreamServerInterceptor(tracer *tracing.Tracer) grpc.StreamServerInterceptor {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if methodExcludedFromTracing(info.FullMethod) {
return handler(srv, ss)
}
spanMeta, err := ExtractSpanMetaFromGRPCCtx(ss.Context(), tracer)
if err != nil {
return err
}
if !tracing.SpanInclusionFuncForServer(tracer, spanMeta) {
return handler(srv, ss)
}
ctx, serverSpan := tracer.StartSpanCtx(
ss.Context(),
info.FullMethod,
tracing.WithRemoteParentFromSpanMeta(spanMeta),
tracing.WithServerSpanKind,
)
defer serverSpan.Finish()
ss = &tracingServerStream{
ServerStream: ss,
ctx: ctx,
}
err = handler(srv, ss)
if err != nil {
setGRPCErrorTag(serverSpan, err)
serverSpan.Recordf("error: %s", err)
}
return err
}
}
type tracingServerStream struct {
grpc.ServerStream
ctx context.Context
}
func (ss *tracingServerStream) Context() context.Context {
return ss.ctx
}
func injectSpanMeta(
ctx context.Context, tracer *tracing.Tracer, clientSpan *tracing.Span,
) context.Context {
md, ok := metadata.FromOutgoingContext(ctx)
if !ok {
md = metadata.New(nil)
} else {
md = md.Copy()
}
tracer.InjectMetaInto(clientSpan.Meta(), tracing.MetadataCarrier{MD: md})
return metadata.NewOutgoingContext(ctx, md)
}
// ClientInterceptor returns a grpc.UnaryClientInterceptor suitable
// for use in a grpc.Dial call.
//
// For example:
//
// conn, err := grpc.Dial(
// address,
// ..., // (existing DialOptions)
// grpc.WithUnaryInterceptor(ClientInterceptor(tracer)))
//
// All gRPC client spans will inject the tracing SpanMeta into the gRPC
// metadata; they will also look in the context.Context for an active
// in-process parent Span and establish a ChildOf relationship if such a parent
// Span could be found.
func ClientInterceptor(
tracer *tracing.Tracer, init func(*tracing.Span),
) grpc.UnaryClientInterceptor {
if init == nil {
init = func(*tracing.Span) {}
}
return func(
ctx context.Context,
method string,
req, resp interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
// Local RPCs don't need any special tracing, since the caller's context
// will be used on the "server".
_, localRequest := grpcutil.IsLocalRequestContext(ctx)
if localRequest {
return invoker(ctx, method, req, resp, cc, opts...)
}
parent := tracing.SpanFromContext(ctx)
if !tracing.SpanInclusionFuncForClient(parent) {
return invoker(ctx, method, req, resp, cc, opts...)
}
clientSpan := tracer.StartSpan(
method,
tracing.WithParent(parent),
tracing.WithClientSpanKind,
)
init(clientSpan)
defer clientSpan.Finish()
// For most RPCs we pass along tracing info as gRPC metadata. Some select
// RPCs carry the tracing in the request protos, which is more efficient.
if !methodExcludedFromTracing(method) {
ctx = injectSpanMeta(ctx, tracer, clientSpan)
}
if invoker != nil {
err := invoker(ctx, method, req, resp, cc, opts...)
if err != nil {
setGRPCErrorTag(clientSpan, err)
clientSpan.Recordf("error: %s", err)
return err
}
}
return nil
}
}
// StreamClientInterceptor returns a grpc.StreamClientInterceptor suitable
// for use in a grpc.Dial call. The interceptor instruments streaming RPCs by creating
// a single span to correspond to the lifetime of the RPC's stream.
//
// For example:
//
// conn, err := grpc.Dial(
// address,
// ..., // (existing DialOptions)
// grpc.WithStreamInterceptor(StreamClientInterceptor(tracer)))
//
// All gRPC client spans will inject the tracing SpanMeta into the gRPC
// metadata; they will also look in the context.Context for an active
// in-process parent Span and establish a ChildOf relationship if such a parent
// Span could be found.
func StreamClientInterceptor(
tracer *tracing.Tracer, init func(*tracing.Span),
) grpc.StreamClientInterceptor {
if init == nil {
init = func(*tracing.Span) {}
}
return func(
ctx context.Context,
desc *grpc.StreamDesc,
cc *grpc.ClientConn,
method string,
streamer grpc.Streamer,
opts ...grpc.CallOption,
) (grpc.ClientStream, error) {
// Local RPCs don't need any special tracing, since the caller's context
// will be used on the "server".
_, localRequest := grpcutil.IsLocalRequestContext(ctx)
if localRequest {
return streamer(ctx, desc, cc, method, opts...)
}
parent := tracing.SpanFromContext(ctx)
if !tracing.SpanInclusionFuncForClient(parent) {
return streamer(ctx, desc, cc, method, opts...)
}
// Create a span that will live for the life of the stream.
clientSpan := tracer.StartSpan(
method,
tracing.WithParent(parent),
tracing.WithClientSpanKind,
)
init(clientSpan)
if !methodExcludedFromTracing(method) {
ctx = injectSpanMeta(ctx, tracer, clientSpan)
}
cs, err := streamer(ctx, desc, cc, method, opts...)
if err != nil {
clientSpan.Recordf("error: %s", err)
setGRPCErrorTag(clientSpan, err)
clientSpan.Finish()
return cs, err
}
return newTracingClientStream(
ctx, cs, desc,
// Pass ownership of clientSpan to the stream.
clientSpan), nil
}
}
// newTracingClientStream creates and implementation of grpc.ClientStream that
// finishes `clientSpan` when the stream terminates.
func newTracingClientStream(
ctx context.Context, cs grpc.ClientStream, desc *grpc.StreamDesc, clientSpan *tracing.Span,
) grpc.ClientStream {
finishChan := make(chan struct{})
isFinished := new(int32)
*isFinished = 0
finishFunc := func(err error) {
// Since we have multiple code paths that could concurrently call
// `finishFunc`, we need to add some sort of synchronization to guard
// against multiple finishing.
if !atomic.CompareAndSwapInt32(isFinished, 0, 1) {
return
}
close(finishChan)
defer clientSpan.Finish()
if err != nil {
clientSpan.Recordf("error: %s", err)
setGRPCErrorTag(clientSpan, err)
}
}
go func() {
select {
case <-finishChan:
// The client span is being finished by another code path; hence, no
// action is necessary.
case <-ctx.Done():
// A streaming RPC can be finished by the caller cancelling the ctx. If
// the ctx is cancelled, the caller doesn't necessarily need to interact
// with the stream anymore (see [1]), so finishChan might never be
// signaled). Thus, we listen for ctx cancellation and finish the span.
//
// [1] https://pkg.go.dev/google.golang.org/grpc#ClientConn.NewStream
finishFunc(nil /* err */)
}
}()
return &tracingClientStream{
ClientStream: cs,
desc: desc,
finishFunc: finishFunc,
}
}
type tracingClientStream struct {
grpc.ClientStream
desc *grpc.StreamDesc
finishFunc func(error)
}
func (cs *tracingClientStream) Header() (metadata.MD, error) {
md, err := cs.ClientStream.Header()
if err != nil {
cs.finishFunc(err)
}
return md, errors.Wrap(err, "header error")
}
func (cs *tracingClientStream) SendMsg(m interface{}) error {
err := cs.ClientStream.SendMsg(m)
if err != nil {
cs.finishFunc(err)
}
return errors.Wrap(err, "send msg error")
}
func (cs *tracingClientStream) RecvMsg(m interface{}) error {
err := cs.ClientStream.RecvMsg(m)
if err == io.EOF {
cs.finishFunc(nil)
// Do not wrap EOF.
return err
} else if err != nil {
cs.finishFunc(err)
} else if !cs.desc.ServerStreams {
cs.finishFunc(nil)
}
return errors.Wrap(err, "recv msg error")
}
func (cs *tracingClientStream) CloseSend() error {
err := cs.ClientStream.CloseSend()
if err != nil {
cs.finishFunc(err)
}
return errors.Wrap(err, "close send error")
}