forked from kbudde/rabbitmq_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.go
84 lines (67 loc) · 1.7 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
package main
import (
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
var (
exportersMu sync.RWMutex
exporterFactories = make(map[string]func() Exporter)
)
//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.Gauge
exporter []Exporter
}
//Exporter interface for prometheus metrics. Collect is fetching the data and therefore can return an error
type Exporter interface {
Collect(ch chan<- prometheus.Metric) error
Describe(ch chan<- *prometheus.Desc)
}
func newExporter() *exporter {
enabledExporter := []Exporter{}
for _, e := range config.EnabledExporters {
enabledExporter = append(enabledExporter, exporterFactories[e]())
}
return &exporter{
upMetric: newGauge("up", "Was the last scrape of rabbitmq successful."),
exporter: enabledExporter,
}
}
func (e *exporter) Describe(ch chan<- *prometheus.Desc) {
for _, ex := range e.exporter {
ex.Describe(ch)
}
e.upMetric.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()
start := time.Now()
allUp := true
for _, ex := range e.exporter {
err := ex.Collect(ch)
if err != nil {
allUp = false
}
}
BuildInfo.Collect(ch)
if allUp {
e.upMetric.Set(1)
} else {
e.upMetric.Set(0)
}
e.upMetric.Collect(ch)
log.WithField("duration", time.Since(start)).Info("Metrics updated")
}