/
instrumentation.go
60 lines (49 loc) · 1.43 KB
/
instrumentation.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
package metrics
import (
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/prometheus/client_golang/prometheus"
)
const (
storeError = "storeError"
success = "success"
)
var defaultInstrumentation = instrumentation{
messageMetric: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "received_messages",
Help: "received messages per topic and status",
}, []string{"status", "topic"},
),
connectedMetric: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "mqtt2prometheus_connected",
Help: "is the mqtt2prometheus exporter connected to the broker",
},
),
}
type instrumentation struct {
messageMetric *prometheus.CounterVec
connectedMetric prometheus.Gauge
}
func (i *instrumentation) Collector() prometheus.Collector {
return i
}
func (i *instrumentation) Describe(desc chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(i, desc)
}
func (i *instrumentation) Collect(metrics chan<- prometheus.Metric) {
i.connectedMetric.Collect(metrics)
i.messageMetric.Collect(metrics)
}
func (i *instrumentation) CountSuccess(topic string) {
i.messageMetric.WithLabelValues(success, topic).Inc()
}
func (i *instrumentation) CountStoreError(topic string) {
i.messageMetric.WithLabelValues(storeError, topic).Inc()
}
func (i *instrumentation) ConnectionLostHandler(client mqtt.Client, err error) {
i.connectedMetric.Set(0)
}
func (i *instrumentation) OnConnectHandler(client mqtt.Client) {
i.connectedMetric.Set(1)
}