-
Notifications
You must be signed in to change notification settings - Fork 33
/
registry.go
275 lines (233 loc) · 7.37 KB
/
registry.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
// Copyright (C) 2015 Space Monkey, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package monkit
import (
"sort"
"sync"
)
type traceWatcherRef struct {
watcher func(*Trace)
}
type registryInternal struct {
// sync/atomic things
traceWatcher *traceWatcherRef
watcherMtx sync.Mutex
watcherCounter int64
traceWatchers map[int64]func(*Trace)
scopeMtx sync.Mutex
scopes map[string]*Scope
spanMtx sync.Mutex
spans map[*Span]struct{}
orphanMtx sync.Mutex
orphans map[*Span]struct{}
}
// Registry encapsulates all of the top-level state for a monitoring system.
// In general, only the Default registry is ever used.
type Registry struct {
// A *Registry type is actually just a reference handle to *registryInternal.
// There may be multiple different *Registry types pointing at the same
// *registryInternal but with different transformer sets, to make
// WithTransformers work right.
*registryInternal
transformers []CallbackTransformer
}
// NewRegistry creates a NewRegistry, though you almost certainly just want
// to use Default.
func NewRegistry() *Registry {
return &Registry{
registryInternal: ®istryInternal{
traceWatchers: map[int64]func(*Trace){},
scopes: map[string]*Scope{},
spans: map[*Span]struct{}{},
orphans: map[*Span]struct{}{}}}
}
// WithTransformers returns a copy of Registry but with the additional
// CallbackTransformers applied to the Stats method.
func (r *Registry) WithTransformers(t ...CallbackTransformer) *Registry {
return &Registry{
registryInternal: r.registryInternal,
transformers: append(append([]CallbackTransformer(nil), r.transformers...), t...),
}
}
// Package creates a new monitoring Scope, named after the top level package.
// It's expected that you'll have something like
//
// var mon = monkit.Package()
//
// at the top of each package.
func (r *Registry) Package() *Scope {
return r.ScopeNamed(callerPackage(1))
}
// ScopeNamed is like Package, but lets you choose the name.
func (r *Registry) ScopeNamed(name string) *Scope {
r.scopeMtx.Lock()
defer r.scopeMtx.Unlock()
s, exists := r.scopes[name]
if exists {
return s
}
s = newScope(r, name)
r.scopes[name] = s
return s
}
func (r *Registry) observeTrace(t *Trace) {
watcher := loadTraceWatcherRef(&r.traceWatcher)
if watcher != nil {
watcher.watcher(t)
}
}
func (r *Registry) updateWatcher() {
cbs := make([]func(*Trace), 0, len(r.traceWatchers))
for _, cb := range r.traceWatchers {
cbs = append(cbs, cb)
}
switch len(cbs) {
case 0:
storeTraceWatcherRef(&r.traceWatcher, nil)
case 1:
storeTraceWatcherRef(&r.traceWatcher,
&traceWatcherRef{watcher: cbs[0]})
default:
storeTraceWatcherRef(&r.traceWatcher,
&traceWatcherRef{watcher: func(t *Trace) {
for _, cb := range cbs {
cb(t)
}
}})
}
}
// ObserveTraces lets you observe all traces flowing through the system.
// The passed in callback 'cb' will be called for every new trace as soon as
// it starts, until the returned cancel method is called.
// Note: this only applies to all new traces. If you want to find existing
// or running traces, please pull them off of live RootSpans.
func (r *Registry) ObserveTraces(cb func(*Trace)) (cancel func()) {
// even though observeTrace doesn't get a mutex, it's only ever loading
// the traceWatcher pointer, so we can use this mutex here to safely
// coordinate the setting of the traceWatcher pointer.
r.watcherMtx.Lock()
defer r.watcherMtx.Unlock()
cbId := r.watcherCounter
r.watcherCounter += 1
r.traceWatchers[cbId] = cb
r.updateWatcher()
return func() {
r.watcherMtx.Lock()
defer r.watcherMtx.Unlock()
delete(r.traceWatchers, cbId)
r.updateWatcher()
}
}
func (r *Registry) rootSpanStart(s *Span) {
r.spanMtx.Lock()
r.spans[s] = struct{}{}
r.spanMtx.Unlock()
}
func (r *Registry) rootSpanEnd(s *Span) {
r.spanMtx.Lock()
delete(r.spans, s)
r.spanMtx.Unlock()
}
func (r *Registry) orphanedSpan(s *Span) {
r.orphanMtx.Lock()
r.orphans[s] = struct{}{}
r.orphanMtx.Unlock()
}
func (r *Registry) orphanEnd(s *Span) {
r.orphanMtx.Lock()
delete(r.orphans, s)
r.orphanMtx.Unlock()
}
// RootSpans will call 'cb' on all currently executing Spans with no live or
// reachable parent. See also AllSpans.
func (r *Registry) RootSpans(cb func(s *Span)) {
r.spanMtx.Lock()
spans := make([]*Span, 0, len(r.spans))
for s := range r.spans {
spans = append(spans, s)
}
r.spanMtx.Unlock()
r.orphanMtx.Lock()
orphans := make([]*Span, 0, len(r.orphans))
for s := range r.orphans {
orphans = append(orphans, s)
}
r.orphanMtx.Unlock()
spans = append(spans, orphans...)
sort.Sort(spanSorter(spans))
for _, s := range spans {
cb(s)
}
}
func walkSpan(s *Span, cb func(s *Span)) {
cb(s)
s.Children(func(s *Span) {
walkSpan(s, cb)
})
}
// AllSpans calls 'cb' on all currently known Spans. See also RootSpans.
func (r *Registry) AllSpans(cb func(s *Span)) {
r.RootSpans(func(s *Span) { walkSpan(s, cb) })
}
// Scopes calls 'cb' on all currently known Scopes.
func (r *Registry) Scopes(cb func(s *Scope)) {
r.scopeMtx.Lock()
c := make([]*Scope, 0, len(r.scopes))
for _, s := range r.scopes {
c = append(c, s)
}
r.scopeMtx.Unlock()
sort.Sort(scopeSorter(c))
for _, s := range c {
cb(s)
}
}
// Funcs calls 'cb' on all currently known Funcs.
func (r *Registry) Funcs(cb func(f *Func)) {
r.Scopes(func(s *Scope) { s.Funcs(cb) })
}
// Stats implements the StatSource interface.
func (r *Registry) Stats(cb func(key SeriesKey, field string, val float64)) {
for _, t := range r.transformers {
cb = t.Transform(cb)
}
r.Scopes(func(s *Scope) { s.Stats(cb) })
}
var _ StatSource = (*Registry)(nil)
// Default is the default Registry
var Default = NewRegistry()
// ScopeNamed is just a wrapper around Default.ScopeNamed
func ScopeNamed(name string) *Scope { return Default.ScopeNamed(name) }
// RootSpans is just a wrapper around Default.RootSpans
func RootSpans(cb func(s *Span)) { Default.RootSpans(cb) }
// Scopes is just a wrapper around Default.Scopes
func Scopes(cb func(s *Scope)) { Default.Scopes(cb) }
// Funcs is just a wrapper around Default.Funcs
func Funcs(cb func(f *Func)) { Default.Funcs(cb) }
// Package is just a wrapper around Default.Package
func Package() *Scope { return Default.ScopeNamed(callerPackage(1)) }
// Stats is just a wrapper around Default.Stats
func Stats(cb func(key SeriesKey, field string, val float64)) { Default.Stats(cb) }
type spanSorter []*Span
func (s spanSorter) Len() int { return len(s) }
func (s spanSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s spanSorter) Less(i, j int) bool {
ispan, jspan := s[i], s[j]
iname, jname := ispan.f.FullName(), jspan.f.FullName()
return (iname < jname) || (iname == jname && ispan.id < jspan.id)
}
type scopeSorter []*Scope
func (s scopeSorter) Len() int { return len(s) }
func (s scopeSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s scopeSorter) Less(i, j int) bool { return s[i].name < s[j].name }