-
Notifications
You must be signed in to change notification settings - Fork 1
/
twirp_metrics.go
146 lines (117 loc) · 3.76 KB
/
twirp_metrics.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package elephantine
import (
"context"
"fmt"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/twitchtv/twirp"
)
// The code in this file is adapted from
// https://github.com/navigacontentlab/panurge/blob/main/twirp.go
//
// See twirp_metrics.LICENSE
type TwirpMetricsOptions struct {
reg prometheus.Registerer
testLatency time.Duration
contextCustomer func(ctx context.Context) string
}
type TwirpMetricOptionFunc func(opts *TwirpMetricsOptions)
// WithTwirpMetricsRegisterer uses a custom registerer for Twirp metrics.
func WithTwirpMetricsRegisterer(reg prometheus.Registerer) TwirpMetricOptionFunc {
return func(opts *TwirpMetricsOptions) {
opts.reg = reg
}
}
// WithTwirpMetricsStaticTestLatency configures the RPC metrics to report
// a static duration.
func WithTwirpMetricsStaticTestLatency(latency time.Duration) TwirpMetricOptionFunc {
return func(opts *TwirpMetricsOptions) {
opts.testLatency = latency
}
}
// WithTwirpMetricsCustomerFunc sets a function that can be used to return the
// customer label value for a context.
func WithTwirpMetricsCustomerFunc(fn func(ctx context.Context) string) TwirpMetricOptionFunc {
return func(opts *TwirpMetricsOptions) {
opts.contextCustomer = fn
}
}
// NewTwirpMetricsHooks creates new twirp hooks enabling prometheus metrics.
func NewTwirpMetricsHooks(opts ...TwirpMetricOptionFunc) (*twirp.ServerHooks, error) {
opt := TwirpMetricsOptions{
reg: prometheus.DefaultRegisterer,
contextCustomer: func(ctx context.Context) string {
return ""
},
}
for i := range opts {
opts[i](&opt)
}
requestsReceived := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "rpc_requests_total",
Help: "Number of RPC requests received.",
},
[]string{"service", "method", "customer"},
)
if err := opt.reg.Register(requestsReceived); err != nil {
return nil, fmt.Errorf("failed to register metric: %w", err)
}
duration := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "rpc_duration_seconds",
Help: "Duration for a rpc call.",
Buckets: prometheus.ExponentialBuckets(0.005, 1.75, 15),
}, []string{"service", "method", "customer"})
if err := opt.reg.Register(duration); err != nil {
return nil, fmt.Errorf("failed to register metric: %w", err)
}
responsesSent := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "rpc_responses_total",
Help: "Number of RPC responses sent.",
},
[]string{"service", "method", "status", "customer"},
)
if err := opt.reg.Register(responsesSent); err != nil {
return nil, fmt.Errorf("failed to register metric: %w", err)
}
var hooks twirp.ServerHooks
reqStartTimestampKey := new(int)
hooks.RequestReceived = func(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, reqStartTimestampKey, time.Now()), nil
}
hooks.ResponseSent = func(ctx context.Context) {
serviceName, sOk := twirp.ServiceName(ctx)
method, mOk := twirp.MethodName(ctx)
if !mOk || !sOk {
return
}
customer := opt.contextCustomer(ctx)
status, _ := twirp.StatusCode(ctx)
responsesSent.WithLabelValues(
serviceName, method, status, customer,
).Inc()
if start, ok := ctx.Value(reqStartTimestampKey).(time.Time); ok {
dur := time.Since(start).Seconds() // 100ms = 0.1 sek
if opt.testLatency != 0 {
dur = opt.testLatency.Seconds()
}
duration.WithLabelValues(
serviceName, method, customer,
).Observe(dur)
}
}
hooks.RequestRouted = func(ctx context.Context) (context.Context, error) {
serviceName, sOk := twirp.ServiceName(ctx)
method, mOk := twirp.MethodName(ctx)
if !(sOk && mOk) {
return ctx, nil
}
customer := opt.contextCustomer(ctx)
requestsReceived.WithLabelValues(
serviceName, method, customer,
).Inc()
return ctx, nil
}
return &hooks, nil
}