forked from TheThingsArchive/ttn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
180 lines (157 loc) · 4.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
package stats
import (
"fmt"
"reflect"
"sync"
"github.com/rcrowley/go-metrics"
)
// Ticker has a Tick() func
type Ticker interface {
Tick()
SetDefaultTTL(uint)
SetTTL(string, uint)
Renew(string)
}
// DefaultTTL for the registry
var DefaultTTL uint = 0
// Registry is the default metrics registry
var Registry = NewRegistry()
// TTNRegistry is a mutex-protected map of names to metrics.
// It is basically an extension of https://github.com/rcrowley/go-metrics/blob/master/registry.go
type TTNRegistry struct {
metrics map[string]interface{}
timeouts map[string]uint
defaultTTL uint
mutex sync.Mutex
}
// NewRegistry reates a new registry and starts a goroutine for the TTL.
func NewRegistry() metrics.Registry {
return &TTNRegistry{
metrics: make(map[string]interface{}),
timeouts: make(map[string]uint),
defaultTTL: DefaultTTL,
}
}
// Each calls the given function for each registered metric.
func (r *TTNRegistry) Each(f func(string, interface{})) {
for name, i := range r.registered() {
f(name, i)
}
}
// Get the metric by the given name or nil if none is registered.
func (r *TTNRegistry) Get(name string) interface{} {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.metrics[name]
}
// GetOrRegister gets an existing metric or creates and registers a new one. Threadsafe
// alternative to calling Get and Register on failure.
// The interface can be the metric to register if not found in registry,
// or a function returning the metric for lazy instantiation.
func (r *TTNRegistry) GetOrRegister(name string, i interface{}) interface{} {
r.mutex.Lock()
defer r.mutex.Unlock()
if metric, ok := r.metrics[name]; ok {
return metric
}
if v := reflect.ValueOf(i); v.Kind() == reflect.Func {
i = v.Call(nil)[0].Interface()
}
r.register(name, i)
return i
}
// Register the given metric under the given name. Returns a DuplicateMetric
// if a metric by the given name is already registered.
func (r *TTNRegistry) Register(name string, i interface{}) error {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.register(name, i)
}
// RunHealthchecks runs all registered healthchecks.
func (r *TTNRegistry) RunHealthchecks() {
r.mutex.Lock()
defer r.mutex.Unlock()
for _, i := range r.metrics {
if h, ok := i.(metrics.Healthcheck); ok {
h.Check()
}
}
}
// Unregister the metric with the given name.
func (r *TTNRegistry) Unregister(name string) {
r.mutex.Lock()
defer r.mutex.Unlock()
delete(r.metrics, name)
}
// UnregisterAll unregisters all metrics. (Mostly for testing.)
func (r *TTNRegistry) UnregisterAll() {
r.mutex.Lock()
defer r.mutex.Unlock()
for name := range r.metrics {
delete(r.metrics, name)
}
}
// Tick decreases the TTL of all metrics that have one
// If the TTL becomes zero, the metric is deleted
func (r *TTNRegistry) Tick() {
r.mutex.Lock()
defer r.mutex.Unlock()
for name, ttl := range r.timeouts {
r.timeouts[name] = ttl - 1
if ttl == 0 {
delete(r.metrics, name)
delete(r.timeouts, name)
}
}
}
// SetDefaultTTL sets a default TTL of a number of ticks for all new metrics
func (r *TTNRegistry) SetDefaultTTL(ticks uint) {
r.defaultTTL = ticks
}
// SetTTL sets a TTL of a number of ticks for a given metric
func (r *TTNRegistry) SetTTL(name string, ticks uint) {
r.mutex.Lock()
defer r.mutex.Unlock()
if _, ok := r.metrics[name]; ok {
// Ignore if the existing ttl is higher than the new ttl
if ttl, ok := r.timeouts[name]; ok && ttl > ticks {
return
}
r.timeouts[name] = ticks
}
}
// Renew sets the TTL of a metric to the default value
func (r *TTNRegistry) Renew(name string) {
if r.defaultTTL == 0 {
return
}
r.mutex.Lock()
defer r.mutex.Unlock()
_, ok := r.metrics[name]
ttl, _ := r.timeouts[name]
if ok && r.defaultTTL > ttl {
r.timeouts[name] = r.defaultTTL
}
}
func (r *TTNRegistry) register(name string, i interface{}) error {
if _, ok := r.metrics[name]; ok {
return fmt.Errorf("duplicate metric: %s", name)
}
switch i.(type) {
case metrics.Counter, metrics.Gauge, metrics.GaugeFloat64, metrics.Healthcheck, metrics.Histogram, metrics.Meter, metrics.Timer, String:
r.metrics[name] = i
if r.defaultTTL > 0 {
r.timeouts[name] = r.defaultTTL
}
}
return nil
}
func (r *TTNRegistry) registered() map[string]interface{} {
r.mutex.Lock()
defer r.mutex.Unlock()
metrics := make(map[string]interface{}, len(r.metrics))
for name, i := range r.metrics {
metrics[name] = i
}
return metrics
}