forked from criteo/marathon_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexporter.go
468 lines (413 loc) · 12.3 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package main
import (
"errors"
"fmt"
"strings"
"time"
"github.com/jeffail/gabs"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
const defaultNamespace = "marathon"
type Exporter struct {
scraper Scraper
duration prometheus.Gauge
scrapeError prometheus.Gauge
up prometheus.Gauge
totalErrors prometheus.Counter
totalScrapes prometheus.Counter
Counters *CounterContainer
Gauges *GaugeContainer
}
// Describe implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
log.Debugln("Describing metrics")
metricCh := make(chan prometheus.Metric)
doneCh := make(chan struct{})
go func() {
for m := range metricCh {
ch <- m.Desc()
}
close(doneCh)
}()
e.Collect(metricCh)
close(metricCh)
<-doneCh
}
// Collect implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
log.Debugln("Collecting metrics")
e.scrape(ch)
ch <- e.duration
ch <- e.totalScrapes
ch <- e.totalErrors
ch <- e.scrapeError
ch <- e.up
}
func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
e.totalScrapes.Inc()
var err error
defer func(begin time.Time) {
e.duration.Set(time.Since(begin).Seconds())
if err == nil {
e.scrapeError.Set(0)
e.up.Set(1)
} else {
e.totalErrors.Inc()
e.scrapeError.Set(1)
e.up.Set(0)
}
}(time.Now())
// Rebuild gauges & coutners to avoid stale values
e.Gauges = NewGaugeContainer(e.Gauges.namespace)
e.Counters = NewCounterContainer(e.Counters.namespace)
if err = e.exportApps(ch); err != nil {
return
}
if err = e.exportMetrics(ch); err != nil {
return
}
e.Counters.mutex.Lock()
defer e.Counters.mutex.Unlock()
for _, counter := range e.Counters.counters {
counter.Collect(ch)
}
e.Gauges.mutex.Lock()
defer e.Gauges.mutex.Unlock()
for _, gauge := range e.Gauges.gauges {
gauge.Collect(ch)
}
}
func (e *Exporter) exportApps(ch chan<- prometheus.Metric) (err error) {
content, err := e.scraper.Scrape("v2/apps?embed=apps.taskStats")
if err != nil {
log.Debugf("Problem scraping v2/apps endpoint: %v\n", err)
return
}
json, err := gabs.ParseJSON(content)
if err != nil {
log.Debugf("Problem parsing v2/apps response: %v\n", err)
return
}
e.scrapeApps(json, ch)
return
}
func (e *Exporter) exportMetrics(ch chan<- prometheus.Metric) (err error) {
content, err := e.scraper.Scrape("metrics")
if err != nil {
log.Debugf("Problem scraping metrics endpoint: %v\n", err)
return
}
json, err := gabs.ParseJSON(content)
if err != nil {
log.Debugf("Problem parsing metrics response: %v\n", err)
return
}
e.scrapeMetrics(json, ch)
return
}
func (e *Exporter) scrapeApps(json *gabs.Container, ch chan<- prometheus.Metric) {
elements, _ := json.S("apps").Children()
states := map[string]string{
"running": "tasksRunning",
"staged": "tasksStaged",
"healthy": "tasksHealthy",
"unhealthy": "tasksUnhealthy",
"cpus": "cpus",
"mem_in_mb": "mem",
"disk_in_mb": "disk",
"gpus": "gpus",
"avg_uptime": "taskStats.startedAfterLastScaling.stats.lifeTime.averageSeconds",
}
name := "app_instances"
gauge, new := e.Gauges.Fetch(name, "Marathon app instance count", "app", "app_version")
if new {
log.Infof("Added gauge %q\n", name)
}
gauge.Reset()
for _, app := range elements {
id := app.Path("id").Data().(string)
version := app.Path("version").Data().(string)
data := app.Path("instances").Data()
count, ok := data.(float64)
if !ok {
log.Debugf(fmt.Sprintf("Bad conversion! Unexpected value \"%v\" for number of app instances\n", data))
continue
}
gauge.WithLabelValues(id, version).Set(count)
for key, value := range states {
name := fmt.Sprintf("app_task_%s", key)
gauge, new := e.Gauges.Fetch(name, fmt.Sprintf("Marathon app task %s count", key), "app", "app_version")
if new {
log.Infof("Added gauge %q\n", name)
}
data := app.Path(value).Data()
count, ok := data.(float64)
if !ok {
log.Debugf(fmt.Sprintf("Bad conversion! Unexpected value \"%v\" for number of \"%s\" tasks\n", data, key))
continue
}
gauge.WithLabelValues(id, version).Set(count)
}
}
}
func (e *Exporter) scrapeMetrics(json *gabs.Container, ch chan<- prometheus.Metric) {
elements, _ := json.ChildrenMap()
for key, element := range elements {
switch key {
case "message":
log.Errorf("Problem collecting metrics: %s\n", element.Data().(string))
return
case "version":
data := element.Data()
version, ok := data.(string)
if !ok {
log.Errorf(fmt.Sprintf("Bad conversion! Unexpected value \"%v\" for version\n", data))
} else {
gauge, _ := e.Gauges.Fetch("metrics_version", "Marathon metrics version", "version")
gauge.WithLabelValues(version).Set(1)
gauge.Collect(ch)
}
case "counters":
e.scrapeCounters(element)
case "gauges":
e.scrapeGauges(element)
case "histograms":
e.scrapeHistograms(element)
case "meters":
e.scrapeMeters(element)
case "timers":
e.scrapeTimers(element)
}
}
}
func (e *Exporter) scrapeCounters(json *gabs.Container) {
elements, _ := json.ChildrenMap()
for key, element := range elements {
new, err := e.scrapeCounter(key, element)
if err != nil {
log.Debug(err)
} else if new {
log.Infof("Added counter %q\n", key)
}
}
}
func (e *Exporter) scrapeCounter(key string, json *gabs.Container) (bool, error) {
data := json.Path("count").Data()
count, ok := data.(float64)
if !ok {
return false, errors.New(fmt.Sprintf("Bad conversion! Unexpected value \"%v\" for counter %s\n", data, key))
}
name := renameMetric(key)
help := fmt.Sprintf(counterHelp, key)
counter, new := e.Counters.Fetch(name, help)
counter.WithLabelValues().Set(count)
return new, nil
}
func (e *Exporter) scrapeGauges(json *gabs.Container) {
elements, _ := json.ChildrenMap()
for key, element := range elements {
new, err := e.scrapeGauge(key, element)
if err != nil {
log.Debug(err)
} else if new {
log.Infof("Added gauge %q\n", key)
}
}
}
func (e *Exporter) scrapeGauge(key string, json *gabs.Container) (bool, error) {
data := json.Path("value").Data()
value, ok := data.(float64)
if !ok {
return false, errors.New(fmt.Sprintf("Bad conversion! Unexpected value \"%v\" for gauge %s\n", data, key))
}
name := renameMetric(key)
help := fmt.Sprintf(gaugeHelp, key)
gauge, new := e.Gauges.Fetch(name, help)
gauge.WithLabelValues().Set(value)
return new, nil
}
func (e *Exporter) scrapeMeters(json *gabs.Container) {
elements, _ := json.ChildrenMap()
for key, element := range elements {
new, err := e.scrapeMeter(key, element)
if err != nil {
log.Debug(err)
} else if new {
log.Infof("Added meter %q\n", key)
}
}
}
func (e *Exporter) scrapeMeter(key string, json *gabs.Container) (bool, error) {
count, ok := json.Path("count").Data().(float64)
if !ok {
return false, errors.New(fmt.Sprintf("Bad meter! %s has no count\n", key))
}
units, ok := json.Path("units").Data().(string)
if !ok {
return false, errors.New(fmt.Sprintf("Bad meter! %s has no units\n", key))
}
name := renameMetric(key)
help := fmt.Sprintf(meterHelp, key, units)
counter, new := e.Counters.Fetch(name+"_count", help)
counter.WithLabelValues().Set(count)
gauge, _ := e.Gauges.Fetch(name, help, "rate")
properties, _ := json.ChildrenMap()
for key, property := range properties {
if strings.Contains(key, "rate") {
if value, ok := property.Data().(float64); ok {
gauge.WithLabelValues(renameRate(key)).Set(value)
}
}
}
return new, nil
}
func (e *Exporter) scrapeHistograms(json *gabs.Container) {
elements, _ := json.ChildrenMap()
for key, element := range elements {
new, err := e.scrapeHistogram(key, element)
if err != nil {
log.Debug(err)
} else if new {
log.Infof("Added histogram %q\n", key)
}
}
}
func (e *Exporter) scrapeHistogram(key string, json *gabs.Container) (bool, error) {
count, ok := json.Path("count").Data().(float64)
if !ok {
return false, errors.New(fmt.Sprintf("Bad historgram! %s has no count\n", key))
}
name := renameMetric(key)
help := fmt.Sprintf(histogramHelp, key)
counter, new := e.Counters.Fetch(name+"_count", help)
counter.WithLabelValues().Set(count)
percentiles, _ := e.Gauges.Fetch(name, help, "percentile")
max, _ := e.Gauges.Fetch(name+"_max", help)
mean, _ := e.Gauges.Fetch(name+"_mean", help)
min, _ := e.Gauges.Fetch(name+"_min", help)
stddev, _ := e.Gauges.Fetch(name+"_stddev", help)
properties, _ := json.ChildrenMap()
for key, property := range properties {
switch key {
case "p50", "p75", "p95", "p98", "p99", "p999":
if value, ok := property.Data().(float64); ok {
percentiles.WithLabelValues("0." + key[1:]).Set(value)
}
case "min":
if value, ok := property.Data().(float64); ok {
min.WithLabelValues().Set(value)
}
case "max":
if value, ok := property.Data().(float64); ok {
max.WithLabelValues().Set(value)
}
case "mean":
if value, ok := property.Data().(float64); ok {
mean.WithLabelValues().Set(value)
}
case "stddev":
if value, ok := property.Data().(float64); ok {
stddev.WithLabelValues().Set(value)
}
}
}
return new, nil
}
func (e *Exporter) scrapeTimers(json *gabs.Container) {
elements, _ := json.ChildrenMap()
for key, element := range elements {
new, err := e.scrapeTimer(key, element)
if err != nil {
log.Debug(err)
} else if new {
log.Infof("Added timer %q\n", key)
}
}
}
func (e *Exporter) scrapeTimer(key string, json *gabs.Container) (bool, error) {
count, ok := json.Path("count").Data().(float64)
if !ok {
return false, errors.New(fmt.Sprintf("Bad timer! %s has no count\n", key))
}
units, ok := json.Path("rate_units").Data().(string)
if !ok {
return false, errors.New(fmt.Sprintf("Bad timer! %s has no units\n", key))
}
name := renameMetric(key)
help := fmt.Sprintf(timerHelp, key, units)
counter, new := e.Counters.Fetch(name+"_count", help)
counter.WithLabelValues().Set(count)
rates, _ := e.Gauges.Fetch(name+"_rate", help, "rate")
percentiles, _ := e.Gauges.Fetch(name, help, "percentile")
min, _ := e.Gauges.Fetch(name+"_min", help)
max, _ := e.Gauges.Fetch(name+"_max", help)
mean, _ := e.Gauges.Fetch(name+"_mean", help)
stddev, _ := e.Gauges.Fetch(name+"_stddev", help)
properties, _ := json.ChildrenMap()
for key, property := range properties {
switch key {
case "mean_rate", "m1_rate", "m5_rate", "m15_rate":
if value, ok := property.Data().(float64); ok {
rates.WithLabelValues(renameRate(key)).Set(value)
}
case "p50", "p75", "p95", "p98", "p99", "p999":
if value, ok := property.Data().(float64); ok {
percentiles.WithLabelValues("0." + key[1:]).Set(value)
}
case "min":
if value, ok := property.Data().(float64); ok {
min.WithLabelValues().Set(value)
}
case "max":
if value, ok := property.Data().(float64); ok {
max.WithLabelValues().Set(value)
}
case "mean":
if value, ok := property.Data().(float64); ok {
mean.WithLabelValues().Set(value)
}
case "stddev":
if value, ok := property.Data().(float64); ok {
stddev.WithLabelValues().Set(value)
}
}
}
return new, nil
}
func NewExporter(s Scraper, namespace string) *Exporter {
return &Exporter{
scraper: s,
Counters: NewCounterContainer(namespace),
Gauges: NewGaugeContainer(namespace),
duration: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "exporter",
Name: "last_scrape_duration_seconds",
Help: "Duration of the last scrape of metrics from Marathon.",
}),
up: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "up",
Help: "Whether the last scrape of metrics from Marathon resulted in an error (0 for error, 1 for success).",
}),
scrapeError: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "exporter",
Name: "last_scrape_error",
Help: "Whether the last scrape of metrics from Marathon resulted in an error (1 for error, 0 for success).",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "exporter",
Name: "scrapes_total",
Help: "Total number of times Marathon was scraped for metrics.",
}),
totalErrors: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "exporter",
Name: "errors_total",
Help: "Total number of times the exporter experienced errors collecting Marathon metrics.",
}),
}
}