-
Notifications
You must be signed in to change notification settings - Fork 1
/
grpc_connect_options.go
401 lines (336 loc) · 11.3 KB
/
grpc_connect_options.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
package connect
import (
"context"
"net"
"strings"
"time"
"runtime/debug"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
"github.com/afex/hystrix-go/hystrix"
"github.com/redis/go-redis/v9"
"github.com/sirupsen/logrus"
"github.com/ulule/limiter/v3"
redisStore "github.com/ulule/limiter/v3/drivers/store/redis"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc"
"github.com/imdario/mergo"
"github.com/kumparan/go-connect/internal"
"github.com/kumparan/go-connect/middleware"
"github.com/kumparan/go-utils"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/baggage"
otelcodes "go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
"go.opentelemetry.io/otel/trace"
)
// NewUnaryGRPCConnection establish a new grpc connection
func NewUnaryGRPCConnection(target string, dialOptions ...grpc.DialOption) (*grpc.ClientConn, error) {
conn, err := grpc.NewClient(target, dialOptions...)
if err != nil {
logrus.Errorf("Error : %v", err)
return nil, err
}
return conn, err
}
type messageType attribute.KeyValue
// Event adds an event of the messageType to the span associated with the
// passed context with a message id.
func (m messageType) Event(ctx context.Context, id int, _ interface{}) {
span := trace.SpanFromContext(ctx)
if !span.IsRecording() {
return
}
span.AddEvent("message", trace.WithAttributes(
attribute.KeyValue(m),
attribute.Key("message.id").Int(id),
))
}
var (
messageSent = messageType(attribute.Key("message.type").String("SENT"))
messageReceived = messageType(attribute.Key("message.type").String("RECEIVED"))
)
// RecoveryHandlerFunc is a function that recovers from the panic `p` by returning an `error`.
// The context can be used to extract request scoped metadata and context values.
type RecoveryHandlerFunc func(ctx context.Context, p interface{}) (err error)
// GRPCUnaryInterceptorOptions wrapper options for the grpc connection
type GRPCUnaryInterceptorOptions struct {
// UseCircuitBreaker flag if the connection will implement a circuit breaker
UseCircuitBreaker bool
// RetryCount retry the operation if found error.
// When set to <= 1, then it means no retry
RetryCount int
// RetryInterval next interval for retry.
RetryInterval time.Duration
// Timeout value, will return context deadline exceeded when the operation exceeds the duration
Timeout time.Duration
// UseOpenTelemetry flag if the connection will implement open telemetry
UseOpenTelemetry bool
// RateLimiter flag if the connection will implement rate limiter
RateLimiter *GRPCRateLimiter
RecoveryHandlerFunc RecoveryHandlerFunc
}
// GRPCRateLimiter wrapper for the gRPC rate limiter
type GRPCRateLimiter struct {
Limit int64
Period time.Duration
ExcludedIPs []string
ExcludedUserAgents []string
}
var defaultGRPCUnaryInterceptorOptions = &GRPCUnaryInterceptorOptions{
UseCircuitBreaker: false,
RetryCount: 0,
RetryInterval: 20 * time.Millisecond,
Timeout: 1 * time.Second,
UseOpenTelemetry: false,
RateLimiter: &GRPCRateLimiter{
Limit: 100,
Period: time.Second,
ExcludedIPs: []string{},
ExcludedUserAgents: []string{},
},
RecoveryHandlerFunc: func(ctx context.Context, p interface{}) (err error) {
logrus.WithFields(logrus.Fields{
"ctx": utils.DumpIncomingContext(ctx),
"stackTrace": string(debug.Stack()),
}).Errorf("panic recovered: %v", p)
return status.Error(codes.Internal, "internal server error")
},
}
// UnaryClientInterceptor wrapper with circuit breaker, retry, timeout, open telemetry, and metadata logging
func UnaryClientInterceptor(opts *GRPCUnaryInterceptorOptions) grpc.UnaryClientInterceptor {
o := applyGRPCUnaryInterceptorOptions(opts)
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
ctx, cancel := context.WithTimeout(ctx, o.Timeout)
defer cancel()
ctx = metadata.AppendToOutgoingContext(ctx, "caller", utils.MyCaller(5))
if o.UseCircuitBreaker {
success := make(chan bool, 1)
ignoredError := make(chan error, 1)
errC := hystrix.GoC(ctx, method, func(ctx context.Context) error {
err := o.retryableInvoke(ctx, method, req, reply, cc, invoker, opts...)
switch status.Code(err) {
case codes.OK:
success <- true
return nil
case codes.Internal, // circuit breaker can only open by these error codes
codes.Unknown,
codes.Unavailable,
codes.DeadlineExceeded:
return err
default:
ignoredError <- err
return nil
}
}, nil)
select {
case out := <-success:
logrus.Debugf("success %v", out)
return nil
case err := <-ignoredError:
return err
case err := <-errC:
logrus.Warnf("failed %s", err)
return err
}
}
return o.retryableInvoke(ctx, method, req, reply, cc, invoker, opts...)
}
}
func applyGRPCUnaryInterceptorOptions(opts *GRPCUnaryInterceptorOptions) *GRPCUnaryInterceptorOptions {
if opts == nil {
return defaultGRPCUnaryInterceptorOptions
}
// if error occurs, also return options from input
_ = mergo.Merge(opts, *defaultGRPCUnaryInterceptorOptions)
return opts
}
// spanInfo returns a span name and all appropriate attributes from the gRPC
// method and peer address.
func spanInfo(fullMethod, peerAddress string) (string, []attribute.KeyValue) {
attrs := []attribute.KeyValue{semconv.RPCSystemKey.String("grpc")}
name, mAttrs := internal.ParseFullMethod(fullMethod)
attrs = append(attrs, mAttrs...)
attrs = append(attrs, peerAttr(peerAddress)...)
return name, attrs
}
// peerAttr returns attributes about the peer address.
func peerAttr(addr string) []attribute.KeyValue {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return []attribute.KeyValue(nil)
}
if host == "" {
host = "127.0.0.1"
}
return []attribute.KeyValue{
semconv.NetPeerIPKey.String(host),
semconv.NetPeerPortKey.String(port),
}
}
func (o *GRPCUnaryInterceptorOptions) retryableInvoke(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return utils.Retry(o.RetryCount, o.RetryInterval, func() (err error) {
if !o.UseOpenTelemetry {
err = invoker(ctx, method, req, reply, cc, opts...)
if status.Code(err) != codes.Unavailable { // stop retrying unless Unavailable
return utils.NewRetryStopper(err)
}
return err
}
requestMetadata, _ := metadata.FromOutgoingContext(ctx)
metadataCopy := requestMetadata.Copy()
tracer := newConfig().TracerProvider.Tracer(
instrumentationName,
)
name, attr := spanInfo(method, cc.Target())
var span trace.Span
ctx, span = tracer.Start(
ctx,
name,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attr...),
)
defer span.End()
inject(ctx, &metadataCopy)
ctx = metadata.NewOutgoingContext(ctx, metadataCopy)
messageSent.Event(ctx, defaultMessageID, req)
err = invoker(ctx, method, req, reply, cc, opts...)
messageReceived.Event(ctx, defaultMessageID, reply)
switch {
case span == nil:
logrus.WithFields(logrus.Fields{
"context": utils.DumpIncomingContext(ctx),
}).Error("span is nil")
case err != nil:
s, _ := status.FromError(err)
span.SetStatus(otelcodes.Error, s.Message())
span.SetAttributes(statusCodeAttr(s.Code()))
default:
span.SetAttributes(statusCodeAttr(codes.OK))
}
if status.Code(err) != codes.Unavailable { // stop retrying unless Unavailable
return utils.NewRetryStopper(err)
}
return err
})
}
// peerFromCtx returns a peer address from a context, if one exists.
func peerFromCtx(ctx context.Context) string {
p, ok := peer.FromContext(ctx)
if !ok {
return ""
}
return p.Addr.String()
}
// statusCodeAttr returns status code attribute based on given gRPC code.
func statusCodeAttr(c codes.Code) attribute.KeyValue {
return grpcStatusCodeKey.Int64(int64(c))
}
// UnaryServerInterceptor wrapper with open telemetry
//
//gocognit:ignore
func UnaryServerInterceptor(opts *GRPCUnaryInterceptorOptions, redisClient *redis.Client) grpc.UnaryServerInterceptor {
return func(
ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (resp interface{}, err error) {
panicked := true // default value, if not panic, this will be changed to false before the defer func called
defer func() {
if r := recover(); r != nil || panicked {
err = recoverFrom(ctx, r, opts.RecoveryHandlerFunc)
}
}()
ctx, cancel := context.WithTimeout(ctx, opts.Timeout)
defer cancel()
var span trace.Span
if opts.UseOpenTelemetry {
requestMetadata, _ := metadata.FromIncomingContext(ctx)
metadataCopy := requestMetadata.Copy()
bags, spanCtx := extract(ctx, &metadataCopy)
ctx = baggage.ContextWithBaggage(ctx, bags)
tracer := newConfig().TracerProvider.Tracer(
instrumentationName,
)
name, attr := spanInfo(info.FullMethod, peerFromCtx(ctx))
ctx, span = tracer.Start(
trace.ContextWithRemoteSpanContext(ctx, spanCtx),
name,
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(attr...),
)
defer span.End()
messageReceived.Event(ctx, defaultMessageID, req)
}
if opts.RateLimiter != nil && redisClient != nil {
meta, ok := metadata.FromIncomingContext(ctx)
switch {
// skip if the ip address metadata is not found
case !ok || len(meta.Get(string(ipAddressKey))) <= 0:
default:
ipAddress := meta.Get(string(ipAddressKey))[0]
var userAgent string
if len(meta.Get(string(userAgentKey))) > 0 {
userAgent = meta.Get(string(userAgentKey))[0]
}
if ipAddress != "" && isRateLimited(ctx, redisClient, ipAddress, userAgent, opts.RateLimiter) {
err = status.Errorf(codes.ResourceExhausted, "too many requests")
goto TraceAndReturn
}
}
}
resp, err = handler(ctx, req)
TraceAndReturn:
if opts.UseOpenTelemetry {
if err != nil {
s, _ := status.FromError(err)
span.SetStatus(otelcodes.Error, s.Message())
span.SetAttributes(statusCodeAttr(s.Code()))
messageSent.Event(ctx, defaultMessageID, s.Proto())
} else {
span.SetAttributes(statusCodeAttr(codes.OK))
messageSent.Event(ctx, defaultMessageID, resp)
}
}
panicked = false
return resp, err
}
}
func recoverFrom(ctx context.Context, p interface{}, r RecoveryHandlerFunc) error {
if r == nil {
logrus.WithFields(logrus.Fields{
"ctx": utils.DumpIncomingContext(ctx),
"stackTrace": string(debug.Stack()),
}).Errorf("panic recovered: %v", p)
return status.Errorf(codes.Internal, "%v", p)
}
return r(ctx, p)
}
func isRateLimited(ctx context.Context, redisClient *redis.Client, ip, userAgent string, ratelimiter *GRPCRateLimiter) bool {
switch {
case middleware.PrivateIPAddressRegex.MatchString(ip), utils.Contains[string](ratelimiter.ExcludedIPs, ip):
return false
case userAgent == "":
case utils.Contains[string](ratelimiter.ExcludedUserAgents, strings.TrimSpace(strings.ToLower(userAgent))):
return false
}
store, err := redisStore.NewStoreWithOptions(redisClient, limiter.StoreOptions{
Prefix: "grpc-rate-limiter:",
})
if err != nil {
return false
}
limiterCtx, err := limiter.New(store, limiter.Rate{
Period: ratelimiter.Period,
Limit: ratelimiter.Limit,
}).Get(ctx, ip)
if err != nil {
return false
}
if limiterCtx.Reached {
return true
}
return false
}