-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
455 lines (422 loc) · 11.6 KB
/
handler.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package grpcprom
import (
"context"
"fmt"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/stats"
"google.golang.org/grpc/status"
)
const (
header = "Header"
payload = "Payload"
trailer = "Trailer"
)
const (
unknown = "Unknown"
unary = "Unary"
clientStream = "ClientStream"
serverStream = "ServerStream"
bidiStream = "BidiStream"
)
type handler struct {
methods sync.Map // name => info
connsOpen prometheus.Gauge
connsTotal prometheus.Counter
reqsPending gaugeVec
reqsTotal counterVec
latency observer
sentBytes observer
recvBytes observer
}
func newMetrics(subsys string, opts ...Option) *handler {
o := &options{
latency: histogramOptions{
buckets: DefaultLatencyBuckets,
},
}
for _, opt := range opts {
opt.applyOption(o)
}
return &handler{
connsOpen: newConnsOpen(subsys, o.connsOpen),
connsTotal: newConnsTotal(subsys, o.connsTotal),
reqsPending: newReqsPending(subsys, o.reqsPending),
reqsTotal: newReqsTotal(subsys, o.reqsTotal),
latency: newLatency(subsys, o.latency),
sentBytes: newSentBytes(subsys, o.sentBytes),
recvBytes: newRecvBytes(subsys, o.recvBytes),
}
}
func newConnsOpen(subsys string, opts metricOptions) prometheus.Gauge {
if opts.disable {
return noopGauge{}
}
return prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: "grpc",
Subsystem: subsys,
Name: "connections_open",
Help: fmt.Sprintf("Number of gRPC %s connections open.", subsys),
},
)
}
func newConnsTotal(subsys string, opts metricOptions) prometheus.Counter {
if opts.disable {
return noopCounter{}
}
return prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: "grpc",
Subsystem: subsys,
Name: "connections_total",
Help: fmt.Sprintf("Total number of gRPC %s connections opened.", subsys),
},
)
}
func newReqsPending(subsys string, opts metricOptions) gaugeVec {
if opts.disable {
return noopGaugeVec{}
}
return prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "grpc",
Subsystem: subsys,
Name: "requests_pending",
Help: fmt.Sprintf("Number of gRPC %s requests pending.", subsys),
},
[]string{"grpc_type", "grpc_service", "grpc_method"},
)
}
func newReqsTotal(subsys string, opts metricOptions) counterVec {
if opts.disable {
return noopCounterVec{}
}
return prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "grpc",
Subsystem: subsys,
Name: "requests_total",
Help: fmt.Sprintf("Total number of gRPC %s requests completed.", subsys),
},
[]string{"grpc_type", "grpc_service", "grpc_method", "grpc_code"},
)
}
func newLatency(subsys string, opts histogramOptions) observer {
if opts.disable {
return noopObserver{}
}
if len(opts.buckets) > 0 {
return &histogram{prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "grpc",
Subsystem: subsys,
Name: "latency_seconds",
Help: fmt.Sprintf("Latency of gRPC %s requests.", subsys),
Buckets: opts.buckets,
},
[]string{"grpc_type", "grpc_service", "grpc_method", "grpc_code"},
)}
}
return &counters{
sum: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "grpc",
Subsystem: subsys,
Name: "latency_seconds_sum",
Help: fmt.Sprintf("Latency of gRPC %s requests sum.", subsys),
},
[]string{"grpc_type", "grpc_service", "grpc_method", "grpc_code"},
),
num: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "grpc",
Subsystem: subsys,
Name: "latency_seconds_count",
Help: fmt.Sprintf("Latency of gRPC %s requests count.", subsys),
},
[]string{"grpc_type", "grpc_service", "grpc_method", "grpc_code"},
),
}
}
func newSentBytes(subsys string, opts histogramOptions) observer {
if opts.disable {
return noopObserver{}
}
typ := "responses"
if subsys == "client" {
typ = "requests"
}
if len(opts.buckets) > 0 {
return &histogram{prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "grpc",
Subsystem: subsys,
Name: "sent_bytes",
Help: fmt.Sprintf("Bytes sent in gRPC %s %s.", subsys, typ),
Buckets: opts.buckets,
},
[]string{"grpc_type", "grpc_service", "grpc_method", "grpc_frame"},
)}
}
return &counters{
sum: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "grpc",
Subsystem: subsys,
Name: "sent_bytes_sum",
Help: fmt.Sprintf("Bytes sent in gRPC %s %s sum.", subsys, typ),
},
[]string{"grpc_type", "grpc_service", "grpc_method", "grpc_frame"},
),
num: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "grpc",
Subsystem: subsys,
Name: "sent_bytes_count",
Help: fmt.Sprintf("Bytes sent in gRPC %s %s count.", subsys, typ),
},
[]string{"grpc_type", "grpc_service", "grpc_method", "grpc_frame"},
),
}
}
func newRecvBytes(subsys string, opts histogramOptions) observer {
if opts.disable {
return noopObserver{}
}
typ := "requests"
if subsys == "client" {
typ = "responses"
}
if len(opts.buckets) > 0 {
return &histogram{prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "grpc",
Subsystem: subsys,
Name: "recv_bytes",
Help: fmt.Sprintf("Bytes received in gRPC %s %s.", subsys, typ),
Buckets: opts.buckets,
},
[]string{"grpc_type", "grpc_service", "grpc_method", "grpc_frame"},
)}
}
return &counters{
sum: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "grpc",
Subsystem: subsys,
Name: "recv_bytes_sum",
Help: fmt.Sprintf("Bytes received in gRPC %s %s sum.", subsys, typ),
},
[]string{"grpc_type", "grpc_service", "grpc_method", "grpc_frame"},
),
num: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "grpc",
Subsystem: subsys,
Name: "recv_bytes_count",
Help: fmt.Sprintf("Bytes received in gRPC %s %s count.", subsys, typ),
},
[]string{"grpc_type", "grpc_service", "grpc_method", "grpc_frame"},
),
}
}
func (h *handler) init(server string, methods []grpc.MethodInfo, codes []codes.Code) {
for _, meth := range methods {
typ := grpcType(meth.IsClientStream, meth.IsServerStream)
h.methods.Store("/"+server+"/"+meth.Name, methodInfo{
typ: typ,
server: server,
method: meth.Name,
})
h.reqsPending.GetMetricWithLabelValues(typ, server, meth.Name)
for _, c := range codes {
code := c.String()
h.reqsTotal.GetMetricWithLabelValues(typ, server, meth.Name, code)
h.latency.Init(typ, server, meth.Name, code)
}
for _, f := range [...]string{header, payload, trailer} {
h.sentBytes.Init(typ, server, meth.Name, f)
h.recvBytes.Init(typ, server, meth.Name, f)
}
}
}
func (h *handler) describe(ch chan<- *prometheus.Desc) {
h.connsOpen.Describe(ch)
h.connsTotal.Describe(ch)
h.reqsPending.Describe(ch)
h.reqsTotal.Describe(ch)
h.latency.Describe(ch)
h.sentBytes.Describe(ch)
h.recvBytes.Describe(ch)
}
func (h *handler) collect(ch chan<- prometheus.Metric) {
h.connsOpen.Collect(ch)
h.connsTotal.Collect(ch)
h.reqsPending.Collect(ch)
h.reqsTotal.Collect(ch)
h.latency.Collect(ch)
h.sentBytes.Collect(ch)
h.recvBytes.Collect(ch)
}
// TagConn implements the stats.Handler interface.
func (*handler) TagConn(ctx context.Context, v *stats.ConnTagInfo) context.Context {
return ctx
}
// HandleConn implements the stats.Handler interface.
func (h *handler) HandleConn(ctx context.Context, stat stats.ConnStats) {
switch stat.(type) {
case *stats.ConnBegin:
h.connsOpen.Inc()
h.connsTotal.Inc()
case *stats.ConnEnd:
h.connsOpen.Dec()
}
}
type rpcInfo struct {
methodInfo
begin time.Time
}
type methodInfo struct {
typ string
server string
method string
}
func (h *handler) methodInfo(method, typ string) methodInfo {
x, _ := h.methods.Load(method)
if info, ok := x.(methodInfo); ok {
return info
}
srv, meth := splitFullMethodName(method)
info := methodInfo{
typ: typ,
server: srv,
method: meth,
}
if typ != unknown {
h.methods.Store(method, info)
}
return info
}
// TagRPC implements the stats.Handler interface.
func (h *handler) TagRPC(ctx context.Context, v *stats.RPCTagInfo) context.Context {
if _, ok := ctx.Value(h).(*rpcInfo); ok {
return ctx
}
return context.WithValue(ctx, h, &rpcInfo{
methodInfo: h.methodInfo(v.FullMethodName, unknown),
})
}
func splitFullMethodName(s string) (server, method string) {
s = strings.TrimPrefix(s, "/")
i := strings.Index(s, "/")
if i < 0 {
return "Unknown", "Unknown"
}
return s[:i], s[i+1:]
}
// HandleRPC implements the stats.Handler interface.
func (h *handler) HandleRPC(ctx context.Context, stat stats.RPCStats) {
v, ok := ctx.Value(h).(*rpcInfo)
if !ok {
return
}
switch s := stat.(type) {
case *stats.Begin:
v.begin = s.BeginTime
h.reqsPending.WithLabelValues(v.typ, v.server, v.method).Inc()
case *stats.End:
code := status.Code(s.Error).String()
h.latency.Observe(time.Since(v.begin).Seconds(), v.typ, v.server, v.method, code)
h.reqsTotal.WithLabelValues(v.typ, v.server, v.method, code).Inc()
h.reqsPending.WithLabelValues(v.typ, v.server, v.method).Dec()
case *stats.InHeader:
h.recvBytes.Observe(float64(s.WireLength), v.typ, v.server, v.method, header)
case *stats.InPayload:
h.recvBytes.Observe(float64(s.WireLength), v.typ, v.server, v.method, payload)
case *stats.InTrailer:
h.recvBytes.Observe(float64(s.WireLength), v.typ, v.server, v.method, trailer)
case *stats.OutHeader:
// TODO: WireLength doesn't exist ???
h.sentBytes.Observe(0, v.typ, v.server, v.method, header)
case *stats.OutPayload:
h.sentBytes.Observe(float64(s.WireLength), v.typ, v.server, v.method, payload)
case *stats.OutTrailer:
// TODO: WireLength is never set ???
h.sentBytes.Observe(0, v.typ, v.server, v.method, trailer)
}
}
func (h *handler) unaryClientInterceptor(
ctx context.Context,
method string,
req, reply interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
ctx = h.context(ctx, method, unary)
return invoker(ctx, method, req, reply, cc, opts...)
}
func (h *handler) unaryServerInterceptor(
ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (resp interface{}, err error) {
ctx = h.context(ctx, info.FullMethod, unary)
return handler(ctx, req)
}
func (h *handler) streamClientInterceptor(
ctx context.Context,
desc *grpc.StreamDesc,
cc *grpc.ClientConn,
method string,
streamer grpc.Streamer,
opts ...grpc.CallOption,
) (grpc.ClientStream, error) {
ctx = h.context(ctx, method, grpcType(desc.ClientStreams, desc.ServerStreams))
return streamer(ctx, desc, cc, method, opts...)
}
func (h *handler) streamServerInterceptor(
srv interface{},
ss grpc.ServerStream,
info *grpc.StreamServerInfo,
handler grpc.StreamHandler,
) error {
typ := grpcType(info.IsClientStream, info.IsServerStream)
return handler(srv, &ctxServerStream{
ServerStream: ss,
ctx: h.context(ss.Context(), info.FullMethod, typ),
})
}
func (h *handler) context(ctx context.Context, method string, typ string) context.Context {
info := h.methodInfo(method, typ)
if v, ok := ctx.Value(h).(*rpcInfo); ok {
v.methodInfo = info
return ctx
}
return context.WithValue(ctx, h, &rpcInfo{methodInfo: info})
}
type ctxServerStream struct {
grpc.ServerStream
ctx context.Context
}
func (ss *ctxServerStream) Context() context.Context {
return ss.ctx
}
func grpcType(isClientStream, isServerStream bool) string {
if isServerStream {
if isClientStream {
return bidiStream
}
return serverStream
}
if isClientStream {
return clientStream
}
return unary
}