-
Notifications
You must be signed in to change notification settings - Fork 1
/
registry.go
250 lines (219 loc) · 5.27 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
package instruments
import (
"log"
"math"
"os"
"sync"
"time"
)
// Logger allows to plug in a logger.
type Logger interface {
Printf(string, ...interface{})
}
// Registry is a registry of all instruments.
type Registry struct {
Logger Logger
instruments map[string]interface{}
reporters []Reporter
prefix string
tags []string
closing chan struct{}
closed chan error
mutex sync.RWMutex
}
// New creates a new Registry with a flushInterval at which metrics
// are reported to the subscribed Reporter instances, a custom prefix
// which is prepended to every metric name and default tags.
// Default: 60s
//
// You should call/defer Close() on exit to flush all
// accummulated data and release all resources.
func New(flushInterval time.Duration, prefix string, tags ...string) *Registry {
if flushInterval < time.Second {
flushInterval = 30 * time.Second
}
r := &Registry{
Logger: log.New(os.Stderr, "instruments: ", log.LstdFlags),
instruments: make(map[string]interface{}),
prefix: prefix,
tags: tags,
closing: make(chan struct{}),
closed: make(chan error, 1),
}
go r.loop(flushInterval)
return r
}
// NewUnstarted creates a new Registry without a background flush thread.
func NewUnstarted(prefix string, tags ...string) *Registry {
return &Registry{
instruments: make(map[string]interface{}),
prefix: prefix,
tags: tags,
}
}
// Subscribe attaches a reporter to the Registry.
func (r *Registry) Subscribe(rep Reporter) {
r.mutex.Lock()
r.reporters = append(r.reporters, rep)
r.mutex.Unlock()
}
// Get returns an instrument from the Registry.
func (r *Registry) Get(name string, tags []string) interface{} {
key := MetricID(name, tags)
r.mutex.RLock()
v := r.instruments[key]
r.mutex.RUnlock()
return v
}
// Register registers a new instrument.
func (r *Registry) Register(name string, tags []string, v interface{}) {
switch v.(type) {
case Discrete, Sample:
key := MetricID(name, tags)
r.mutex.Lock()
r.instruments[key] = v
r.mutex.Unlock()
}
}
// Unregister remove from the registry the instrument matching the given name/tags
func (r *Registry) Unregister(name string, tags []string) {
key := MetricID(name, tags)
r.mutex.Lock()
delete(r.instruments, key)
r.mutex.Unlock()
}
// Fetch returns an instrument from the Registry or creates a new one
// using the provided factory.
func (r *Registry) Fetch(name string, tags []string, factory func() interface{}) interface{} {
key := MetricID(name, tags)
r.mutex.RLock()
v, ok := r.instruments[key]
r.mutex.RUnlock()
if ok {
return v
}
r.mutex.Lock()
defer r.mutex.Unlock()
if v, ok = r.instruments[key]; !ok {
switch v = factory(); v.(type) {
case Discrete, Sample:
r.instruments[key] = v
}
}
return v
}
// Size returns the numbers of instruments in the registry.
func (r *Registry) Size() int {
r.mutex.RLock()
size := len(r.instruments)
r.mutex.RUnlock()
return size
}
// Flush performs a manual flush to all subscribed reporters.
// This method is usually called by a background thread
// every flushInterval, specified in New()
func (r *Registry) Flush() error {
r.mutex.RLock()
reporters := r.reporters
rtags := r.tags
r.mutex.RUnlock()
for _, rep := range reporters {
if err := rep.Prep(); err != nil {
return err
}
}
for metricID, val := range r.reset() {
name, tags := SplitMetricID(metricID)
if len(name) > 0 && name[0] == '|' {
name = name[1:]
} else {
name = r.prefix + name
}
tags = append(tags, rtags...)
switch inst := val.(type) {
case Discrete:
val := inst.Snapshot()
if math.IsNaN(val) || math.IsInf(val, 0) {
break
}
for _, rep := range reporters {
if err := rep.Discrete(name, tags, val); err != nil {
return err
}
}
case Sample:
val := inst.Snapshot()
if val.Count() == 0 {
break
}
for _, rep := range reporters {
if err := rep.Sample(name, tags, val); err != nil {
return err
}
}
releaseDistribution(val)
}
}
for _, rep := range reporters {
if err := rep.Flush(); err != nil {
return err
}
}
return nil
}
// Tags returns global registry tags
func (r *Registry) Tags() []string {
r.mutex.RLock()
tags := r.tags
r.mutex.RUnlock()
return tags
}
// SetTags allows to set tags
func (r *Registry) SetTags(tags ...string) {
r.mutex.Lock()
r.tags = tags
r.mutex.Unlock()
}
// AddTags allows to add tags
func (r *Registry) AddTags(tags ...string) {
r.mutex.Lock()
r.tags = append(r.tags, tags...)
r.mutex.Unlock()
}
// Close flushes all pending data to reporters
// and releases resources.
func (r *Registry) Close() error {
if r.closing == nil {
return nil
}
close(r.closing)
return <-r.closed
}
func (r *Registry) reset() map[string]interface{} {
r.mutex.Lock()
instruments := r.instruments
r.instruments = make(map[string]interface{})
r.mutex.Unlock()
return instruments
}
func (r *Registry) loop(flushInterval time.Duration) {
flusher := time.NewTicker(flushInterval)
defer flusher.Stop()
for {
select {
case <-r.closing:
r.closed <- r.Flush()
close(r.closed)
return
case <-flusher.C:
if err := r.Flush(); err != nil {
r.logf("flush error: %s", err.Error())
}
}
}
}
func (r *Registry) logf(s string, v ...interface{}) {
if r.Logger != nil {
r.Logger.Printf(s, v...)
}
}