-
Notifications
You must be signed in to change notification settings - Fork 6
/
metric_server_requests_total.go
57 lines (49 loc) · 1.49 KB
/
metric_server_requests_total.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
package promgrpc
import (
"context"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc/stats"
)
func NewServerRequestsTotalCounterVec(opts ...CollectorOption) *prometheus.CounterVec {
labels := []string{
labelMethod,
labelService,
}
return newRequestsTotalCounterVec("server", "requests_received_total", "TODO", labels, opts...)
}
type ServerRequestsTotalStatsHandler struct {
baseStatsHandler
vec *prometheus.CounterVec
}
// NewServerRequestsTotalStatsHandler ...
// The GaugeVec must have zero, one, two, three or four non-const non-curried labels.
// For those, the only allowed labelsFn names are "fail_fast", "handler", "service".
func NewServerRequestsTotalStatsHandler(vec *prometheus.CounterVec, opts ...StatsHandlerOption) *ServerRequestsTotalStatsHandler {
h := &ServerRequestsTotalStatsHandler{
baseStatsHandler: baseStatsHandler{
collector: vec,
options: statsHandlerOptions{
handleRPCLabelFn: serverRequestsTotalLabels,
},
},
vec: vec,
}
h.applyOpts(opts...)
return h
}
// HandleRPC implements stats Handler interface.
func (h *ServerRequestsTotalStatsHandler) HandleRPC(ctx context.Context, stat stats.RPCStats) {
if beg, ok := stat.(*stats.Begin); ok {
switch {
case !beg.IsClient():
h.vec.WithLabelValues(h.options.handleRPCLabelFn(ctx, stat)...).Inc()
}
}
}
func serverRequestsTotalLabels(ctx context.Context, _ stats.RPCStats) []string {
tag := ctx.Value(tagRPCKey).(rpcTagLabels)
return []string{
tag.method,
tag.service,
}
}