forked from kbudde/rabbitmq_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexporter_aliveness.go
104 lines (85 loc) · 2.53 KB
/
exporter_aliveness.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 main
import (
"context"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
func init() {
RegisterExporter("aliveness", newExporterAliveness)
}
var (
alivenessLabels = []string{"vhost"}
alivenessGaugeVec = map[string]*prometheus.GaugeVec{
"vhost.aliveness": newGaugeVec("aliveness_test", "vhost aliveness test", alivenessLabels),
}
rabbitmqAlivenessMetric = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "rabbitmq_aliveness_info",
Help: "A metric with value 1 status:ok else 0 labeled by aliveness test status, error, reason",
},
[]string{"status", "error", "reason"},
)
)
type exporterAliveness struct {
alivenessMetrics map[string]*prometheus.GaugeVec
alivenessInfo AlivenessInfo
}
type AlivenessInfo struct {
Status string
Error string
Reason string
}
func newExporterAliveness() Exporter {
alivenessGaugeVecActual := alivenessGaugeVec
if len(config.ExcludeMetrics) > 0 {
for _, metric := range config.ExcludeMetrics {
if alivenessGaugeVecActual[metric] != nil {
delete(alivenessGaugeVecActual, metric)
}
}
}
return &exporterAliveness{
alivenessMetrics: alivenessGaugeVecActual,
alivenessInfo: AlivenessInfo{},
}
}
func (e *exporterAliveness) Collect(ctx context.Context, ch chan<- prometheus.Metric) error {
body, contentType, err := apiRequest(config, "aliveness-test")
if err != nil {
return err
}
reply, err := MakeReply(contentType, body)
if err != nil {
return err
}
rabbitMqAlivenessData := reply.MakeMap()
e.alivenessInfo.Status, _ = reply.GetString("status")
e.alivenessInfo.Error, _ = reply.GetString("error")
e.alivenessInfo.Reason, _ = reply.GetString("reason")
rabbitmqAlivenessMetric.Reset()
var flag float64 = 0
if e.alivenessInfo.Status == "ok" {
flag = 1
}
rabbitmqAlivenessMetric.WithLabelValues(e.alivenessInfo.Status, e.alivenessInfo.Error, e.alivenessInfo.Reason).Set(flag)
log.WithField("alivenesswData", rabbitMqAlivenessData).Debug("Aliveness data")
for key, gauge := range e.alivenessMetrics {
if value, ok := rabbitMqAlivenessData[key]; ok {
log.WithFields(log.Fields{"key": key, "value": value}).Debug("Set aliveness metric for key")
gauge.WithLabelValues(e.alivenessInfo.Status).Set(value)
}
}
if ch != nil {
rabbitmqAlivenessMetric.Collect(ch)
for _, gauge := range e.alivenessMetrics {
gauge.Collect(ch)
}
}
return nil
}
func (e exporterAliveness) Describe(ch chan<- *prometheus.Desc) {
rabbitmqVersionMetric.Describe(ch)
for _, gauge := range e.alivenessMetrics {
gauge.Describe(ch)
}
}