-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathhealth.go
267 lines (224 loc) · 6.8 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package health
import (
"time"
"sync"
"fmt"
"log"
"github.com/pkg/errors"
"gitlab.appsflyer.com/Architecture/af-go-health/checks"
)
const (
maxExpectedChecks = 16
initialResultMsg = "didn't run yet"
)
// Health is the API for registering / deregistering health checks, and for fetching the health checks results.
type Health interface {
// RegisterCheck registers a health check according to the given configuration.
// Once RegisterCheck() is called, the check is being scheduled to run in it's own goroutine.
// Callers must make sure the checks complete at a reasonable time frame, or the next execution will delay.
RegisterCheck(cfg *Config) error
// Deregister removes a health check from this instance, and stops it's next executions.
// If the check is running while Deregister() is called, the check may complete it's current execution.
// Once a check is removed, it's results are no longer returned.
Deregister(name string)
// Results returns a snapshot of the health checks execution results at the time of calling, and the current health.
// A system is considered healthy iff all checks are passing
Results() (results map[string]Result, healthy bool)
// IsHealthy returns the current health of the system.
// A system is considered healthy iff all checks are passing.
IsHealthy() bool
// DeregisterAll Deregister removes all health checks from this instance, and stops their next executions.
// It is equivalent of calling Deregister() for each currently registered check.
DeregisterAll()
}
// Config defines a health Check and it's scheduling timing requirements.
type Config struct {
// Check is the health Check to be scheduled for execution.
Check checks.Check
// ExecutionPeriod is the period between successive executions.
ExecutionPeriod time.Duration
// InitialDelay is the time to delay first execution; defaults to zero.
InitialDelay time.Duration
}
// Result represents the output of a health check execution.
type Result interface {
fmt.Stringer
// IsHealthy returns true iff the corresponding health check has passed
IsHealthy() bool
}
// New returns a new Health instance.
func New() Health {
return &health{
results: make(map[string]*result, maxExpectedChecks),
checkTasks: make(map[string]checkTask, maxExpectedChecks),
lock: sync.RWMutex{},
}
}
type health struct {
results map[string]*result
checkTasks map[string]checkTask
lock sync.RWMutex
}
func (h *health) RegisterCheck(cfg *Config) error {
if cfg.Check == nil || cfg.Check.Name() == "" {
err := errors.WithStack(errors.New(fmt.Sprintf("misconfigured check %s", cfg.Check)))
log.Println(err)
return err
}
// checks are initially failing...
h.updateResult(cfg.Check.Name(), initialResultMsg, fmt.Errorf(initialResultMsg), time.Now())
h.scheduleCheck(h.createCheckTask(cfg), cfg)
return nil
}
func (h *health) createCheckTask(cfg *Config) *checkTask {
h.lock.Lock()
defer h.lock.Unlock()
task := checkTask{
stopChan: make(chan bool, 1),
ticker: time.NewTicker(cfg.ExecutionPeriod),
check: cfg.Check,
}
h.checkTasks[cfg.Check.Name()] = task
return &task
}
type checkTask struct {
stopChan chan bool
ticker *time.Ticker
check checks.Check
}
func (h *health) stopCheckTask(name string) {
h.lock.Lock()
defer h.lock.Unlock()
log.Println("Cleaning task ", name)
task := h.checkTasks[name]
task.ticker.Stop()
delete(h.results, name)
delete(h.checkTasks, name)
log.Printf("Task '%s' Stopped\n", name)
}
func (h *health) scheduleCheck(task *checkTask, cfg *Config) {
go func() {
// initial execution
h.runCheckOrStop(task, time.After(cfg.InitialDelay))
// scheduled execution
for {
h.runCheckOrStop(task, task.ticker.C)
}
}()
}
func (h *health) runCheckOrStop(task *checkTask, timerChan <-chan time.Time) {
select {
case <-task.stopChan:
h.stopCheckTask(task.check.Name())
return
case t := <-timerChan:
h.checkAndUpdateResult(task.check, t)
}
}
func (h *health) checkAndUpdateResult(check checks.Check, time time.Time) {
log.Printf("%s running task '%s'...\n", time, check.Name())
details, err := check.Execute()
h.updateResult(check.Name(), details, err, time)
}
func (h *health) Deregister(name string) {
log.Printf("Stopping task '%s'", name)
h.lock.RLock()
defer h.lock.RUnlock()
task, ok := h.checkTasks[name]
if ok {
// actual cleanup happens in the task go routine
task.stopChan <- true
}
}
func (h *health) DeregisterAll() {
log.Printf("Stopping health instance")
h.lock.RLock()
defer h.lock.RUnlock()
for k := range h.checkTasks {
h.Deregister(k)
}
}
func (h *health) Results() (results map[string]Result, healthy bool) {
results = make(map[string]Result)
h.lock.RLock()
defer h.lock.RUnlock()
healthy = true
for k, v := range h.results {
results[k] = v
healthy = healthy && v.IsHealthy()
}
return
}
func (h *health) IsHealthy() (healthy bool) {
h.lock.Lock()
defer h.lock.Unlock()
healthy = true
for _, v := range h.results {
healthy = healthy && v.IsHealthy()
}
return
}
func (h *health) updateResult(name string, details interface{}, err error, t time.Time) {
h.lock.Lock()
defer h.lock.Unlock()
prevResult, ok := h.results[name]
result := &result{
Details: details,
Error: newMarshalableError(err),
TimeStamp: t,
TimeOfFirstFailure: nil,
}
if !result.IsHealthy() {
if ok {
result.ContiguousFailures = prevResult.ContiguousFailures + 1
if prevResult.IsHealthy() {
result.TimeOfFirstFailure = &t
} else {
result.TimeOfFirstFailure = prevResult.TimeOfFirstFailure
}
} else {
result.ContiguousFailures = 1
result.TimeOfFirstFailure = &t
}
}
h.results[name] = result
}
type result struct {
// the details of task result - may be nil
Details interface{} `json:"message,omitempty"`
// the error returned from a failed health check - nil when successful
Error error `json:"error,omitempty"`
// the time of the last health check
TimeStamp time.Time `json:"timestamp"`
// the number of failures that occurred in a row
ContiguousFailures int64 `json:"num_failures"`
// the time of the initial transitional failure
TimeOfFirstFailure *time.Time `json:"first_failure_time"`
}
func (r *result) IsHealthy() bool {
return r.Error == nil
}
func (r *result) String() string {
return fmt.Sprintf("Result{details: %s, err: %s, time: %s, contiguousFailures: %d, timeOfFirstFailure:%s}",
r.Details, r.Error, r.TimeStamp, r.ContiguousFailures, r.TimeOfFirstFailure)
}
type marshalableError struct {
Message string `json:"message,omitempty"`
Cause error `json:"cause,omitempty"`
}
func newMarshalableError(err error) error {
if err == nil {
return nil
}
mr := &marshalableError{
Message: err.Error(),
}
cause := errors.Cause(err)
if cause != err {
mr.Cause = newMarshalableError(cause)
}
return mr
}
func (e *marshalableError) Error() string {
return e.Message
}