-
Notifications
You must be signed in to change notification settings - Fork 73
/
prometheus.go
119 lines (101 loc) · 3.02 KB
/
prometheus.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
package grpcbp
import (
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/reddit/baseplate.go/internal/prometheusbpint"
"github.com/reddit/baseplate.go/prometheusbp"
)
const (
serviceLabel = "grpc_service"
methodLabel = "grpc_method"
typeLabel = "grpc_type"
successLabel = "grpc_success"
codeLabel = "grpc_code"
clientNameLabel = "grpc_client_name"
)
const (
unary = "unary"
clientStream = "client_stream"
serverStream = "server_stream"
)
var (
serverLatencyLabels = []string{
serviceLabel,
methodLabel,
typeLabel,
successLabel,
}
serverLatencyDistribution = promauto.With(prometheusbpint.GlobalRegistry).NewHistogramVec(prometheus.HistogramOpts{
Name: "grpc_server_latency_seconds",
Help: "RPC latencies",
Buckets: prometheusbp.DefaultLatencyBuckets,
}, serverLatencyLabels)
serverTotalRequestLabels = []string{
serviceLabel,
methodLabel,
typeLabel,
successLabel,
codeLabel,
}
serverTotalRequests = promauto.With(prometheusbpint.GlobalRegistry).NewCounterVec(prometheus.CounterOpts{
Name: "grpc_server_requests_total",
Help: "Total RPC request count",
}, serverTotalRequestLabels)
serverActiveRequestsLabels = []string{
serviceLabel,
methodLabel,
typeLabel,
}
serverActiveRequests = promauto.With(prometheusbpint.GlobalRegistry).NewGaugeVec(prometheus.GaugeOpts{
Name: "grpc_server_active_requests",
Help: "The number of in-flight requests being handled by the service",
}, serverActiveRequestsLabels)
)
var (
clientLatencyLabels = []string{
serviceLabel,
methodLabel,
typeLabel,
successLabel,
clientNameLabel,
}
clientLatencyDistribution = promauto.With(prometheusbpint.GlobalRegistry).NewHistogramVec(prometheus.HistogramOpts{
Name: "grpc_client_latency_seconds",
Help: "RPC latencies",
Buckets: prometheusbp.DefaultLatencyBuckets,
}, clientLatencyLabels)
clientTotalRequestLabels = []string{
serviceLabel,
methodLabel,
typeLabel,
successLabel,
codeLabel,
clientNameLabel,
}
clientTotalRequests = promauto.With(prometheusbpint.GlobalRegistry).NewCounterVec(prometheus.CounterOpts{
Name: "grpc_client_requests_total",
Help: "Total RPC request count",
}, clientTotalRequestLabels)
clientActiveRequestsLabels = []string{
serviceLabel,
methodLabel,
typeLabel,
clientNameLabel,
}
clientActiveRequests = promauto.With(prometheusbpint.GlobalRegistry).NewGaugeVec(prometheus.GaugeOpts{
Name: "grpc_client_active_requests",
Help: "The number of in-flight requests",
}, clientActiveRequestsLabels)
)
// serviceAndMethodSlug splits the UnaryServerInfo.FullMethod and returns
// the package.service part separate from the method part.
// ref: https://pkg.go.dev/google.golang.org/grpc#UnaryServerInfo
func serviceAndMethodSlug(fullMethod string) (service, method string) {
fullMethod = strings.TrimPrefix(fullMethod, "/")
split := strings.SplitN(fullMethod, "/", 2)
if len(split) < 2 {
return "", fullMethod
}
return split[0], split[1]
}