forked from infinityworks/prometheus-rancher-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
58 lines (46 loc) · 1.35 KB
/
prometheus.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 main
import (
"strings"
"github.com/prometheus/client_golang/prometheus"
)
// Resets the guageVecs back to 0
// Ensures we start from a clean sheet
func (e *Exporter) resetGaugeVecs() {
for _, m := range e.gaugeVecs {
m.Reset()
}
}
// Describe describes all the metrics ever exported by the Rancher exporter
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
for _, m := range e.gaugeVecs {
m.Describe(ch)
}
}
// Collect function, called on by Prometheus Client library
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock() // To protect metrics from concurrent collects.
defer e.mutex.Unlock()
e.resetGaugeVecs() // Clean starting point
var endpointOfAPI []string
if strings.HasSuffix(rancherURL, "v3") || strings.HasSuffix(rancherURL, "v3/") {
endpointOfAPI = endpointsV3
} else {
endpointOfAPI = endpoints
}
// Range over the pre-configured endpoints array
for _, p := range endpointOfAPI {
var data, err = e.gatherData(e.rancherURL, e.resourceLimit, e.accessKey, e.secretKey, p, ch)
if err != nil {
log.Error("Error getting JSON from URL ", p)
return
}
if err := e.processMetrics(data, p, e.hideSys, ch); err != nil {
log.Errorf("Error scraping rancher url: %s", err)
return
}
log.Infof("Metrics successfully processed for %s", p)
}
for _, m := range e.gaugeVecs {
m.Collect(ch)
}
}