-
Notifications
You must be signed in to change notification settings - Fork 3
/
monitor.go
176 lines (146 loc) · 3.18 KB
/
monitor.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
package libhealth
import (
"context"
"sync"
"time"
)
var (
epoch = time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
defaultTimeout = 60 * time.Second
defaultPeriod = 30 * time.Second
)
// Monitor is the standard implementation of HealthChecker.
type Monitor struct {
name string
timeout time.Duration
period time.Duration
description string
docURL string
urgency Urgency
checker HealthChecker
statusChan chan HealthStatus
previous Health
lastOk time.Time
failed int
lock sync.RWMutex // locks above data
}
var _ HealthMonitor = (*Monitor)(nil)
// NewMonitorWithOptions constructs a new monitor from the provided components and configures optional ones (such as
// a status channel, timeout, and interval) based on the provided in options.
func NewMonitorWithOptions(
name,
description,
docURL string,
urgency Urgency,
check HealthChecker,
options ...MonitorOption,
) *Monitor {
monitor := &Monitor{
name: name,
timeout: defaultTimeout,
period: defaultPeriod,
description: description,
docURL: docURL,
urgency: urgency,
checker: check,
statusChan: nil,
previous: NewHealth(OK, "starting up"),
lastOk: epoch,
failed: 0,
}
for _, option := range options {
option(monitor)
}
return monitor
}
// NewMonitor will create a new monitor and set some defaults.
// If desired, pass in a channel and it will be published to when the health state of the monitor changes.
func NewMonitor(
name,
description,
docURL string,
urgency Urgency,
check HealthChecker,
statusChan chan HealthStatus,
) *Monitor {
return NewMonitorWithOptions(
name, description, docURL, urgency, check,
WithStatusChan(statusChan),
)
}
type HealthStatus struct {
Monitor HealthMonitor
Prev Status
Next Health
}
// Check will execute the HealthChecker associated with the monitor.
func (m *Monitor) Check(ctx context.Context) Health {
prev, next := m.checkOnce(ctx)
m.record(next, prev.Status)
return next
}
func (m *Monitor) checkOnce(ctx context.Context) (prev, next Health) {
startTime := time.Now()
next = m.checker(ctx)
endTime := time.Now()
next.Urgency = m.urgency
next.Time = startTime
next.Duration = endTime.Sub(startTime)
m.lock.Lock()
{
if next.SameAs(OK) {
m.lastOk = endTime
m.failed = 0
} else {
m.failed++
}
prev = m.previous
m.previous = next
}
m.lock.Unlock()
return prev, next
}
func (m *Monitor) record(next Health, prev Status) {
if m.statusChan == nil {
return
}
status := HealthStatus{
Monitor: m,
Prev: prev,
Next: next,
}
select {
case m.statusChan <- status:
return
default:
return
}
}
func (m *Monitor) Name() string {
return m.name
}
func (m *Monitor) Timeout() time.Duration {
return m.timeout
}
func (m *Monitor) Period() time.Duration {
return m.period
}
func (m *Monitor) Description() string {
return m.description
}
func (m *Monitor) Documentation() string {
return m.docURL
}
func (m *Monitor) Urgency() Urgency {
return m.urgency
}
func (m *Monitor) LastOk() time.Time {
m.lock.RLock()
defer m.lock.RUnlock()
return m.lastOk
}
func (m *Monitor) Failed() int {
m.lock.RLock()
defer m.lock.RUnlock()
return m.failed
}