/
exporter_metrics.go
59 lines (51 loc) · 1.4 KB
/
exporter_metrics.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
package main
import (
"time"
"github.com/prometheus/client_golang/prometheus"
)
type scrapeObserver struct {
scrapeDuration prometheus.Histogram
scrapeSuccess prometheus.Gauge
}
func newScrapeObserver() *scrapeObserver {
ns := "sagemcom_fast_exporter"
return &scrapeObserver{
scrapeDuration: prometheus.NewHistogram(
prometheus.HistogramOpts{
Namespace: ns,
Name: "collector_scrape_duration_seconds",
Help: "Duration of a collector scrape.",
Buckets: prometheus.DefBuckets,
// Enable native histograms, with the factor suggested in the docs
NativeHistogramBucketFactor: 1.1,
// OTel default
NativeHistogramMaxBucketNumber: 160,
// Reset buckets every 24 hours
NativeHistogramMinResetDuration: 24 * time.Hour,
},
),
scrapeSuccess: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: ns,
Name: "collector_scrape_success",
Help: "Whether a collection succeeded.",
},
),
}
}
func (o *scrapeObserver) Observe(duration time.Duration, success bool) {
o.scrapeDuration.Observe(duration.Seconds())
if success {
o.scrapeSuccess.Set(1)
} else {
o.scrapeSuccess.Set(0)
}
}
func (o *scrapeObserver) Describe(ch chan<- *prometheus.Desc) {
ch <- o.scrapeDuration.Desc()
ch <- o.scrapeSuccess.Desc()
}
func (o *scrapeObserver) Collect(ch chan<- prometheus.Metric) {
o.scrapeDuration.Collect(ch)
o.scrapeSuccess.Collect(ch)
}