-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.go
52 lines (43 loc) · 1.31 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
package db
import (
"agent/config"
"agent/errors"
"agent/logger"
"agent/util"
"reflect"
"time"
)
type Monitor interface {
Run(postgresClient *PostgresClient)
}
type MonitorWorker struct {
config config.Config
monitor Monitor
postgresClient *PostgresClient
}
func (m *MonitorWorker) Start() {
// recover from monitor panics but log what happened
defer errors.DeferRecoverWithCallback(func(err error) {
msg := reflect.TypeOf(m.monitor).Elem().Name() + " panicked!"
logger.Error(msg, "server", m.postgresClient.serverID.ConfigName, "panic", err.Error())
})
started_at := time.Now().UTC().UnixNano()
m.monitor.Run(m.postgresClient)
duration := NanosecondsToMilliseconds(time.Now().UTC().UnixNano() - started_at)
if m.config.IsLogDebug() {
msg := reflect.TypeOf(m.monitor).Elem().Name() + " ran"
logger.Debug(msg, "server", m.postgresClient.serverID.ConfigName, "duration_ms", duration)
}
}
func NewMonitorWorker(config config.Config, postgresClient *PostgresClient, monitor Monitor) *MonitorWorker {
return &MonitorWorker{
monitor: monitor,
config: config,
postgresClient: postgresClient,
}
}
func NanosecondsToMilliseconds(duration int64) float64 {
durationMs := float64(duration) / float64(1000000) // ns to ms
durationMs = util.Round(durationMs)
return durationMs
}