forked from johnbellone/grpc-middleware-sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_interceptors.go
200 lines (173 loc) · 5.03 KB
/
server_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
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
package grpc_sentry
import (
"context"
"encoding/hex"
"regexp"
"github.com/getsentry/sentry-go"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_tags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/grpc"
)
func recoverWithSentry(hub *sentry.Hub, ctx context.Context, o *options) {
if err := recover(); err != nil {
eventID := hub.RecoverWithContext(ctx, err)
if eventID != nil && o.WaitForDelivery {
hub.Flush(o.Timeout)
}
if o.Repanic {
panic(err)
}
}
}
func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
o := newConfig(opts)
return func(ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (interface{}, error) {
hub := sentry.GetHubFromContext(ctx)
if hub == nil {
hub = sentry.CurrentHub().Clone()
ctx = sentry.SetHubOnContext(ctx, hub)
}
operationName := defaultServerOperationName
if o.OperationNameOverride != "" {
operationName = o.OperationNameOverride
}
md, _ := metadata.FromIncomingContext(ctx) // nil check in ContinueFromGrpcMetadata
span := sentry.StartSpan(ctx, operationName, ContinueFromGrpcMetadata(md))
ctx = span.Context()
defer span.Finish()
// TODO: Perhaps makes sense to use SetRequestBody instead?
hub.Scope().SetExtra("requestBody", req)
defer recoverWithSentry(hub, ctx, o)
resp, err := handler(ctx, req)
if err != nil && o.ReportOn(err) {
tags := grpc_tags.Extract(ctx)
for k, v := range tags.Values() {
hub.Scope().SetTag(k, v.(string))
}
hub.CaptureException(err)
}
span.Status = toSpanStatus(status.Code(err))
return resp, err
}
}
func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
o := newConfig(opts)
return func(srv interface{},
ss grpc.ServerStream,
info *grpc.StreamServerInfo,
handler grpc.StreamHandler) error {
ctx := ss.Context()
hub := sentry.GetHubFromContext(ctx)
if hub == nil {
hub = sentry.CurrentHub().Clone()
ctx = sentry.SetHubOnContext(ctx, hub)
}
operationName := defaultServerOperationName
if o.OperationNameOverride != "" {
operationName = o.OperationNameOverride
}
md, _ := metadata.FromIncomingContext(ctx) // nil check in ContinueFromGrpcMetadata
span := sentry.StartSpan(ctx, operationName, ContinueFromGrpcMetadata(md))
ctx = span.Context()
defer span.Finish()
stream := grpc_middleware.WrapServerStream(ss)
stream.WrappedContext = ctx
defer recoverWithSentry(hub, ctx, o)
err := handler(srv, stream)
if err != nil && o.ReportOn(err) {
tags := grpc_tags.Extract(ctx)
for k, v := range tags.Values() {
hub.Scope().SetTag(k, v.(string))
}
hub.CaptureException(err)
}
span.Status = toSpanStatus(status.Code(err))
return err
}
}
// ContinueFromGrpcMetadata returns a span option that updates the span to continue
// an existing trace. If it cannot detect an existing trace in the request, the
// span will be left unchanged.
func ContinueFromGrpcMetadata(md metadata.MD) sentry.SpanOption {
return func(s *sentry.Span) {
if md == nil {
return
}
trace, ok := md["sentry-trace"]
if !ok {
return
}
if len(trace) != 1 {
return
}
if trace[0] == "" {
return
}
updateFromSentryTrace(s, []byte(trace[0]))
}
}
// Re-export of functions from tracing.go of sentry-go
var sentryTracePattern = regexp.MustCompile(`^([[:xdigit:]]{32})-([[:xdigit:]]{16})(?:-([01]))?$`)
func updateFromSentryTrace(s *sentry.Span, header []byte) {
m := sentryTracePattern.FindSubmatch(header)
if m == nil {
// no match
return
}
_, _ = hex.Decode(s.TraceID[:], m[1])
_, _ = hex.Decode(s.ParentSpanID[:], m[2])
if len(m[3]) != 0 {
switch m[3][0] {
case '0':
s.Sampled = sentry.SampledFalse
case '1':
s.Sampled = sentry.SampledTrue
}
}
}
func toSpanStatus(code codes.Code) sentry.SpanStatus {
switch code {
case codes.OK:
return sentry.SpanStatusOK
case codes.Canceled:
return sentry.SpanStatusCanceled
case codes.Unknown:
return sentry.SpanStatusUnknown
case codes.InvalidArgument:
return sentry.SpanStatusInvalidArgument
case codes.DeadlineExceeded:
return sentry.SpanStatusDeadlineExceeded
case codes.NotFound:
return sentry.SpanStatusNotFound
case codes.AlreadyExists:
return sentry.SpanStatusAlreadyExists
case codes.PermissionDenied:
return sentry.SpanStatusPermissionDenied
case codes.ResourceExhausted:
return sentry.SpanStatusResourceExhausted
case codes.FailedPrecondition:
return sentry.SpanStatusFailedPrecondition
case codes.Aborted:
return sentry.SpanStatusAborted
case codes.OutOfRange:
return sentry.SpanStatusOutOfRange
case codes.Unimplemented:
return sentry.SpanStatusUnimplemented
case codes.Internal:
return sentry.SpanStatusInternalError
case codes.Unavailable:
return sentry.SpanStatusUnavailable
case codes.DataLoss:
return sentry.SpanStatusDataLoss
case codes.Unauthenticated:
return sentry.SpanStatusUnauthenticated
default:
return sentry.SpanStatusUndefined
}
}