-
Notifications
You must be signed in to change notification settings - Fork 6
/
metric_client_messages_sent_total.go
66 lines (57 loc) · 1.72 KB
/
metric_client_messages_sent_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
65
66
package promgrpc
import (
"context"
"github.com/piotrkowalczuk/promgrpc/v4/internal/useragent"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc/stats"
)
func NewClientMessagesSentTotalCounterVec(opts ...CollectorOption) *prometheus.CounterVec {
labels := []string{
labelIsFailFast,
labelMethod,
labelService,
labelClientUserAgent,
}
return newMessagesSentTotalCounterVec("client", labels, opts...)
}
type ClientMessagesSentTotalStatsHandler struct {
baseStatsHandler
uas useragent.Store
vec *prometheus.CounterVec
}
// NewClientMessagesSentTotalStatsHandler ...
// 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 NewClientMessagesSentTotalStatsHandler(vec *prometheus.CounterVec, opts ...StatsHandlerOption) *ClientMessagesSentTotalStatsHandler {
h := &ClientMessagesSentTotalStatsHandler{
vec: vec,
}
h.baseStatsHandler = baseStatsHandler{
collector: vec,
options: statsHandlerOptions{
handleRPCLabelFn: h.labels,
},
}
h.applyOpts(opts...)
return h
}
// HandleRPC implements stats Handler interface.
func (h *ClientMessagesSentTotalStatsHandler) HandleRPC(ctx context.Context, stat stats.RPCStats) {
switch pay := stat.(type) {
case *stats.OutPayload:
if stat.IsClient() {
h.vec.WithLabelValues(h.options.handleRPCLabelFn(ctx, stat)...).Inc()
}
case *stats.OutHeader:
_ = h.uas.ClientSide(ctx, pay)
}
}
func (h *ClientMessagesSentTotalStatsHandler) labels(ctx context.Context, stat stats.RPCStats) []string {
tag := ctx.Value(tagRPCKey).(rpcTagLabels)
return []string{
tag.isFailFast,
tag.method,
tag.service,
h.uas.ClientSide(ctx, stat),
}
}