-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexporter.go
63 lines (49 loc) · 1.17 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
package main
import (
"sync"
log "github.com/Sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
)
type exporter struct {
mutex sync.RWMutex
metrics map[string]prometheus.Gauge
upMetric prometheus.Gauge
}
func newExporter() *exporter {
return &exporter{
metrics: metricDescription,
upMetric: upMetricDescription,
}
}
func (e *exporter) fetchATS() {
ATSData, err := getMetricMap(config)
if err != nil {
e.upMetric.Set(0)
} else {
e.upMetric.Set(1)
}
for key, gauge := range e.metrics {
if value, ok := ATSData[key]; ok {
log.WithFields(log.Fields{"key": key, "value": value}).Debug("Set metric for key")
gauge.Set(value)
} else {
log.WithFields(log.Fields{"key": key}).Warn("data not found")
}
}
log.Info("Metrics updated successfully.")
}
func (e *exporter) Describe(ch chan<- *prometheus.Desc) {
for _, gauge := range e.metrics {
gauge.Describe(ch)
}
}
func (e *exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock() // To protect metrics from concurrent collects.
defer e.mutex.Unlock()
e.fetchATS()
e.upMetric.Collect(ch)
for _, gauge := range e.metrics {
gauge.Collect(ch)
}
BuildInfo.Collect(ch)
}