forked from Syncano/rabbitmq_exporter
-
Notifications
You must be signed in to change notification settings - Fork 197
/
exporter.go
164 lines (137 loc) · 5.19 KB
/
exporter.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"context"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
var (
exportersMu sync.RWMutex
exporterFactories = make(map[string]func() Exporter)
)
type contextValues string
const (
endpointScrapeDuration contextValues = "endpointScrapeDuration"
endpointUpMetric contextValues = "endpointUpMetric"
nodeName contextValues = "node"
clusterName contextValues = "cluster"
totalQueues contextValues = "totalQueues"
)
//RegisterExporter makes an exporter available by the provided name.
func RegisterExporter(name string, f func() Exporter) {
exportersMu.Lock()
defer exportersMu.Unlock()
if f == nil {
panic("exporterFactory is nil")
}
exporterFactories[name] = f
}
type exporter struct {
mutex sync.RWMutex
upMetric *prometheus.GaugeVec
endpointUpMetric *prometheus.GaugeVec
endpointScrapeDurationMetric *prometheus.GaugeVec
exporter map[string]Exporter
overviewExporter *exporterOverview
lastScrapeOK bool
}
//Exporter interface for prometheus metrics. Collect is fetching the data and therefore can return an error
type Exporter interface {
Collect(ctx context.Context, ch chan<- prometheus.Metric) error
Describe(ch chan<- *prometheus.Desc)
}
func newExporter() *exporter {
enabledExporter := make(map[string]Exporter)
for _, e := range config.EnabledExporters {
if _, ok := exporterFactories[e]; ok {
enabledExporter[e] = exporterFactories[e]()
}
}
return &exporter{
upMetric: newGaugeVec("up", "Was the last scrape of rabbitmq successful.", []string{"cluster", "node"}),
endpointUpMetric: newGaugeVec("module_up", "Was the last scrape of rabbitmq successful per module.", []string{"cluster", "node", "module"}),
endpointScrapeDurationMetric: newGaugeVec("module_scrape_duration_seconds", "Duration of the last scrape in seconds", []string{"cluster", "node", "module"}),
exporter: enabledExporter,
overviewExporter: newExporterOverview(),
lastScrapeOK: true, //return true after start. Value will be updated with each scraping
}
}
func (e *exporter) LastScrapeOK() bool {
e.mutex.Lock() // To protect metrics from concurrent collects.
defer e.mutex.Unlock()
return e.lastScrapeOK
}
func (e *exporter) Describe(ch chan<- *prometheus.Desc) {
e.overviewExporter.Describe(ch)
for _, ex := range e.exporter {
ex.Describe(ch)
}
e.upMetric.Describe(ch)
e.endpointUpMetric.Describe(ch)
e.endpointScrapeDurationMetric.Describe(ch)
BuildInfo.Describe(ch)
}
func (e *exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock() // To protect metrics from concurrent collects.
defer e.mutex.Unlock()
e.upMetric.Reset()
e.endpointUpMetric.Reset()
e.endpointScrapeDurationMetric.Reset()
start := time.Now()
allUp := true
if err := e.collectWithDuration(e.overviewExporter, "overview", ch); err != nil {
log.WithError(err).Warn("retrieving overview failed")
allUp = false
}
for name, ex := range e.exporter {
if err := e.collectWithDuration(ex, name, ch); err != nil {
log.WithError(err).Warn("retrieving " + name + " failed")
allUp = false
}
}
BuildInfo.Collect(ch)
if allUp {
e.upMetric.WithLabelValues(e.overviewExporter.NodeInfo().ClusterName, e.overviewExporter.NodeInfo().Node).Set(1)
} else {
e.upMetric.WithLabelValues(e.overviewExporter.NodeInfo().ClusterName, e.overviewExporter.NodeInfo().Node).Set(0)
}
e.lastScrapeOK = allUp
if e.overviewExporter.NodeInfo().ClusterName != "" && e.overviewExporter.NodeInfo().Node != "" {
e.upMetric.DeleteLabelValues("", "")
}
e.upMetric.Collect(ch)
e.endpointUpMetric.Collect(ch)
e.endpointScrapeDurationMetric.Collect(ch)
log.WithField("duration", time.Since(start)).Info("Metrics updated")
}
func (e *exporter) collectWithDuration(ex Exporter, name string, ch chan<- prometheus.Metric) error {
ctx := context.Background()
ctx = context.WithValue(ctx, endpointScrapeDuration, e.endpointScrapeDurationMetric)
ctx = context.WithValue(ctx, endpointUpMetric, e.endpointUpMetric)
//use last know value, could be outdated or empty
ctx = context.WithValue(ctx, nodeName, e.overviewExporter.NodeInfo().Node)
ctx = context.WithValue(ctx, clusterName, e.overviewExporter.NodeInfo().ClusterName)
ctx = context.WithValue(ctx, totalQueues, e.overviewExporter.NodeInfo().TotalQueues)
startModule := time.Now()
err := ex.Collect(ctx, ch)
//use current data
node := e.overviewExporter.NodeInfo().Node
cluster := e.overviewExporter.NodeInfo().ClusterName
if scrapeDuration, ok := ctx.Value(endpointScrapeDuration).(*prometheus.GaugeVec); ok {
if cluster != "" && node != "" { //values are not available until first scrape of overview succeeded
scrapeDuration.WithLabelValues(cluster, node, name).Set(time.Since(startModule).Seconds())
}
}
if up, ok := ctx.Value(endpointUpMetric).(*prometheus.GaugeVec); ok {
if err != nil {
up.WithLabelValues(cluster, node, name).Set(0)
} else {
up.WithLabelValues(cluster, node, name).Set(1)
}
if node != "" && cluster != "" {
up.DeleteLabelValues("", "", name)
}
}
return err
}