-
Notifications
You must be signed in to change notification settings - Fork 3
/
metrics.go
140 lines (118 loc) · 3.77 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
package bunnify
import (
"errors"
"github.com/prometheus/client_golang/prometheus"
)
const (
queue = "queue"
exchange = "exchange"
result = "result"
routingKey = "routing_key"
)
var (
eventReceivedCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "amqp_events_received",
Help: "Count of AMQP events received",
}, []string{queue, routingKey},
)
eventWithoutHandlerCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "amqp_events_without_handler",
Help: "Count of AMQP events without a handler",
}, []string{queue, routingKey},
)
eventNotParsableCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "amqp_events_not_parsable",
Help: "Count of AMQP events that could not be parsed",
}, []string{queue, routingKey},
)
eventNackCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "amqp_events_nack",
Help: "Count of AMQP events that were not acknowledged",
}, []string{queue, routingKey},
)
eventAckCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "amqp_events_ack",
Help: "Count of AMQP events that were acknowledged",
}, []string{queue, routingKey},
)
eventProcessedDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "amqp_events_processed_duration",
Help: "Milliseconds taken to process an event",
Buckets: []float64{100, 200, 500, 1000, 3000, 5000, 10000},
}, []string{queue, routingKey, result},
)
eventPublishSucceedCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "amqp_events_publish_succeed",
Help: "Count of AMQP events that could be published successfully",
}, []string{exchange, routingKey},
)
eventPublishFailedCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "amqp_events_publish_failed",
Help: "Count of AMQP events that could not be published",
}, []string{exchange, routingKey},
)
)
func eventReceived(queue string, routingKey string) {
eventReceivedCounter.WithLabelValues(queue, routingKey).Inc()
}
func eventWithoutHandler(queue string, routingKey string) {
eventWithoutHandlerCounter.WithLabelValues(queue, routingKey).Inc()
}
func eventNotParsable(queue string, routingKey string) {
eventNotParsableCounter.WithLabelValues(queue, routingKey).Inc()
}
func eventNack(queue string, routingKey string, milliseconds int64) {
eventNackCounter.WithLabelValues(queue, routingKey).Inc()
eventProcessedDuration.
WithLabelValues(queue, routingKey, "NACK").
Observe(float64(milliseconds))
}
func eventAck(queue string, routingKey string, milliseconds int64) {
eventAckCounter.WithLabelValues(queue, routingKey).Inc()
eventProcessedDuration.
WithLabelValues(queue, routingKey, "ACK").
Observe(float64(milliseconds))
}
func eventPublishSucceed(exchange string, routingKey string) {
eventPublishSucceedCounter.WithLabelValues(exchange, routingKey).Inc()
}
func eventPublishFailed(exchange string, routingKey string) {
eventPublishFailedCounter.WithLabelValues(exchange, routingKey).Inc()
}
func InitMetrics(registerer prometheus.Registerer) error {
collectors := []prometheus.Collector{
eventReceivedCounter,
eventAckCounter,
eventNackCounter,
eventWithoutHandlerCounter,
eventNotParsableCounter,
eventProcessedDuration,
eventPublishSucceedCounter,
eventPublishFailedCounter,
}
for _, collector := range collectors {
mv, ok := collector.(metricResetter)
if ok {
mv.Reset()
}
err := registerer.Register(collector)
if err != nil && !errors.As(err, &prometheus.AlreadyRegisteredError{}) {
return err
}
}
return nil
}
// CounterVec, MetricVec and HistogramVec have a Reset func
// in order not to cast to each specific type, metricResetter can
// be used to just get access to the Reset func
type metricResetter interface {
Reset()
}