-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.go
73 lines (60 loc) · 2.31 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"github.com/prometheus/client_golang/prometheus"
)
// PromCollectors has instances of Prometheus Collectors
type PromCollectors struct {
count *prometheus.CounterVec
error *prometheus.CounterVec
code *prometheus.GaugeVec
duration *prometheus.HistogramVec
length *prometheus.GaugeVec
hash *prometheus.GaugeVec
registerer prometheus.Registerer
}
// Register registers all collectors
func (promCollectors *PromCollectors) Register(registerer prometheus.Registerer) {
promCollectors.registerer = registerer
promCollectors.count = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "sitest_count",
Help: "Total number of performed check",
}, []string{"site"})
registerer.MustRegister(promCollectors.count)
promCollectors.error = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "sitest_error",
Help: "Total number of error",
}, []string{"site"})
registerer.MustRegister(promCollectors.error)
promCollectors.code = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "sitest_code",
Help: "Response code",
}, []string{"site"})
registerer.MustRegister(promCollectors.code)
promCollectors.duration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "sitest_duration_seconds",
Help: "Histogram of request duration",
}, []string{"site"})
registerer.MustRegister(promCollectors.duration)
promCollectors.length = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "sitest_length",
Help: "Page length",
}, []string{"site"})
registerer.MustRegister(promCollectors.length)
promCollectors.hash = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "sitest_hash",
Help: "Page hash",
}, []string{"site"})
registerer.MustRegister(promCollectors.hash)
}
// Update copied values from latest measurements to Prometheus collectors
func (promCollectors *PromCollectors) Update(site string, result Result, err error) {
siteLabels := prometheus.Labels{"site": site}
promCollectors.count.With(siteLabels).Inc()
promCollectors.code.With(siteLabels).Set(float64(result.StatusCode))
promCollectors.duration.With(siteLabels).Observe(result.Duration.Seconds())
promCollectors.length.With(siteLabels).Set(float64(result.Length))
promCollectors.hash.With(siteLabels).Set(float64(result.Hash))
if err != nil {
promCollectors.error.With(siteLabels).Inc()
}
}