-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.go
532 lines (422 loc) · 11.7 KB
/
service.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
package main
import (
"fmt"
"bytes"
"time"
"sort"
"regexp"
)
const (
STATUS_UP = 1
STATUS_DOWN = 2
STATUS_UNKNOWN = 0
)
const (
OKAY = 0
DEBUG = 1
INFO = 2
WARN = 3
ERROR = 4
)
type Service struct {
Name string
Enabled bool
Monitor *HeartbeatMonitor
Status int
HeartbeatCount int
LastHeartbeatTimestamp time.Time
Log ServiceLog
Group string
Description string
// filter on summary message
NotificationFilters map[int] *regexp.Regexp
// filter on when notification was generated
NotificationFirstMinute int
NotificationLastMinute int
}
type LogEntry struct {
ServiceName string
Summary string
Severity int
Timestamp time.Time
Sequence int
}
type ServiceLog struct {
entries []*LogEntry
}
type ServiceHub struct {
timeline *Timeline
services map[string] *Service
notifier *Notifier
logEntryCounter int
}
type ServiceSnapshot struct {
Name string
Status int
LastHeartbeatTimestamp string
IsUp bool
IsDown bool
IsUnknown bool
Enabled bool
Notifications []NotificationSummary
Description string
Group string
FilterCount int
}
type NotificationSummary struct {
Severity int
Count int
}
type ApiError struct {
error string
}
func (e ApiError) String() string {
return e.error
}
type FilterSnapshot struct {
Id int
Expression string
}
// contract between service hub and all threads running outside of
// timeline thread
type ThreadSafeServiceHub interface {
Log(serviceName string, summary string, severity int, timestamp time.Time) *ApiError
Heartbeat(serviceName string) *ApiError
GetLogEntries(serviceName string) []*LogEntry
RemoveLogEntry(sequence int)
GetServices() []ServiceSnapshot
GetNotificationFilters(serviceName string) []*FilterSnapshot
SetServiceEnabled(serviceName string, enabled bool)
RemoveNotificationFilter(serviceName string, id int)
AddNotificationFilter(serviceName string, expression *regexp.Regexp)
}
type ServiceHubAdapter struct {
hub *ServiceHub
}
func (a *ServiceHubAdapter) AddNotificationFilter(serviceName string, expression *regexp.Regexp) {
c := make(chan *ApiError)
hub := a.hub
hub.timeline.Execute(func() {
hub.AddNotificationFilter(serviceName, expression)
c<-nil
})
<-c
}
func (a *ServiceHubAdapter) RemoveNotificationFilter(serviceName string, id int) {
c := make(chan *ApiError)
hub := a.hub
hub.timeline.Execute(func() {
hub.RemoveNotificationFilter(serviceName, id)
c<-nil
})
<-c
}
func (a *ServiceHubAdapter) SetServiceEnabled(serviceName string, enabled bool) {
c := make(chan *ApiError)
hub := a.hub
hub.timeline.Execute(func() {
c <- hub.SetServiceEnabled(serviceName, enabled)
})
<-c
}
func (a *ServiceHubAdapter) Log(serviceName string, summary string, severity int, timestamp time.Time) *ApiError {
c := make(chan *ApiError)
hub := a.hub
hub.timeline.Execute(func() {
c <- hub.Log(serviceName, summary, severity, timestamp)
})
return <-c
}
func (a *ServiceHubAdapter) Heartbeat (serviceName string) *ApiError {
c := make(chan *ApiError)
hub := a.hub
hub.timeline.Execute(func() {
service, found := hub.services[serviceName]
if !found {
c <- &ApiError{"No service named \""+serviceName+"\""}
return
}
if service.Monitor != nil {
service.Monitor.Heartbeat()
}
c <- nil
})
return <-c
}
func (a *ServiceHubAdapter) GetServices() []ServiceSnapshot {
c := make(chan []ServiceSnapshot)
hub := a.hub
hub.timeline.Execute(func() {
ss := make([]ServiceSnapshot, 0, len(hub.services))
for _, v := range(hub.services) {
notifications := make([]NotificationSummary, 0, 10)
// count the number of message per severity
counts := make(map[int] int)
for _, l := range(v.Log.entries) {
c, exists := counts[l.Severity]
if !exists {
c = 0
}
c += 1
counts[l.Severity] = c
}
// now add them to the notification list ordered by severity
keys := make([]int, 0, len(notifications))
for k, _ := range(counts) {
keys = append(keys, k)
}
sort.Sort(sort.IntSlice(keys))
for _, k := range(keys) {
notifications = append(notifications, NotificationSummary{k, counts[k]})
}
var timestamp string
if v.HeartbeatCount == 0 {
timestamp = ""
} else {
timestamp = v.LastHeartbeatTimestamp.Format(time.Kitchen)
}
ss = append(ss, ServiceSnapshot{v.Name,
v.Status,
timestamp,
v.Status == STATUS_UP, v.Status == STATUS_DOWN, v.Status == STATUS_UNKNOWN,
v.Enabled, notifications, v.Description, v.Group,
len(v.NotificationFilters) })
}
c <- ss
})
return <-c
}
func (a *ServiceHubAdapter) GetLogEntries(serviceName string) []*LogEntry {
c := make(chan []*LogEntry)
hub := a.hub
hub.timeline.Execute(func() {
ss := make([]*LogEntry, 0, 100)
service, found := hub.services[serviceName]
if ! found {
c <- ss
return
}
for _, v := range(service.Log.entries) {
ss = append(ss, v)
}
c <- ss
})
return <-c
}
func (a *ServiceHubAdapter) GetNotificationFilters(serviceName string) []*FilterSnapshot {
c := make(chan []*FilterSnapshot)
hub := a.hub
hub.timeline.Execute(func() {
fs := make([]*FilterSnapshot, 0, 100)
service, found := hub.services[serviceName]
if ! found {
c <- fs
return
}
for k, v := range(service.NotificationFilters) {
fs = append(fs, &FilterSnapshot{k, v.String()})
}
c <- fs
})
return <-c
}
func removeLogEntriesWithId(entries []*LogEntry, sequenceToDel int) []*LogEntry {
dest := 0
for i, v := range(entries) {
if v.Sequence == sequenceToDel {
continue
}
entries[dest] = entries[i]
dest++
}
return entries[:dest]
}
func (a *ServiceHubAdapter) RemoveLogEntry(sequence int) {
c := make(chan bool)
hub := a.hub
hub.timeline.Execute(func() {
for _, service := range(hub.services) {
service.Log.entries = removeLogEntriesWithId(service.Log.entries, sequence)
}
c <- true
})
<-c
}
func NewHubAdapter(hub *ServiceHub) *ServiceHubAdapter {
return &ServiceHubAdapter{hub}
}
////////////////////////////////////////////////////////////////////////
func NewServiceHub(timeline *Timeline) *ServiceHub {
hub := &ServiceHub{timeline: timeline, services: make(map[string] *Service)}
hub.logEntryCounter = 1
return hub
}
func (h *ServiceHub) AddNotificationFilter(serviceName string, expression *regexp.Regexp) *ApiError{
service, found := h.services[serviceName]
if !found {
return &ApiError{"No service named \""+serviceName+"\""}
}
id := h.nextSequenceId()
service.NotificationFilters[id] = expression
return nil
}
func (h *ServiceHub) RemoveNotificationFilter(serviceName string, id int) *ApiError{
service, found := h.services[serviceName]
if !found {
return &ApiError{"No service named \""+serviceName+"\""}
}
delete(service.NotificationFilters, id)
return nil
}
func (h *ServiceHub) SetServiceEnabled(serviceName string, enabled bool) *ApiError {
service, found := h.services[serviceName]
if !found {
return &ApiError{"No service named \""+serviceName+"\""}
}
service.Enabled = enabled
return nil
}
func (h *ServiceHub) Log(serviceName string, summary string, severity int, timestamp time.Time) *ApiError {
service, found := h.services[serviceName]
if !found {
return &ApiError{"No service named \""+serviceName+"\""}
}
seq := h.nextSequenceId()
service.Log.entries = append(service.Log.entries, &LogEntry{serviceName, summary, severity, timestamp, seq})
h.notifier.CheckAndSendNotifications()
return nil
}
func (h *ServiceHub) AddService(serviceName string, heartbeatTimeout time.Duration, group string, description string, enabled bool, nstart int, nstop int) {
var s *Service
s = &Service{Name: serviceName,
Enabled: enabled,
Status: STATUS_UNKNOWN,
Description: description,
Group: group,
NotificationFilters: make(map[int]*regexp.Regexp),
NotificationFirstMinute: nstart,
NotificationLastMinute: nstop }
heartbeatCallback := func(name string, isFailure bool) {
if isFailure {
h.Log(serviceName, "Heartbeat failure", WARN, h.timeline.Now())
s.Status = STATUS_DOWN
} else {
s.Status = STATUS_UP
s.HeartbeatCount += 1
s.LastHeartbeatTimestamp = h.timeline.Now()
}
}
s.Monitor = NewHeartbeatMonitor(h.timeline, serviceName, heartbeatTimeout, heartbeatCallback)
h.services[serviceName] = s
s.Monitor.Start()
}
func (h *ServiceHub) nextSequenceId() int {
h.logEntryCounter += 1
seq := h.logEntryCounter
return seq
}
func (l *ServiceLog) FindAfter(sequence int) []*LogEntry {
result := make([]*LogEntry, 0, len(l.entries))
for _, v := range(l.entries) {
if v.Sequence > sequence {
result = append(result, v)
}
}
return result
}
type ExecutorFn func (command string, input string)
type Notifier struct {
command string
lastCheckSeq int
lastSendTimestamp time.Time
timeline *Timeline
hub *ServiceHub
executor ExecutorFn
throttle time.Duration
}
func NewNotifier(command string, throttle time.Duration, executor ExecutorFn, timeline *Timeline, hub *ServiceHub) *Notifier {
return &Notifier{command: command, throttle: throttle, timeline: timeline, hub: hub, executor: executor}
}
func (n *Notifier) CheckAndSendNotifications() {
now := n.timeline.Now()
if now.Sub(n.lastSendTimestamp) >= n.throttle {
// enough time has passed since the last send
// so we can flush the event queue
n.lastSendTimestamp = now
n.sendNotificationSummary()
} else {
// too soon, so schedule a check of the queue after enough time has passed
n.timeline.Schedule(n.lastSendTimestamp.Add(n.throttle), func() { n.CheckAndSendNotifications() } )
}
}
func isAllowingNotifications(service *Service, entry *LogEntry ) bool {
summary := entry.Summary
localTime := entry.Timestamp
minuteOfDay := localTime.Hour() * 60 + localTime.Minute()
//log.Printf("minuteOfDay=%d first=%d last=%d\n", minuteOfDay, service.NotificationFirstMinute, service.NotificationLastMinute)
if minuteOfDay < service.NotificationFirstMinute || minuteOfDay > service.NotificationLastMinute {
return false
}
// check each filter
for _, filter := range(service.NotificationFilters) {
if filter.FindStringIndex(summary) != nil {
return false
}
}
return service.Enabled && entry.Severity >= WARN
}
func (n *Notifier) sendNotificationSummary() {
// find all outstanding notifications, grouping them by service
msgsByService := make(map[string] []string)
maxSeq := 0
for k, v := range(n.hub.services) {
e := v.Log.FindAfter(n.lastCheckSeq)
if len(e) > 0 {
msgs := make([]string, 0, len(e))
for _, l := range(e) {
if l.Sequence > maxSeq {
maxSeq = l.Sequence
}
// wait until the last moment to test v.Enabled so that maxSeq gets updated
if isAllowingNotifications(v, l) {
msgs = append(msgs, fmt.Sprintf("%s: %s", k, l.Summary))
}
}
if len(msgs) > 0 {
msgsByService[k] = msgs
}
}
}
// remember where we left off so we can identify what are new notifications
if n.lastCheckSeq < maxSeq {
n.lastCheckSeq = maxSeq
}
if len(msgsByService) > 1 {
msg := bytes.NewBufferString("Multiple services had notifications: ")
for k, v := range(msgsByService) {
msg.WriteString(fmt.Sprintf("%s(%d) ", k, len(v)))
}
n.sendNotification(msg.String())
} else if len(msgsByService) == 1 {
// get the only msg list
var serviceName string
var msgs []string
for tservice, tmsg := range(msgsByService) {
serviceName = tservice
msgs = tmsg
}
if len(msgs) > 1 {
// if we have multiple messages, just send the count of messages and
msg := fmt.Sprintf("%s had %d notifications", serviceName, len(msgs))
n.sendNotification(msg)
} else {
// we must only have one message so just send that
msg := msgs[0]
n.sendNotification(msg)
}
}
// otherwise if there were no messages pending, so do nothing
}
func (n *Notifier) sendNotification( msg string ) {
n.executor(n.command, msg)
}