-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.go
197 lines (161 loc) · 4.03 KB
/
snapshot.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
package monitoring
import "strings"
// FlatSnapshot represents a flatten snapshot of all metrics.
// Names in the tree will be joined with `.` .
type FlatSnapshot struct {
Bools map[string]bool
Ints map[string]int64
Floats map[string]float64
Strings map[string]string
}
type flatSnapshotVisitor struct {
snapshot FlatSnapshot
level []string
}
type structSnapshotVisitor struct {
key keyStack
event eventStack
depth int
}
type keyStack struct {
current string
stack []string
stack0 [32]string
}
type eventStack struct {
current map[string]interface{}
stack []map[string]interface{}
stack0 [32]map[string]interface{}
}
// CollectFlatSnapshot collects a flattened snapshot of
// a metrics tree start with the given registry.
func CollectFlatSnapshot(r *Registry, mode Mode, expvar bool) FlatSnapshot {
if r == nil {
r = Default
}
vs := newFlatSnapshotVisitor()
r.Visit(mode, vs)
if expvar {
VisitExpvars(vs)
}
return vs.snapshot
}
func MakeFlatSnapshot() FlatSnapshot {
return FlatSnapshot{
Bools: map[string]bool{},
Ints: map[string]int64{},
Floats: map[string]float64{},
Strings: map[string]string{},
}
}
// CollectStructSnapshot collects a structured metrics snaphot of
// a metrics tree starting with the given registry.
// Empty namespaces will be omitted.
func CollectStructSnapshot(r *Registry, mode Mode, expvar bool) map[string]interface{} {
if r == nil {
r = Default
}
vs := newStructSnapshotVisitor()
r.Visit(mode, vs)
snapshot := vs.event.current
if expvar {
vs := newStructSnapshotVisitor()
VisitExpvars(vs)
for k, v := range vs.event.current {
snapshot[k] = v
}
}
return snapshot
}
func newFlatSnapshotVisitor() *flatSnapshotVisitor {
return &flatSnapshotVisitor{snapshot: MakeFlatSnapshot()}
}
func (vs *flatSnapshotVisitor) OnRegistryStart() {}
func (vs *flatSnapshotVisitor) OnRegistryFinished() {
if len(vs.level) > 0 {
vs.dropName()
}
}
func (vs *flatSnapshotVisitor) OnKey(name string) {
vs.level = append(vs.level, name)
}
func (vs *flatSnapshotVisitor) getName() string {
defer vs.dropName()
if len(vs.level) == 1 {
return vs.level[0]
}
return strings.Join(vs.level, ".")
}
func (vs *flatSnapshotVisitor) dropName() {
vs.level = vs.level[:len(vs.level)-1]
}
func (vs *flatSnapshotVisitor) OnString(s string) {
vs.snapshot.Strings[vs.getName()] = s
}
func (vs *flatSnapshotVisitor) OnBool(b bool) {
vs.snapshot.Bools[vs.getName()] = b
}
func (vs *flatSnapshotVisitor) OnInt(i int64) {
vs.snapshot.Ints[vs.getName()] = i
}
func (vs *flatSnapshotVisitor) OnFloat(f float64) {
vs.snapshot.Floats[vs.getName()] = f
}
func newStructSnapshotVisitor() *structSnapshotVisitor {
vs := &structSnapshotVisitor{}
vs.key.stack = vs.key.stack0[:0]
vs.event.stack = vs.event.stack0[:0]
return vs
}
func (s *structSnapshotVisitor) OnRegistryStart() {
if s.depth > 0 {
s.event.push()
}
s.depth++
}
func (s *structSnapshotVisitor) OnRegistryFinished() {
s.depth--
if s.depth == 0 {
return
}
event := s.event.pop()
if event == nil {
s.key.pop()
return
}
s.setValue(event)
}
func (s *structSnapshotVisitor) OnKey(key string) {
s.key.push(key)
}
func (s *structSnapshotVisitor) OnString(str string) { s.setValue(str) }
func (s *structSnapshotVisitor) OnBool(b bool) { s.setValue(b) }
func (s *structSnapshotVisitor) OnInt(i int64) { s.setValue(i) }
func (s *structSnapshotVisitor) OnFloat(f float64) { s.setValue(f) }
func (s *structSnapshotVisitor) setValue(v interface{}) {
if s.event.current == nil {
s.event.current = map[string]interface{}{}
}
s.event.current[s.key.current] = v
s.key.pop()
}
func (s *keyStack) push(key string) {
s.stack = append(s.stack, s.current)
s.current = key
}
func (s *keyStack) pop() {
last := len(s.stack) - 1
s.current = s.stack[last]
s.stack = s.stack[:last]
}
func (s *eventStack) push() {
s.stack = append(s.stack, s.current)
s.current = nil
}
func (s *eventStack) pop() map[string]interface{} {
event := s.current
last := len(s.stack) - 1
s.current = s.stack[last]
s.stack = s.stack[:last]
return event
}