-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
332 lines (275 loc) · 8.83 KB
/
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package pubsub_prometheus
import (
"context"
"sync"
"time"
"github.com/hashicorp/go-multierror"
"github.com/hmoragrega/pubsub"
"github.com/prometheus/client_golang/prometheus"
)
type labelKey = string
const (
consumerKey labelKey = "consumer"
msgNameKey labelKey = "msg_name"
msgVersionKey labelKey = "msg_version"
errorKey labelKey = "error"
checkpointKey labelKey = "checkpoint"
operationKey labelKey = "operation"
topicKey labelKey = "topic"
)
var (
defaultMonitor Monitor
commonLabels = []string{consumerKey, msgNameKey, msgVersionKey, errorKey}
)
// Monitor can be used to instrument a pubsub.Router.
//
// The zero value monitor will add the metrics with their default names
// in the prometheus default registerer.
type Monitor struct {
// Registerer optional registerer for the metrics.
Registerer prometheus.Registerer
// ProcessedOpts optional options for the processed message histogram.
ProcessedOpts prometheus.HistogramOpts
// CheckpointOpts optional options for the router checkpoints counter.
CheckpointOpts prometheus.CounterOpts
// AckOpts optional options for the message acknowledgements counter.
AckOpts prometheus.CounterOpts
// PublishOpts optional options for the publishing attempts histogram.
PublishOpts prometheus.HistogramOpts
// PublishedOpts optional options for the published messages counter.
PublishedOpts prometheus.CounterOpts
// ConsumedOpts optional options for the subscriber next message counter.
ConsumedOpts prometheus.CounterOpts
// Namespace will be used on all metrics unless overwritten by the
// specific metric config.
Namespace string
// Subsystem will be used on all metrics unless overwritten by the
// specific metric config.
Subsystem string
// ConstLabels can be used to add constant label in the all the metrics.
ConstLabels prometheus.Labels
processed *prometheus.HistogramVec
checkpoint *prometheus.CounterVec
ack *prometheus.CounterVec
publish *prometheus.HistogramVec
published *prometheus.CounterVec
consumed *prometheus.CounterVec
once sync.Once
registerErr error
}
// Register will register the metrics in the prometheus registerer.
//
// Note that Register will be automatically called when instrumenting a router.
// Use it for fine-grained control on registration errors.
//
// Register can only be called once, and successive calls will return the
// first execution error.
func (m *Monitor) Register() error {
m.once.Do(func() {
reg := prometheus.DefaultRegisterer
if m.Registerer != nil {
reg = m.Registerer
}
collectors := []prometheus.Collector{
m.buildProcessed(m.ProcessedOpts),
m.buildCheckpoint(m.CheckpointOpts),
m.buildAck(m.AckOpts),
m.buildPublish(m.PublishOpts),
m.buildPublished(m.PublishedOpts),
m.buildConsumed(m.ConsumedOpts),
}
result := make(chan error, 4)
go func() {
defer close(result)
for _, c := range collectors {
result <- reg.Register(c)
}
}()
for err := range result {
if err != nil {
m.registerErr = multierror.Append(m.registerErr, err)
return
}
}
})
return m.registerErr
}
// MustInstrumentRouterWithMonitor helper to instrument a router and returns the same instance,
// use it for one line router initializations.
//
// It will panic on metric registration error.
func MustInstrumentRouterWithMonitor(monitor *Monitor, router *pubsub.Router) *pubsub.Router {
return monitor.MustInstrumentRouter(router)
}
// MustInstrumentRouter helper to instrument a router and returns the same instance,
// use it for one line router initializations.
//
// It will panic on metric registration error.
func MustInstrumentRouter(router *pubsub.Router) *pubsub.Router {
return MustInstrumentRouterWithMonitor(&defaultMonitor, router)
}
// MustInstrumentRouter helper to instrument a router and returns the same instance.
//
// It will panic on metric registration error.
func (m *Monitor) MustInstrumentRouter(router *pubsub.Router) *pubsub.Router {
if err := m.InstrumentRouter(router); err != nil {
panic(err)
}
return router
}
// InstrumentRouterWithMonitor helper to instrument a router returning any errors that may happen.
func InstrumentRouterWithMonitor(monitor *Monitor, router *pubsub.Router) error {
return monitor.InstrumentRouter(router)
}
// InstrumentRouter helper to instrument a router returning any errors that may happen.
func InstrumentRouter(router *pubsub.Router) error {
return InstrumentRouterWithMonitor(&defaultMonitor, router)
}
// InstrumentRouter a router returning any errors that may happen.
func (m *Monitor) InstrumentRouter(router *pubsub.Router) error {
if err := m.Register(); err != nil {
return err
}
// checkpoint counters
router.OnReceive = pubsub.WrapCheckpoint(router.OnReceive, m.onCheckpoint("receive"))
router.OnUnmarshal = pubsub.WrapCheckpoint(router.OnUnmarshal, m.onCheckpoint("unmarshal"))
router.OnHandler = pubsub.WrapCheckpoint(router.OnHandler, m.onCheckpoint("handler"))
router.OnAck = pubsub.WrapCheckpoint(router.OnAck, m.onCheckpoint("ack"))
// processed messages
router.OnProcess = pubsub.WrapOnProcess(router.OnProcess, m.onProcess)
// acknowledgements and consume operations
router.OnNext = pubsub.WrapNext(router.OnNext, m.onNext)
return nil
}
func (m *Monitor) onNext(_ context.Context, consumerName string, next pubsub.Next) pubsub.Next {
if msg := next.Message; msg != nil {
next.Message = wrap(msg, consumerName, m.ack)
}
m.consumed.With(
metricLabels(nil, consumerName, next.Message, next.Err)).
Add(1)
return next
}
// OnProcess will sent a histogram metric
func (m *Monitor) onProcess(_ context.Context, consumerName string, elapsed time.Duration, msg pubsub.ReceivedMessage, err error) {
m.processed.
With(metricLabels(nil, consumerName, msg, err)).
Observe(elapsed.Seconds())
}
func (m *Monitor) onCheckpoint(checkpoint string) pubsub.Checkpoint {
return func(ctx context.Context, consumerName string, msg pubsub.ReceivedMessage, err error) error {
m.checkpoint.
With(metricLabels(map[string]string{checkpointKey: checkpoint}, consumerName, msg, err)).
Add(1)
return nil
}
}
func (m *Monitor) buildProcessed(opts prometheus.HistogramOpts) *prometheus.HistogramVec {
if opts.Name == "" {
opts.Name = "pubsub_message_processed"
}
if opts.Help == "" {
opts.Help = "Number of processed messages"
}
if opts.Namespace == "" {
opts.Namespace = m.Namespace
}
if opts.Subsystem == "" {
opts.Subsystem = m.Subsystem
}
opts.ConstLabels = mergeLabels(m.ConstLabels, opts.ConstLabels)
h := prometheus.NewHistogramVec(opts, metricKeys())
m.processed = h
return h
}
func (m *Monitor) buildCheckpoint(opts prometheus.CounterOpts) *prometheus.CounterVec {
if opts.Name == "" {
opts.Name = "pubsub_message_checkpoint"
}
if opts.Help == "" {
opts.Help = "Number of message checkpoints executed"
}
if opts.Namespace == "" {
opts.Namespace = m.Namespace
}
if opts.Subsystem == "" {
opts.Subsystem = m.Subsystem
}
opts.ConstLabels = mergeLabels(m.ConstLabels, opts.ConstLabels)
h := prometheus.NewCounterVec(opts, metricKeys(checkpointKey))
m.checkpoint = h
return h
}
func (m *Monitor) buildAck(opts prometheus.CounterOpts) *prometheus.CounterVec {
if opts.Name == "" {
opts.Name = "pubsub_message_acknowledgements"
}
if opts.Help == "" {
opts.Help = "Number of message acknowledgements executed"
}
if opts.Namespace == "" {
opts.Namespace = m.Namespace
}
if opts.Subsystem == "" {
opts.Subsystem = m.Subsystem
}
opts.ConstLabels = mergeLabels(m.ConstLabels, opts.ConstLabels)
h := prometheus.NewCounterVec(opts, metricKeys(operationKey))
m.ack = h
return h
}
func (m *Monitor) buildConsumed(opts prometheus.CounterOpts) *prometheus.CounterVec {
if opts.Name == "" {
opts.Name = "pubsub_message_consumed"
}
if opts.Help == "" {
opts.Help = "Counter consumed next message"
}
if opts.Namespace == "" {
opts.Namespace = m.Namespace
}
if opts.Subsystem == "" {
opts.Subsystem = m.Subsystem
}
opts.ConstLabels = mergeLabels(m.ConstLabels, opts.ConstLabels)
h := prometheus.NewCounterVec(opts, metricKeys())
m.consumed = h
return h
}
func metricKeys(keys ...string) []string {
return append(keys, commonLabels...)
}
func metricLabels(custom map[string]string, consumerName string, msg pubsub.ReceivedMessage, err error) map[string]string {
var version string
var msgName string
if msg != nil {
version = msg.Version()
msgName = msg.Name()
}
labels := map[string]string{
consumerKey: consumerName,
msgNameKey: msgName,
msgVersionKey: version,
errorKey: errorLabel(err),
}
for k, v := range custom {
labels[k] = v
}
return labels
}
func mergeLabels(a, b map[string]string) (merged map[string]string) {
merged = make(map[string]string, len(a)+len(b))
for k, v := range a {
merged[k] = v
}
for k, v := range b {
merged[k] = v
}
return merged
}
func errorLabel(err error) string {
if err != nil {
return "true"
}
return "false"
}