-
Notifications
You must be signed in to change notification settings - Fork 6
/
metric_server_request_duration_histogram_seconds.go
64 lines (54 loc) · 1.5 KB
/
metric_server_request_duration_histogram_seconds.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"
"google.golang.org/grpc/status"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc/stats"
)
func NewServerRequestDurationHistogramVec(opts ...CollectorOption) *prometheus.HistogramVec {
labels := []string{
labelClientUserAgent,
labelCode,
labelMethod,
labelService,
}
return newRequestDurationHistogramVec("server", labels, opts...)
}
type ServerRequestDurationStatsHandler struct {
baseStatsHandler
vec prometheus.ObserverVec
}
// NewServerRequestDurationStatsHandler ...
func NewServerRequestDurationStatsHandler(vec prometheus.ObserverVec, opts ...StatsHandlerOption) *ServerRequestDurationStatsHandler {
h := &ServerRequestDurationStatsHandler{
baseStatsHandler: baseStatsHandler{
collector: vec,
options: statsHandlerOptions{
handleRPCLabelFn: serverRequestDurationLabels,
},
},
vec: vec,
}
h.applyOpts(opts...)
return h
}
// HandleRPC processes the RPC stats.
func (h *ServerRequestDurationStatsHandler) HandleRPC(ctx context.Context, stat stats.RPCStats) {
if end, ok := stat.(*stats.End); ok {
switch {
case !stat.IsClient():
h.vec.
WithLabelValues(h.options.handleRPCLabelFn(ctx, stat)...).
Observe(end.EndTime.Sub(end.BeginTime).Seconds())
}
}
}
func serverRequestDurationLabels(ctx context.Context, stat stats.RPCStats) []string {
tag := ctx.Value(tagRPCKey).(rpcTagLabels)
return []string{
tag.clientUserAgent,
status.Code(stat.(*stats.End).Error).String(),
tag.method,
tag.service,
}
}