forked from johnbellone/grpc-middleware-sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_interceptors.go
92 lines (75 loc) · 2.15 KB
/
client_interceptors.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
package grpc_sentry
import (
"context"
"github.com/getsentry/sentry-go"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc"
)
func UnaryClientInterceptor(opts ...Option) grpc.UnaryClientInterceptor {
o := newConfig(opts)
return func(ctx context.Context,
method string,
req, reply interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
callOpts ...grpc.CallOption) error {
hub := sentry.GetHubFromContext(ctx)
if hub == nil {
hub = sentry.CurrentHub().Clone()
ctx = sentry.SetHubOnContext(ctx, hub)
}
operationName := defaultClientOperationName
if o.OperationNameOverride != "" {
operationName = o.OperationNameOverride
}
span := sentry.StartSpan(ctx, operationName)
ctx = span.Context()
md, ok := metadata.FromOutgoingContext(ctx)
if ok {
md.Append("sentry-trace", span.ToSentryTrace())
} else {
md = metadata.Pairs("sentry-trace", span.ToSentryTrace())
}
ctx = metadata.NewOutgoingContext(ctx, md)
defer span.Finish()
err := invoker(ctx, method, req, reply, cc, callOpts...)
if err != nil && o.ReportOn(err) {
hub.CaptureException(err)
}
return err
}
}
func StreamClientInterceptor(opts ...Option) grpc.StreamClientInterceptor {
o := newConfig(opts)
return func(ctx context.Context,
desc *grpc.StreamDesc,
cc *grpc.ClientConn,
method string,
streamer grpc.Streamer,
callOpts ...grpc.CallOption) (grpc.ClientStream, error) {
hub := sentry.GetHubFromContext(ctx)
if hub == nil {
hub = sentry.CurrentHub().Clone()
ctx = sentry.SetHubOnContext(ctx, hub)
}
operationName := defaultClientOperationName
if o.OperationNameOverride != "" {
operationName = o.OperationNameOverride
}
span := sentry.StartSpan(ctx, operationName)
ctx = span.Context()
md, ok := metadata.FromOutgoingContext(ctx)
if ok {
md.Append("sentry-trace", span.ToSentryTrace())
} else {
md = metadata.Pairs("sentry-trace", span.ToSentryTrace())
}
ctx = metadata.NewOutgoingContext(ctx, md)
defer span.Finish()
clientStream, err := streamer(ctx, desc, cc, method, callOpts...)
if err != nil && o.ReportOn(err) {
hub.CaptureException(err)
}
return clientStream, err
}
}