/
metric.go
382 lines (344 loc) · 9.53 KB
/
metric.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
package hostworker
import (
"context"
"fmt"
"strings"
"sync"
"text/template"
"time"
"github.com/cretz/monitoral/monitoralpb"
"github.com/cretz/monitoral/systemworker"
"go.temporal.io/sdk/activity"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
const metricTable = "metrics"
type metricManager struct {
host *host
configSeq uint64
metrics map[string]*metric
metricsLock sync.Mutex // Governs all fields above
newMetrics chan map[string]*metric
runCancel context.CancelFunc
runCancelLock sync.Mutex
}
func newMetricManager(host *host) *metricManager {
return &metricManager{
host: host,
metrics: map[string]*metric{},
// Important to only have a buffer of 1 here w/ how it's used
newMetrics: make(chan map[string]*metric, 1),
}
}
func (m *metricManager) updateConfigs(configSeq uint64, configs []*monitoralpb.HostConfig) error {
// Lock the metrics for the life of the update
m.metricsLock.Lock()
defer m.metricsLock.Unlock()
// Make sure config seq is newer
if m.configSeq >= configSeq {
return nil
}
m.configSeq = configSeq
// Also lock the run cancel for the life of the update so we can check if it's
// running and we should start new metrics
m.runCancelLock.Lock()
defer m.runCancelLock.Unlock()
// Go over every config, updating metrics if there or creating as needed
errs := []string{}
seenMetrics := map[string]bool{}
newMetrics := map[string]*metric{}
for _, hostConfig := range configs {
for _, metricConfig := range hostConfig.Metrics {
seenMetrics[metricConfig.Name] = true
// If it already exists, just update config. Otherwise create and
// set as new. This means if a config comes in twice for the same metric,
// the latest config wins.
var err error
if metric := m.metrics[metricConfig.Name]; metric != nil {
err = metric.updateConfig(configSeq, metricConfig)
} else if metric, err = newMetric(m.host, configSeq, metricConfig); err == nil {
newMetrics[metricConfig.Name] = metric
}
if err != nil {
errs = append(errs, fmt.Sprintf("failed updating config for metric %q: %v", metricConfig.Name, err))
}
}
}
// Stop and delete any unseen metrics
for name, metric := range m.metrics {
if !seenMetrics[name] {
delete(m.metrics, name)
// Can just stop here, the error will be nil from run() if running
metric.stop()
}
}
// Drain the new new metrics in channel into our map and resend
select {
case otherNewMetrics := <-m.newMetrics:
for k, v := range otherNewMetrics {
newMetrics[k] = v
}
default:
}
select {
case m.newMetrics <- newMetrics:
default:
// Should never happen
}
// Return errors
// TODO(cretz): Include in more advanced error details w/ strong types
if len(errs) > 0 {
return fmt.Errorf("errors during config update: %v", strings.Join(errs, ", "))
}
return nil
}
// Does not return error on context close. Context is an activity context.
func (m *metricManager) run(ctx context.Context) error {
// Make sure not running then set cancel callback
m.runCancelLock.Lock()
exists := m.runCancel != nil
if !exists {
ctx, m.runCancel = context.WithCancel(ctx)
}
m.runCancelLock.Unlock()
if exists {
return fmt.Errorf("already running")
}
defer m.stop()
// Start all of the metrics in the background
runMetric := func(name string, metric *metric) {
if err := metric.run(ctx); err != nil && ctx.Err() == nil {
// Metric failed, so we want to log and remove it from the metric set
m.metricsLock.Lock()
delete(m.metrics, name)
m.metricsLock.Unlock()
// TODO(cretz): Send system notification instead?
activity.GetLogger(ctx).Warn("Metric failed, will not run again", "Metric", name, "Error", err)
}
}
m.metricsLock.Lock()
for name, metric := range m.metrics {
go runMetric(name, metric)
}
// Also drain the channel
select {
case <-m.newMetrics:
default:
}
m.metricsLock.Unlock()
// Run until cancel or new metrics or notification
for {
select {
case <-ctx.Done():
return nil
case newMetrics := <-m.newMetrics:
// Start each new metric in the background
for name, metric := range newMetrics {
go runMetric(name, metric)
}
}
}
}
func (m *metricManager) stop() {
m.runCancelLock.Lock()
if m.runCancel != nil {
m.runCancel()
}
m.runCancel = nil
m.runCancelLock.Unlock()
}
type metric struct {
host *host
runCancel context.CancelFunc
runCancelLock sync.Mutex
lastConfig *metricConfig
lastConfigLock sync.Mutex
configUpdated chan *metricConfig
}
func newMetric(
host *host,
configSeq uint64,
config *monitoralpb.HostConfig_Metric,
) (*metric, error) {
m := &metric{
host: host,
configUpdated: make(chan *metricConfig, 1),
}
if err := m.updateConfig(configSeq, config); err != nil {
return nil, err
}
return m, nil
}
// Does not return error on context close
func (m *metric) run(ctx context.Context) error {
// Make sure not running then set cancel callback
m.runCancelLock.Lock()
exists := m.runCancel != nil
if !exists {
ctx, m.runCancel = context.WithCancel(ctx)
}
m.runCancelLock.Unlock()
if exists {
return fmt.Errorf("already running")
}
defer m.stop()
// Create ticker
m.lastConfigLock.Lock()
config := m.lastConfig
// Drain config updated
select {
case <-m.configUpdated:
default:
}
m.lastConfigLock.Unlock()
ticker := time.NewTicker(config.ExecFreq.AsDuration())
defer ticker.Stop()
// Run until stopped
// TODO(cretz): How best to stop it from alerting when complete?
currentlyAlerting := false
for {
// Wait for tick
select {
case <-ctx.Done():
return nil
case config = <-m.configUpdated:
// Reset ticker
ticker.Reset(config.ExecFreq.AsDuration())
case <-ticker.C:
// Collect metric
alerting, err := m.execMetric(ctx, config)
if err != nil {
return err
}
// If alerting status changed, send notification
if alerting != currentlyAlerting {
currentlyAlerting = alerting
change := &monitoralpb.Notification_AlertChange{Description: config.Description}
notification := &monitoralpb.Notification{
Host: m.host.hostname,
Name: config.Name,
StartedOn: timestamppb.Now(),
}
if currentlyAlerting {
notification.Detail = &monitoralpb.Notification_AlertStarted{AlertStarted: change}
} else {
notification.Detail = &monitoralpb.Notification_AlertStopped{AlertStopped: change}
}
err := m.host.client.SystemUpdateNotifications(
ctx,
systemworker.SystemWorkflowID,
"",
&monitoralpb.SystemUpdateNotificationsRequest{Notifications: []*monitoralpb.Notification{notification}},
)
if err != nil {
return fmt.Errorf("failed sending alert notification: %w", err)
}
}
}
}
}
func (m *metric) stop() {
m.runCancelLock.Lock()
if m.runCancel != nil {
m.runCancel()
}
m.runCancel = nil
m.runCancelLock.Unlock()
}
func (m *metric) updateConfig(configSeq uint64, config *monitoralpb.HostConfig_Metric) error {
// TODO(cretz): Validate config
// Lock for the rest of the call
m.lastConfigLock.Lock()
defer m.lastConfigLock.Unlock()
// If not newer or not changed, do nothing
if configSeq <= m.lastConfig.seq || proto.Equal(config, m.lastConfig.HostConfig_Metric) {
return nil
}
// Set last config
newConf, err := newMetricConfig(config, configSeq)
if err != nil {
return fmt.Errorf("invalid metric config: %w", err)
}
m.lastConfig = newConf
// Continually attempt to drain-and-set the config until we can
for {
select {
case m.configUpdated <- newConf:
return nil
case <-m.configUpdated:
}
}
}
func (m *metric) execMetric(ctx context.Context, config *metricConfig) (alerting bool, err error) {
query, err := config.buildQuery()
if err != nil {
return false, err
}
// Exec all queries
db := m.host.dbPool.Get(ctx)
defer m.host.dbPool.Put(db)
var lastStmtHadRow bool
for {
query = strings.TrimSpace(query)
if query == "" {
break
}
stmt, trailingBytes, err := db.PrepareTransient(query)
if err != nil {
return false, fmt.Errorf("query creation failed: %w", err)
}
lastStmtHadRow, err = stmt.Step()
stmt.Finalize()
if err != nil {
return false, fmt.Errorf("query execution failed: %w", err)
}
query = query[len(query)-trailingBytes:]
}
// If it has a row it's alerting
return lastStmtHadRow, nil
}
type metricConfig struct {
*monitoralpb.HostConfig_Metric
seq uint64
template *template.Template
templateData *metricTemplateData
}
func newMetricConfig(config *monitoralpb.HostConfig_Metric, seq uint64) (*metricConfig, error) {
if config.GetExec().GetSqlQueryTemplate() == "" {
return nil, fmt.Errorf("missing SQL query template")
} else if config.ExecFreq == nil {
return nil, fmt.Errorf("missing exec frequency")
}
ret := &metricConfig{
HostConfig_Metric: config,
seq: seq,
templateData: &metricTemplateData{
MetricName: config.Name,
MetricTable: metricTable,
Psutil: psutilData,
},
}
var err error
ret.template, err = template.New("query").
Funcs(template.FuncMap{
// Only concerned about single quotes atm
"sqlstring": func(v string) string { return strings.ReplaceAll(v, "'", "''") },
}).
Parse(config.GetExec().GetSqlQueryTemplate())
if err != nil {
return nil, fmt.Errorf("invalid query template: %w", err)
}
return ret, nil
}
func (m *metricConfig) buildQuery() (string, error) {
var bld strings.Builder
if err := m.template.Execute(&bld, m.templateData); err != nil {
return "", fmt.Errorf("building query failed: %w", err)
}
return bld.String(), nil
}
type metricTemplateData struct {
MetricName string
MetricTable string
Psutil map[string]map[string]interface{}
}