forked from thbkrkr/toctoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.go
61 lines (52 loc) · 1.17 KB
/
watcher.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
package main
import (
"time"
log "github.com/Sirupsen/logrus"
"github.com/thbkrkr/toctoc/types"
)
var (
healthTimeoutDuration = time.Second * time.Duration(healthTimeout)
)
func Watch() {
tick := time.NewTicker(time.Second * time.Duration(watchTick))
for range tick.C {
mutex.Lock()
for ns := range events {
for _, event := range events[ns] {
if isKO(event) {
alert(ns, event)
}
}
}
mutex.Unlock()
}
}
func isKO(event types.Event) bool {
if event.Status == types.StatusKO {
return true
}
if time.Since(event.Timestamp).Seconds() > float64(healthTimeout) {
return true
}
return false
}
func alert(ns string, event types.Event) {
event.Status = types.StatusKO
events[ns][event.ID] = event
log.WithField("ns", ns).WithField("ID", event.ID).Errorf("No event since %d seconds", healthTimeout)
if kafkaAlerter {
go sendAlertToKafka(event)
}
}
func sendAlertToKafka(event types.Event) {
msg, err := event.ToBytes()
if err != nil {
log.WithField("Event", event).Errorf("Fail to marshal alert event")
return
}
_, _, err = q.Send(msg)
if err != nil {
log.WithField("Event", event).Errorf("Fail to send event to kafka")
return
}
}