-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
health.go
232 lines (186 loc) · 4.64 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
package services
import (
"sync"
"time"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/smartcontractkit/chainlink/v2/core/static"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
//go:generate mockery --quiet --name Checker --output ./mocks/ --case=underscore
type (
// Checker provides a service which can be probed for system health.
Checker interface {
// Register a service for health checks.
Register(name string, service Checkable) error
// Unregister a service.
Unregister(name string) error
// IsReady returns the current readiness of the system.
// A system is considered ready if all checks are passing (no errors)
IsReady() (ready bool, errors map[string]error)
// IsHealthy returns the current health of the system.
// A system is considered healthy if all checks are passing (no errors)
IsHealthy() (healthy bool, errors map[string]error)
Start() error
Close() error
}
checker struct {
srvMutex sync.RWMutex
services map[string]Checkable
stateMutex sync.RWMutex
healthy map[string]error
ready map[string]error
chStop chan struct{}
chDone chan struct{}
utils.StartStopOnce
}
Status string
)
var _ Checker = (*checker)(nil)
const (
StatusPassing Status = "passing"
StatusFailing Status = "failing"
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",
},
)
nodeVersion = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "version",
Help: "Node version information",
},
[]string{"version", "commit"},
)
)
func NewChecker() Checker {
c := &checker{
services: make(map[string]Checkable, 10),
healthy: make(map[string]error, 10),
ready: make(map[string]error, 10),
chStop: make(chan struct{}),
chDone: make(chan struct{}),
}
return c
}
func (c *checker) Start() error {
return c.StartOnce("HealthCheck", func() error {
nodeVersion.WithLabelValues(static.Version, static.Sha).Inc()
// update immediately
c.update()
go c.run()
return nil
})
}
func (c *checker) Close() error {
return c.StopOnce("HealthCheck", func() error {
close(c.chStop)
<-c.chDone
return nil
})
}
func (c *checker) run() {
defer close(c.chDone)
ticker := time.NewTicker(interval)
for {
select {
case <-ticker.C:
c.update()
case <-c.chStop:
return
}
}
}
func (c *checker) update() {
healthy := make(map[string]error)
ready := make(map[string]error)
c.srvMutex.RLock()
// copy services into a new map to avoid lock contention while doing checks
services := make(map[string]Checkable, len(c.services))
for name, s := range c.services {
services[name] = s
}
c.srvMutex.RUnlock()
// now, do all the checks
for name, s := range services {
ready[name] = s.Ready()
hr := s.HealthReport()
for n, err := range hr {
healthy[n] = err
}
}
// we use a separate lock to avoid holding the lock over state while talking
// to services
c.stateMutex.Lock()
defer c.stateMutex.Unlock()
for name, r := range ready {
c.ready[name] = r
}
for name, h := range healthy {
c.healthy[name] = h
value := 0
if h == nil {
value = 1
}
// report metrics to prometheus
healthStatus.WithLabelValues(name).Set(float64(value))
}
uptimeSeconds.Add(interval.Seconds())
}
func (c *checker) Register(name string, service Checkable) error {
if service == nil || name == "" {
return errors.Errorf("misconfigured check %#v for %v", name, service)
}
c.srvMutex.Lock()
defer c.srvMutex.Unlock()
c.services[name] = service
return nil
}
func (c *checker) Unregister(name string) error {
if name == "" {
return errors.Errorf("name cannot be empty")
}
c.srvMutex.Lock()
defer c.srvMutex.Unlock()
delete(c.services, name)
healthStatus.DeleteLabelValues(name)
return nil
}
func (c *checker) IsReady() (ready bool, errors map[string]error) {
c.stateMutex.RLock()
defer c.stateMutex.RUnlock()
ready = true
errors = make(map[string]error, len(c.services))
for name, state := range c.ready {
errors[name] = state
if state != nil {
ready = false
}
}
return
}
func (c *checker) IsHealthy() (healthy bool, errors map[string]error) {
c.stateMutex.RLock()
defer c.stateMutex.RUnlock()
healthy = true
errors = make(map[string]error, len(c.services))
for name, state := range c.healthy {
errors[name] = state
if state != nil {
healthy = false
}
}
return
}