-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexporter.go
99 lines (82 loc) · 2.22 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
package scraper
import (
"log"
"net/http"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Exporter exports stats in prometheus format
type Exporter struct {
metrics Metrics
entries []TargetResponse
}
// NewExporter creates a new exporter
func NewExporter(options Metrics, chSize int) *Exporter {
return &Exporter{
metrics: options,
entries: make([]TargetResponse, 0, chSize),
}
}
// Describe describe the metrics for prometheus
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.metrics.TargetURLStatus.Describe(ch)
e.metrics.TargetURLResponseTime.Describe(ch)
}
// Collect collects data to be consumed by prometheus
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
for _, res := range e.entries {
log.Println("collect: ", res)
e.metrics.TargetURLStatus.
WithLabelValues(res.URL.String()).
Set(float64(res.Status))
e.metrics.TargetURLResponseTime.
WithLabelValues(res.URL.String()).
Observe(float64(res.ResponseTime.Milliseconds()))
}
e.metrics.TargetURLStatus.Collect(ch)
e.metrics.TargetURLResponseTime.Collect(ch)
}
// Metrics is a collection of the url metrics
type Metrics struct {
TargetURLStatus *prometheus.GaugeVec
TargetURLResponseTime *prometheus.HistogramVec
}
// NewMetrics builds a new metric options
func NewMetrics() Metrics {
us := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "sample",
Subsystem: "external",
Name: "url_up",
Help: "URL status",
},
[]string{"url"},
)
uRH := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "sample",
Subsystem: "external",
Name: "url_response_time_ms",
Help: "URL response time in milli seconds",
},
[]string{"url"},
)
metrics := Metrics{
TargetURLStatus: us,
TargetURLResponseTime: uRH,
}
return metrics
}
// PrometheusHandler prometheus metrics handler
func PrometheusHandler() http.Handler {
return promhttp.Handler()
}
// For syncronisation to make sure RegisterExporter is called only once
var once = sync.Once{}
// RegisterExporter registers the exporter with prometheus
func RegisterExporter(e *ScrapePool) {
once.Do(func() {
prometheus.MustRegister(e)
})
}