-
Notifications
You must be signed in to change notification settings - Fork 6
/
metric_client_requests_total.go
64 lines (55 loc) · 1.65 KB
/
metric_client_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
58
59
60
61
62
63
64
package promgrpc
import (
"context"
"github.com/piotrkowalczuk/promgrpc/v4/internal/useragent"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc/stats"
)
func NewClientRequestsTotalCounterVec(opts ...CollectorOption) *prometheus.CounterVec {
labels := []string{
labelIsFailFast,
labelMethod,
labelService,
labelClientUserAgent,
}
return newRequestsTotalCounterVec("client", "requests_sent_total", "TODO", labels, opts...)
}
type ClientRequestsTotalStatsHandler struct {
baseStatsHandler
uas useragent.Store
vec *prometheus.CounterVec
}
// NewClientRequestsTotalStatsHandler ...
// 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 NewClientRequestsTotalStatsHandler(vec *prometheus.CounterVec, opts ...StatsHandlerOption) *ClientRequestsTotalStatsHandler {
h := &ClientRequestsTotalStatsHandler{
vec: vec,
}
h.baseStatsHandler = baseStatsHandler{
collector: vec,
options: statsHandlerOptions{
handleRPCLabelFn: h.labels,
},
}
h.applyOpts(opts...)
return h
}
// HandleRPC implements stats Handler interface.
func (h *ClientRequestsTotalStatsHandler) HandleRPC(ctx context.Context, stat stats.RPCStats) {
if !stat.IsClient() {
return
}
if _, ok := stat.(*stats.OutHeader); ok {
h.vec.WithLabelValues(h.options.handleRPCLabelFn(ctx, stat)...).Inc()
}
}
func (h *ClientRequestsTotalStatsHandler) labels(ctx context.Context, sts stats.RPCStats) []string {
tag := ctx.Value(tagRPCKey).(rpcTagLabels)
return []string{
tag.isFailFast,
tag.method,
tag.service,
h.uas.ClientSide(ctx, sts),
}
}