-
Notifications
You must be signed in to change notification settings - Fork 6
/
metric_client_connections.go
55 lines (48 loc) · 1.27 KB
/
metric_client_connections.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
package promgrpc
import (
"context"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc/stats"
)
func NewClientConnectionsGaugeVec(opts ...CollectorOption) *prometheus.GaugeVec {
labels := []string{labelRemoteAddr, labelLocalAddr}
return newConnectionsGaugeVec("client", labels, opts...)
}
type ClientConnectionsStatsHandler struct {
baseStatsHandler
vec *prometheus.GaugeVec
}
// NewClientConnectionsStatsHandler ...
func NewClientConnectionsStatsHandler(vec *prometheus.GaugeVec) *ClientConnectionsStatsHandler {
return &ClientConnectionsStatsHandler{
baseStatsHandler: baseStatsHandler{
collector: vec,
options: statsHandlerOptions{
client: true,
},
},
vec: vec,
}
}
// HandleConn HandleRPC processes the RPC stats.
func (h *ClientConnectionsStatsHandler) HandleConn(ctx context.Context, stat stats.ConnStats) {
switch stat.(type) {
case *stats.ConnBegin:
switch {
case stat.IsClient():
h.vec.WithLabelValues(h.labels(ctx)...).Inc()
}
case *stats.ConnEnd:
switch {
case stat.IsClient():
h.vec.WithLabelValues(h.labels(ctx)...).Dec()
}
}
}
func (h *ClientConnectionsStatsHandler) labels(ctx context.Context) []string {
tag := ctx.Value(tagConnKey).(connTagLabels)
return []string{
tag.remoteAddr,
tag.localAddr,
}
}