-
Notifications
You must be signed in to change notification settings - Fork 248
/
checkup.go
306 lines (272 loc) · 8 KB
/
checkup.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Package checkup provides means for checking and reporting the
// status and performance of various endpoints in a distributed,
// lock-free, self-hosted fashion.
package checkup
import (
"bytes"
"encoding/json"
"fmt"
"log"
"sync"
"time"
"github.com/sourcegraph/checkup/types"
)
// Checkup performs a routine checkup on endpoints or
// services.
type Checkup struct {
// Checkers is the list of Checkers to use with
// which to perform checks.
Checkers []Checker `json:"checkers,omitempty"`
// ConcurrentChecks is how many checks, at most, to
// perform concurrently. Default is
// DefaultConcurrentChecks.
ConcurrentChecks int `json:"concurrent_checks,omitempty"`
// Timestamp is the timestamp to force for all checks.
// Useful if wanting to perform distributed check
// "at the same time" even if they might actually
// be a few milliseconds or seconds apart.
Timestamp time.Time `json:"timestamp,omitempty"`
// Storage is the storage mechanism for saving the
// results of checks. Required if calling Store().
// If Storage is also a Maintainer, its Maintain()
// method will be called by c.CheckAndStore().
Storage Storage `json:"storage,omitempty"`
// Notifiers are list of notifiers to invoke with
// the results after checks from all checkers have
// completed. Notifier may evaluate and choose to
// send a notification of potential problems.
Notifiers []Notifier `json:"notifiers,omitempty"`
}
// Check performs the health checks. An error is only
// returned in the case of a misconfiguration or if
// any one of the Checkers returns an error.
func (c Checkup) Check() ([]types.Result, error) {
if c.ConcurrentChecks == 0 {
c.ConcurrentChecks = DefaultConcurrentChecks
}
if c.ConcurrentChecks < 0 {
return nil, fmt.Errorf("invalid value for ConcurrentChecks: %d (must be set > 0)",
c.ConcurrentChecks)
}
results := make([]types.Result, len(c.Checkers))
errs := make(types.Errors, len(c.Checkers))
throttle := make(chan struct{}, c.ConcurrentChecks)
wg := sync.WaitGroup{}
for i, checker := range c.Checkers {
throttle <- struct{}{}
wg.Add(1)
go func(i int, checker Checker) {
results[i], errs[i] = checker.Check()
<-throttle
wg.Done()
}(i, checker)
}
wg.Wait()
if !c.Timestamp.IsZero() {
for i := range results {
results[i].Timestamp = c.Timestamp.UTC().UnixNano()
}
}
if !errs.Empty() {
return results, errs
}
for _, service := range c.Notifiers {
err := service.Notify(results)
if err != nil {
log.Printf("ERROR sending notifications for %s: %s", service.Type(), err)
}
}
return results, nil
}
// CheckAndStore performs health checks and immediately
// stores the results to the configured storage if there
// were no errors. Checks are not performed if c.Storage
// is nil. If c.Storage is also a Maintainer, Maintain()
// will be called if Store() is successful.
func (c Checkup) CheckAndStore() error {
if c.Storage == nil {
return fmt.Errorf("no storage mechanism defined")
}
results, err := c.Check()
if err != nil {
return err
}
err = c.Storage.Store(results)
if err != nil {
return err
}
if m, ok := c.Storage.(Maintainer); ok {
return m.Maintain()
}
return nil
}
// CheckAndStoreEvery calls CheckAndStore every interval. It returns
// the ticker that it's using so you can stop it when you don't want
// it to run anymore. This function does NOT block (it runs the ticker
// in a goroutine). Any errors are written to the standard logger. It
// would not be wise to set an interval lower than the time it takes
// to perform the checks.
func (c Checkup) CheckAndStoreEvery(interval time.Duration) *time.Ticker {
ticker := time.NewTicker(interval)
check := func() {
if err := c.CheckAndStore(); err != nil {
log.Println(err)
}
}
go func() {
check()
for range ticker.C {
check()
}
}()
return ticker
}
// MarshalJSON marshals c into JSON with type information
// included on the interface values.
func (c Checkup) MarshalJSON() ([]byte, error) {
// Start with the fields of c that don't require special
// handling; unfortunately this has to mimic c's definition.
easy := struct {
ConcurrentChecks int `json:"concurrent_checks,omitempty"`
Timestamp time.Time `json:"timestamp,omitempty"`
}{
ConcurrentChecks: c.ConcurrentChecks,
Timestamp: c.Timestamp,
}
result, err := json.Marshal(easy)
if err != nil {
return result, err
}
wrap := func(key string, value []byte) {
b := append([]byte{result[0]}, []byte(`"`+key+`":`)...)
b = append(b, value...)
if len(result) > 2 {
b = append(b, ',')
}
result = append(b, result[1:]...)
}
// Checkers
if len(c.Checkers) > 0 {
var checkers [][]byte
for _, ch := range c.Checkers {
chb, err := json.Marshal(ch)
if err != nil {
return result, err
}
chb = []byte(fmt.Sprintf(`{"type":"%s",%s`, ch.Type(), string(chb[1:])))
checkers = append(checkers, chb)
}
allCheckers := []byte{}
allCheckers = append(allCheckers, '[')
allCheckers = append(allCheckers, bytes.Join(checkers, []byte(","))...)
allCheckers = append(allCheckers, ']')
wrap("checkers", allCheckers)
}
// Storage
if c.Storage != nil {
sb, err := json.Marshal(c.Storage)
if err != nil {
return result, err
}
sb = []byte(fmt.Sprintf(`{"type":"%s",%s`, c.Storage.Type(), string(sb[1:])))
wrap("storage", sb)
}
// Notifiers
if len(c.Notifiers) > 0 {
var checkers [][]byte
for _, ch := range c.Notifiers {
chb, err := json.Marshal(ch)
if err != nil {
return result, err
}
chb = []byte(fmt.Sprintf(`{"type":"%s",%s`, ch.Type(), string(chb[1:])))
checkers = append(checkers, chb)
}
allNotifiers := []byte{}
allNotifiers = append(allNotifiers, '[')
allNotifiers = append(allNotifiers, bytes.Join(checkers, []byte(","))...)
allNotifiers = append(allNotifiers, ']')
wrap("notifiers", allNotifiers)
}
return result, nil
}
// UnmarshalJSON unmarshales b into c. To succeed, it
// requires type information for the interface values.
func (c *Checkup) UnmarshalJSON(b []byte) error {
// Unmarshal as much of b as we can; this requires
// a type that doesn't implement json.Unmarshaler,
// hence the conversion. We also know that the
// interface types will ultimately cause an error,
// but we can ignore it because we handle it below.
type checkup2 *Checkup
_ = json.Unmarshal(b, checkup2(c))
// clean the slate
c.Checkers = []Checker{}
c.Notifiers = []Notifier{}
// Begin unmarshaling interface values by
// collecting the raw JSON
raw := struct {
Checkers []json.RawMessage `json:"checkers"`
Storage json.RawMessage `json:"storage"`
Notifier json.RawMessage `json:"notifier"`
Notifiers []json.RawMessage `json:"notifiers"`
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return err
}
// Then collect the concrete type information
configTypes := struct {
Checkers []struct {
Type string `json:"type"`
}
Storage struct {
Type string `json:"type"`
}
Notifier struct {
Type string `json:"type"`
}
Notifiers []struct {
Type string `json:"type"`
}
}{}
err = json.Unmarshal(b, &configTypes)
if err != nil {
return err
}
// Finally, we unmarshal the remaining values using type
// assertions with the help of the type information
for i, t := range configTypes.Checkers {
checker, err := checkerDecode(t.Type, raw.Checkers[i])
if err != nil {
return err
}
c.Checkers = append(c.Checkers, checker)
}
if raw.Storage != nil {
storage, err := storageDecode(configTypes.Storage.Type, raw.Storage)
if err != nil {
return err
}
c.Storage = storage
}
if raw.Notifier != nil {
notifier, err := notifierDecode(configTypes.Notifier.Type, raw.Notifier)
if err != nil {
return err
}
// Move `notifier` into `notifiers[]`
c.Notifiers = append(c.Notifiers, notifier)
}
for i, n := range configTypes.Notifiers {
notifier, err := notifierDecode(n.Type, raw.Notifiers[i])
if err != nil {
return err
}
c.Notifiers = append(c.Notifiers, notifier)
}
return nil
}
// DefaultConcurrentChecks is how many checks,
// at most, to perform concurrently.
var DefaultConcurrentChecks = 5