/
health.go
239 lines (202 loc) · 4.98 KB
/
health.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package services
import (
"errors"
"fmt"
"maps"
"runtime/debug"
"sync"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
// CopyHealth copies health statuses from src to dest. Useful when implementing HealthReporter.HealthReport.
// If duplicate names are encountered, the errors are joined, unless testing in which case a panic is thrown.
func CopyHealth(dest, src map[string]error) {
for name, err := range src {
errOrig, ok := dest[name]
if ok {
if testing.Testing() {
panic("service names must be unique: duplicate name: " + name)
}
if errOrig != nil {
dest[name] = errors.Join(errOrig, err)
continue
}
}
dest[name] = err
}
}
// HealthChecker is a services.Service which monitors other services and can be probed for system health.
type HealthChecker struct {
StateMachine
chStop chan struct{}
chDone chan struct{}
ver, sha string
servicesMu sync.RWMutex
services map[string]HealthReporter
stateMu sync.RWMutex
healthy map[string]error
ready map[string]error
}
const interval = 15 * time.Second
var (
healthStatus = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "health",
Help: "Health status by service",
},
[]string{"service_id"},
)
uptimeSeconds = promauto.NewCounter(
prometheus.CounterOpts{
Name: "uptime_seconds",
Help: "Uptime of the application measured in seconds",
},
)
version = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "version",
Help: "Application version information",
},
[]string{"version", "commit"},
)
)
func NewChecker(ver, sha string) *HealthChecker {
if ver == "" || sha == "" {
if bi, ok := debug.ReadBuildInfo(); ok {
if ver == "" {
ver = bi.Main.Version
}
if sha == "" {
sha = bi.Main.Sum
}
}
}
if len(sha) > 7 {
sha = sha[:7]
}
return &HealthChecker{
ver: ver,
sha: sha,
services: make(map[string]HealthReporter, 10),
healthy: make(map[string]error, 10),
ready: make(map[string]error, 10),
chStop: make(chan struct{}),
chDone: make(chan struct{}),
}
}
func (c *HealthChecker) Start() error {
return c.StartOnce("HealthCheck", func() error {
version.WithLabelValues(c.ver, c.sha).Inc()
// update immediately
c.update()
go c.run()
return nil
})
}
func (c *HealthChecker) Close() error {
return c.StopOnce("HealthCheck", func() error {
close(c.chStop)
<-c.chDone
return nil
})
}
func (c *HealthChecker) run() {
defer close(c.chDone)
ticker := time.NewTicker(interval)
for {
select {
case <-ticker.C:
c.update()
case <-c.chStop:
return
}
}
}
func (c *HealthChecker) update() {
// copy services into a new map to avoid lock contention while doing checks
c.servicesMu.RLock()
l := len(c.services)
services := make(map[string]HealthReporter, l)
maps.Copy(services, c.services)
c.servicesMu.RUnlock()
ready := make(map[string]error, l)
healthy := make(map[string]error, l)
// now, do all the checks
for name, s := range services {
ready[name] = s.Ready()
for n, err := range s.HealthReport() {
healthy[n] = err
value := 0
if err == nil {
value = 1
}
// report metrics to prometheus
healthStatus.WithLabelValues(name).Set(float64(value))
}
}
uptimeSeconds.Add(interval.Seconds())
// save state
c.stateMu.Lock()
defer c.stateMu.Unlock()
maps.Copy(c.ready, ready)
maps.Copy(c.healthy, healthy)
}
// Register a service for health checks.
func (c *HealthChecker) Register(service HealthReporter) error {
name := service.Name()
if name == "" {
return fmt.Errorf("misconfigured check %#v for %v", name, service)
}
c.servicesMu.Lock()
defer c.servicesMu.Unlock()
if testing.Testing() {
if orig, ok := c.services[name]; ok {
panic(fmt.Errorf("duplicate name %q: service names must be unique: types %T & %T", name, service, orig))
}
}
c.services[name] = service
return nil
}
// Unregister a service.
func (c *HealthChecker) Unregister(name string) error {
if name == "" {
return fmt.Errorf("name cannot be empty")
}
c.servicesMu.Lock()
defer c.servicesMu.Unlock()
delete(c.services, name)
healthStatus.DeleteLabelValues(name)
return nil
}
// IsReady returns the current readiness of the system.
// A system is considered ready if all checks are passing (no errors)
func (c *HealthChecker) IsReady() (ready bool, errors map[string]error) {
c.stateMu.RLock()
defer c.stateMu.RUnlock()
ready = true
errors = make(map[string]error, len(c.ready))
for name, state := range c.ready {
errors[name] = state
if state != nil {
ready = false
}
}
return
}
// IsHealthy returns the current health of the system.
// A system is considered healthy if all checks are passing (no errors)
func (c *HealthChecker) IsHealthy() (healthy bool, errors map[string]error) {
c.stateMu.RLock()
defer c.stateMu.RUnlock()
healthy = true
errors = make(map[string]error, len(c.healthy))
for name, state := range c.healthy {
errors[name] = state
if state != nil {
healthy = false
}
}
return
}