-
Notifications
You must be signed in to change notification settings - Fork 28
/
store_message_metrics.go
58 lines (47 loc) · 1.42 KB
/
store_message_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
package weshnet
import (
"encoding/hex"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"berty.tech/weshnet/v2/internal/queue"
)
const messageMetricNamespace = "bty_store_message"
var (
collectorMessageStoreQueueLength = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: messageMetricNamespace,
Name: "message_queue_length",
Help: "message queue length",
}, []string{"kind", "device_pk"},
)
collectorsMessageStore = []prometheus.Collector{
collectorMessageStoreQueueLength,
}
)
var _ queue.MetricsTracer[*messageItem] = (*messageMetricsTracer)(nil)
type messageMetricsTracer struct {
reg prometheus.Registerer
}
func newMessageMetricsTracer(reg prometheus.Registerer) (mt *messageMetricsTracer) {
mt = &messageMetricsTracer{reg: reg}
for _, collector := range collectorsMessageStore {
if err := reg.Register(collector); err != nil {
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
panic(fmt.Errorf("message metrics errors: %w", err))
}
return
}
}
// reg.MustRegister(collectorsMessageStore...)
return
}
func (s *messageMetricsTracer) ItemQueued(name string, m *messageItem) {
collectorMessageStoreQueueLength.WithLabelValues(
name, hex.EncodeToString(m.headers.DevicePk),
).Inc()
}
func (s *messageMetricsTracer) ItemPop(name string, m *messageItem) {
collectorMessageStoreQueueLength.WithLabelValues(
name, hex.EncodeToString(m.headers.DevicePk),
).Dec()
}