forked from tommyulfsparre/aurora_exporter
/
main.go
162 lines (132 loc) · 3.14 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net"
"net/http"
"sync"
"time"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
)
const namespace = "aurora"
var (
addr = flag.String("web.listen-address", ":9113", "Address to listen on for web interface and telemetry.")
auroraURL = flag.String("exporter.aurora-url", "http://127.0.0.1:8081", "URL to an Aurora scheduler or ZooKeeper ensemble")
metricPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
)
var noLables = []string{}
var httpClient = http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: 2,
ResponseHeaderTimeout: 10 * time.Second,
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 10 * time.Second,
}).Dial,
},
}
type exporter struct {
sync.Mutex
f finder
errors prometheus.Counter
duration prometheus.Gauge
}
func newAuroraExporter(f finder) *exporter {
return &exporter{
f: f,
errors: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "exporter_scrape_errors_total",
Help: "Total scrape errors",
}),
duration: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "exporter_last_scrape_duration_seconds",
Help: "The last scrape duration",
}),
}
}
func (e *exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.duration.Desc()
ch <- e.errors.Desc()
}
func (e *exporter) Collect(ch chan<- prometheus.Metric) {
e.Lock()
defer e.Unlock()
metricsChan := make(chan prometheus.Metric)
go e.scrape(metricsChan)
for metric := range metricsChan {
ch <- metric
}
ch <- e.errors
ch <- e.duration
}
func (e *exporter) scrape(ch chan<- prometheus.Metric) {
defer close(ch)
now := time.Now().UnixNano()
defer func() {
e.duration.Set(float64(time.Now().UnixNano()-now) / 1000000000)
}()
recordErr := func(err error) {
glog.Warning(err)
e.errors.Inc()
}
url, err := e.f.leaderURL()
if err != nil {
recordErr(err)
return
}
varsURL := fmt.Sprintf("%s/vars.json", url)
resp, err := httpClient.Get(varsURL)
if err != nil {
recordErr(err)
return
}
defer resp.Body.Close()
var vars map[string]interface{}
if err = json.NewDecoder(resp.Body).Decode(&vars); err != nil {
recordErr(err)
return
}
for name, v := range vars {
v, ok := v.(float64)
if !ok {
continue
}
if desc, ok := counters[name]; ok {
ch <- prometheus.MustNewConstMetric(
desc,
prometheus.CounterValue,
v, noLables...,
)
}
if desc, ok := gauges[name]; ok {
ch <- prometheus.MustNewConstMetric(
desc,
prometheus.GaugeValue,
v, noLables...,
)
}
labelVars(ch, name, v)
}
}
func main() {
flag.Parse()
finder, err := newFinder(*auroraURL)
if err != nil {
log.Fatal(err)
}
exporter := newAuroraExporter(finder)
prometheus.MustRegister(exporter)
http.Handle(*metricPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, *metricPath, http.StatusMovedPermanently)
})
glog.Info("starting aurora_exporter on ", *addr)
log.Fatal(http.ListenAndServe(*addr, nil))
}