-
Notifications
You must be signed in to change notification settings - Fork 1
/
heartbeat.go
159 lines (137 loc) · 5.57 KB
/
heartbeat.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
package internal
import (
"fmt"
"strings"
"time"
"github.com/containeroo/heartbeats/internal/ago"
"github.com/containeroo/heartbeats/internal/cache"
"github.com/containeroo/heartbeats/internal/metrics"
"github.com/containeroo/heartbeats/internal/timer"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
type NotificationsMap struct {
Name string `mapstructure:"name"`
Type string `mapstructure:"type"`
Enabled bool `mapstructure:"enabled"`
}
// Heartbeat is a struct for a heartbeat
type Heartbeat struct {
Name string `mapstructure:"name"`
UUID string `mapstructure:"uuid,omitempty"`
Enabled *bool `mapstructure:"enabled"`
Description string `mapstructure:"description"`
Interval time.Duration `mapstructure:"interval"`
Grace time.Duration `mapstructure:"grace"`
LastPing time.Time `mapstructure:"lastPing"`
Status string `mapstructure:"status"`
Notifications []string `mapstructure:"notifications"`
NotificationsMap []NotificationsMap `mapstructure:",-,omitempty"`
IntervalTimer *timer.Timer `mapstructure:"intervalTimer,omitempty"`
GraceTimer *timer.Timer `mapstructure:"graceTimer,omitempty"`
}
// TimeAgo returns a string with the time since the last ping
func (h *Heartbeat) TimeAgo(t time.Time) string {
if t.IsZero() {
return "never"
}
return ago.Calculate.Format(t)
}
// GotPing starts the timer for the heartbeat (heartbeatName)
func (h *Heartbeat) GotPing(details map[string]string) {
var msg string
// Timer is expired, grace is running but not completed
if h.IntervalTimer != nil && h.IntervalTimer.IsCompleted() && h.GraceTimer != nil && !h.GraceTimer.IsCompleted() && !h.GraceTimer.IsCancelled() {
log.Trace("Timer is expired, grace is running but not completed")
h.GraceTimer.SetCancelled(true)
msg = "got ping. Stop grace timer"
}
// Heartbeat is running and not expired
if h.IntervalTimer != nil && !h.IntervalTimer.IsCompleted() {
log.Trace("Heartbeat is running and not expired")
h.IntervalTimer.Reset(h.Interval)
msg = fmt.Sprintf("got ping. Reset timer with interval %s", h.Interval)
}
// This is the first ping or after a grace period has expired
// IntervalTimer is nil when never started
// IntervalTimer.IsCompleted is true when expired
// GraceTimer is nil when never started
// GraceTimer.IsCompleted is true when expired
if (h.IntervalTimer == nil || h.IntervalTimer.IsCompleted()) && (h.GraceTimer == nil || h.GraceTimer.IsCompleted() || h.GraceTimer.IsCancelled()) {
log.Tracef("First ping or ping after a grace period has expired")
h.IntervalTimer = timer.NewTimer(h.Interval, func() {
msg := fmt.Sprintf("Timer is expired. Start grace timer with duration %s", h.Grace)
log.Infof("%s %s", h.Name, msg)
cache.Local.Add(h.Name, cache.History{
Event: cache.EventGrace,
Message: fmt.Sprintf("Timer is expired. Start grace timer with duration %s", h.Grace),
})
h.Status = "GRACE"
h.GraceTimer = timer.NewTimer(h.Grace, Notify(h.Name, GRACE))
})
msg = fmt.Sprintf("Start timer with interval %s", h.Interval)
}
// inform the user that the heartbeat is running again
if h.Status != "" && h.Status != "OK" && h.Status != "GRACE" {
go Notify(h.Name, AGAIN)()
}
h.LastPing = time.Now()
h.Status = "OK"
metrics.PromMetrics.TotalHeartbeats.With(prometheus.Labels{"heartbeat": h.Name}).Inc()
metrics.PromMetrics.HeartbeatStatus.With(prometheus.Labels{"heartbeat": h.Name}).Set(1)
log.Infof("%s %s", h.Name, msg)
cache.Local.Add(h.Name, cache.History{
Event: cache.EventPing,
Message: msg,
Details: details,
})
}
// GotPingFail stops the timer for the heartbeat
func (h *Heartbeat) GotPingFail(details map[string]string) {
// cancel grace timer if running
if h.GraceTimer != nil && !h.GraceTimer.IsCompleted() {
h.GraceTimer.SetCancelled(true)
}
// cancel interval timer if running
if h.IntervalTimer != nil && !h.IntervalTimer.IsCompleted() {
h.IntervalTimer.SetCancelled(true)
}
go Notify(h.Name, FAILED)()
h.LastPing = time.Now()
h.Status = "FAIL"
metrics.PromMetrics.TotalHeartbeats.With(prometheus.Labels{"heartbeat": h.Name}).Inc()
metrics.PromMetrics.HeartbeatStatus.With(prometheus.Labels{"heartbeat": h.Name}).Set(0)
log.Infof("%s got '/fail' ping", h.Name)
cache.Local.Add(h.Name, cache.History{
Event: cache.EventFailed,
Message: "got '/fail' ping",
Details: details,
})
}
// GetServiceByType returns notification settings by type
func (h *Heartbeats) GetServiceByName(notificationType string) (*Service, error) {
for i, notification := range h.Notifications.Services {
if strings.EqualFold(notification.Name, notificationType) {
return &h.Notifications.Services[i], nil
}
}
return nil, fmt.Errorf("Notification settings for type «%s» not found", notificationType)
}
// GetHeartbeatByName search heartbeat in HeartbeatsConfig.Heartbeats by name and returns it
func (h *Heartbeats) GetHeartbeatByName(name string) (*Heartbeat, error) {
for i, heartbeat := range h.Heartbeats {
if strings.EqualFold(heartbeat.Name, name) {
return &h.Heartbeats[i], nil
}
}
return nil, fmt.Errorf("Heartbeat with name %s not found", name)
}
// GetHeartbeatByUUID search heartbeat in HeartbeatsConfig.Heartbeats by uuid and returns it
func (h *Heartbeats) GetHeartbeatByUUID(uuid string) (*Heartbeat, error) {
for i, heartbeat := range h.Heartbeats {
if heartbeat.UUID == uuid {
return &h.Heartbeats[i], nil
}
}
return nil, fmt.Errorf("Heartbeat with uuid %s not found", uuid)
}