-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprometheusFeeder.go
85 lines (75 loc) · 1.87 KB
/
prometheusFeeder.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
package main
import (
"strings"
"time"
prom "github.com/prometheus/client_golang/prometheus"
)
type prometheusFeeder struct {
environment string
ticker *time.Ticker
controller controller
}
func newPrometheusFeeder(environment string, controller controller) *prometheusFeeder {
ticker := time.NewTicker(60 * time.Second)
return &prometheusFeeder{
environment: environment,
ticker: ticker,
controller: controller,
}
}
func (p prometheusFeeder) feed() {
ignitePilotLight(p.environment)
serviceStatus := initServiceStatusMetrics()
for range p.ticker.C {
p.recordMetrics(serviceStatus)
}
}
func (p prometheusFeeder) recordMetrics(serviceStatus *prom.GaugeVec) {
for _, service := range p.controller.getMeasuredServices() {
select {
case checkResult := <-service.cachedHealthMetric.toReadFromCache:
name := strings.Replace(checkResult.Name, ".", "-", -1)
checkStatus := inverseBoolToFloat64(checkResult.Ok)
serviceStatus.
With(prom.Labels{"environment": p.environment, "service": name}).
Set(checkStatus)
default:
continue
}
}
}
func initServiceStatusMetrics() *prom.GaugeVec {
serviceStatus := prom.NewGaugeVec(
prom.GaugeOpts{
Namespace: "upp",
Subsystem: "health",
Name: "servicestatus",
Help: "Status of the service: 0 - healthy; 1 - unhealthy",
},
[]string{
"environment",
"service",
})
prom.MustRegister(serviceStatus)
return serviceStatus
}
func ignitePilotLight(environment string) {
pilotLight := prom.NewGaugeVec(
prom.GaugeOpts{
Namespace: "upp",
Subsystem: "health",
Name: "pilotlight",
Help: "Pilot light for the service monitoring UPP service health",
},
[]string{
"environment",
})
prom.MustRegister(pilotLight)
pilotLight.With(prom.Labels{"environment": environment}).Set(1)
}
func inverseBoolToFloat64(b bool) float64 {
if b {
return 0
}
return 1
}