forked from Jesse0Michael/rmqprom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrmqprom.go
104 lines (94 loc) · 3.12 KB
/
rmqprom.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
package rmqprom
import (
"context"
"time"
"github.com/adjust/rmq/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)
type queueStatsCounters struct {
readyCount prometheus.Gauge
rejectedCount prometheus.Gauge
connectionCount prometheus.Gauge
consumerCount prometheus.Gauge
unackedCount prometheus.Gauge
}
func RecordRmqMetrics(ctx context.Context, connection rmq.Connection, l *logrus.Entry) {
counters := registerCounters(connection)
go func() {
defer func() {
if r := recover(); r != nil {
l.Warn("panic in RecordRmqMetrics", r)
}
}()
for {
select {
case <-ctx.Done():
return
default:
queues, openErr := connection.GetOpenQueues()
if openErr != nil {
l.WithError(openErr).Error("failed to open queues for rmq metrics")
}
stats, statErr := connection.CollectStats(queues)
if statErr != nil {
l.WithError(statErr).Error("failed to collect stats for rmq metrics")
}
for queue, queueStats := range stats.QueueStats {
if counter, ok := counters[queue]; ok {
counter.readyCount.Set(float64(queueStats.ReadyCount))
counter.rejectedCount.Set(float64(queueStats.RejectedCount))
counter.connectionCount.Set(float64(queueStats.ConnectionCount()))
counter.consumerCount.Set(float64(queueStats.ConsumerCount()))
counter.unackedCount.Set(float64(queueStats.UnackedCount()))
}
}
time.Sleep(1 * time.Second)
}
}
}()
}
func registerCounters(connection rmq.Connection) map[string]queueStatsCounters {
counters := map[string]queueStatsCounters{}
queues, _ := connection.GetOpenQueues()
for _, queue := range queues {
counters[queue] = queueStatsCounters{
readyCount: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "rmq",
Name: "ready",
Help: "Number of ready messages on queue",
ConstLabels: prometheus.Labels{"queue": queue},
}),
rejectedCount: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "rmq",
Name: "rejected",
Help: "Number of rejected messages on queue",
ConstLabels: prometheus.Labels{"queue": queue},
}),
connectionCount: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "rmq",
Name: "connection",
Help: "Number of connections consuming a queue",
ConstLabels: prometheus.Labels{"queue": queue},
}),
consumerCount: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "rmq",
Name: "consumer",
Help: "Number of consumers consuming messages for a queue",
ConstLabels: prometheus.Labels{"queue": queue},
}),
unackedCount: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "rmq",
Name: "unacked",
Help: "Number of unacked messages on a consumer",
ConstLabels: prometheus.Labels{"queue": queue},
}),
}
prometheus.MustRegister(counters[queue].readyCount)
prometheus.MustRegister(counters[queue].rejectedCount)
prometheus.MustRegister(counters[queue].connectionCount)
prometheus.MustRegister(counters[queue].consumerCount)
prometheus.MustRegister(counters[queue].unackedCount)
}
return counters
}