This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
monitor.go
160 lines (134 loc) · 2.86 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
package mntr
import (
"fmt"
"runtime"
"strings"
"time"
)
type OnMessage func(string, map[string]string)
type OnError func(error, map[string]string)
type Monitor struct {
Fields map[string]interface{}
OnInfo OnMessage
OnChange OnMessage
OnError OnError
verbose bool
}
func (m Monitor) WithField(key string, value interface{}) Monitor {
return m.WithFields(map[string]interface{}{key: value})
}
func (m Monitor) WithFields(add map[string]interface{}) Monitor {
m.Fields = merge(m.Fields, add)
return m
}
func (m Monitor) Info(msg string) {
if m.OnInfo == nil {
return
}
m.Fields = merge(map[string]interface{}{
"msg": msg,
"ts": now(),
}, m.Fields)
if m.verbose {
m.addDebugContext()
}
m.OnInfo(msg, normalize(m.Fields))
}
func (m Monitor) Changed(evt string) {
if m.OnChange == nil {
return
}
m.Fields = merge(map[string]interface{}{
"evt": evt,
"ts": now(),
}, m.Fields)
if m.verbose {
m.addDebugContext()
}
m.OnChange(evt, normalize(m.Fields))
}
func (m Monitor) Error(err error) {
if err == nil || m.OnError == nil {
return
}
m.Fields = merge(map[string]interface{}{
"err": err.Error(),
"ts": now(),
}, m.Fields)
m.addDebugContext()
m.OnError(err, normalize(m.Fields))
}
func (m Monitor) Debug(dbg string) {
if !m.verbose {
return
}
m.Fields = merge(map[string]interface{}{
"dbg": dbg,
"ts": now(),
}, m.Fields)
m.addDebugContext()
LogMessage(dbg, normalize(m.Fields))
}
func (m Monitor) Verbose() Monitor {
m.verbose = true
return m
}
func (m Monitor) IsVerbose() bool {
return m.verbose
}
func now() string {
return time.Now().Format(time.RFC3339)
}
func merge(fields map[string]interface{}, add map[string]interface{}) map[string]interface{} {
newFields := make(map[string]interface{})
for k, v := range fields {
newFields[k] = v
}
for k, v := range add {
panicOnReserved(k)
newFields[k] = v
}
return newFields
}
func panicOnReserved(key string) {
switch key {
case "ts":
fallthrough
case "msg":
fallthrough
case "dbg":
fallthrough
case "evt":
fallthrough
case "src":
fallthrough
case "err":
panic(fmt.Errorf("Key \"%s\" is reserved", key))
}
}
func (m *Monitor) addDebugContext() {
pc := make([]uintptr, 15)
n := runtime.Callers(1, pc)
frames := runtime.CallersFrames(pc[:n])
frame, more := frames.Next()
seenCaller := false
framesLoop:
for more && frame.Func != nil {
receiverStart := strings.LastIndex(frame.Function, "/")
receiverEnd := strings.LastIndex(frame.Function, ".")
receiver := ""
if receiverStart != -1 && receiverEnd != -1 {
receiver = frame.Function[receiverStart+1 : receiverEnd]
}
if !strings.Contains(receiver, ".Monitor") {
if seenCaller {
m.Fields["src"] = fmt.Sprintf("%s:%d", frame.File, frame.Line)
return
}
frame, more = frames.Next()
continue framesLoop
}
seenCaller = true
frame, more = frames.Next()
}
}