-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
43 lines (37 loc) · 1.05 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
package main
import (
"fmt"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type Metrics struct {
reg prometheus.Registerer
metric *prometheus.CounterVec
updater chan MetricUpdate
}
type MetricUpdate struct {
Reason string
Value float64
}
func (m *Metrics) start() {
for u := range m.updater {
c, err := m.metric.GetMetricWith(prometheus.Labels{"reason": u.Reason})
if err != nil {
log.Printf("Error getting a label with reason %s: %s", u.Reason, err)
continue
}
c.Add(u.Value)
}
}
func addMetrics(port int) chan<- MetricUpdate {
m := Metrics{reg: prometheus.NewRegistry(), metric: prometheus.NewCounterVec(prometheus.CounterOpts{Name: "restarts"}, []string{"reason"}), updater: make(chan MetricUpdate)}
m.reg.MustRegister(m.metric)
http.Handle("/metrics", promhttp.HandlerFor(m.reg.(prometheus.Gatherer), promhttp.HandlerOpts{Registry: m.reg}))
if port > 0 {
go http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil)
}
go m.start()
return m.updater
}