/
metrics.go
94 lines (87 loc) · 2.55 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
package event
import (
"time"
"github.com/prometheus/client_golang/prometheus"
)
// MustRegisterMetrics will register all event related metrics on the given registry.
// If metrics with the same name already exist no the register this function will panic.
func MustRegisterMetrics(registry *prometheus.Registry) {
registry.MustRegister(publishDuration, publishCounter, processCounter, processDuration)
}
// SampledMessageHandler will instrument the given MessageHandler returning a new one
// that samples metrics. These will be `event_process_*` metrics using as `name` the
// given eventName.
func SampledMessageHandler(handler MessageHandler, eventName string) MessageHandler {
return func(msg Message) error {
start := time.Now()
err := handler(msg)
elapsed := time.Since(start)
sampleProcess(eventName, elapsed, err)
return err
}
}
func samplePublish(name string, elapsed time.Duration, err error) {
status := "ok"
if err != nil {
status = "error"
}
labels := prometheus.Labels{
"status": status,
"name": name,
}
publishDuration.With(labels).Observe(elapsed.Seconds())
publishCounter.With(labels).Inc()
}
func sampleProcess(name string, elapsed time.Duration, err error) {
status := "ok"
if err != nil {
status = "error"
}
labels := prometheus.Labels{
"status": status,
"name": name,
}
processDuration.With(labels).Observe(elapsed.Seconds())
processCounter.With(labels).Inc()
}
var (
publishDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "event_publish_duration_seconds",
Help: "Duration of event publish",
// publish times are much smaller since they measure only communication with broker.
Buckets: []float64{
.1, .2, .3, .4, .5, .6, .7, .8, .9, 1,
2, 3, 4, 5, 10, 15, 20, 30,
},
},
[]string{"status", "name"},
)
publishCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "event_publish_total",
Help: "Total of published events",
},
[]string{"status", "name"},
)
processDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "event_process_duration_seconds",
Help: "Duration of event processing",
// processing takes longer and GCP max processing time is 10 minutes
Buckets: []float64{
.1, .2, .3, .4, .5, .6, .7, .8, .9, 1,
2, 3, 4, 5, 10, 15, 20, 30, 60, 90, 120,
180, 240, 300, 360, 420, 480, 540, 600,
},
},
[]string{"status", "name"},
)
processCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "event_process_total",
Help: "Total of processed events",
},
[]string{"status", "name"},
)
)