/
registry.go
133 lines (112 loc) · 3.1 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
package reporter
import (
"sync"
"github.com/heroku/instruments"
)
// DefaultRegistry is the default registry.
var DefaultRegistry = NewRegistry()
// Register a new instruments in the default registry.
func Register(name string, v interface{}) interface{} {
return DefaultRegistry.Register(name, v)
}
// Get returns the named instruments from the default registry.
func Get(name string) interface{} {
return DefaultRegistry.Get(name)
}
// Unregister remove the named instruments from the default registry.
func Unregister(name string) {
DefaultRegistry.Unregister(name)
}
// Snapshot returns all instruments and reset the default registry.
func Snapshot() map[string]interface{} {
return DefaultRegistry.Snapshot()
}
// Registry is a registry of all instruments.
type Registry struct {
instruments map[string]interface{}
m sync.RWMutex
}
// NewRegistry creates a new Register.
func NewRegistry() *Registry {
return &Registry{
instruments: make(map[string]interface{}),
}
}
// Get returns an instrument from the Registry.
func (r *Registry) Get(name string) interface{} {
r.m.RLock()
defer r.m.RUnlock()
return r.instruments[name]
}
// Register registers a new instrument or return the existing one.
func (r *Registry) Register(name string, v interface{}) interface{} {
r.m.Lock()
defer r.m.Unlock()
switch v.(type) {
case instruments.Discrete, instruments.Sample:
i, present := r.instruments[name]
if present {
return i
}
r.instruments[name] = v
return v
}
return nil
}
// Unregister remove from the registry the instrument matching the given name.
func (r *Registry) Unregister(name string) {
r.m.Lock()
defer r.m.Unlock()
delete(r.instruments, name)
}
// Snapshot returns and reset all instruments.
func (r *Registry) Snapshot() map[string]interface{} {
r.m.Lock()
defer r.m.Unlock()
instruments := r.instruments
r.instruments = make(map[string]interface{})
return instruments
}
// Instruments returns all instruments.
func (r *Registry) Instruments() map[string]interface{} {
r.m.RLock()
defer r.m.RUnlock()
instruments := r.instruments
return instruments
}
// Size returns the numbers of instruments in the registry.
func (r *Registry) Size() int {
r.m.RLock()
defer r.m.RUnlock()
return len(r.instruments)
}
func NewRegisteredCounter(name string) *instruments.Counter {
counter := instruments.NewCounter()
Register(name, counter)
return counter
}
func NewRegisteredRate(name string) *instruments.Rate {
rate := instruments.NewRate()
Register(name, rate)
return rate
}
func NewRegisteredDerive(name string, value int64) *instruments.Derive {
derive := instruments.NewDerive(value)
Register(name, derive)
return derive
}
func NewRegisteredReservoir(name string, size int64) *instruments.Reservoir {
reservoir := instruments.NewReservoir(size)
Register(name, reservoir)
return reservoir
}
func NewRegisteredGauge(name string, value int64) *instruments.Gauge {
gauge := instruments.NewGauge(value)
Register(name, gauge)
return gauge
}
func NewRegisteredTimer(name string, size int64) *instruments.Timer {
timer := instruments.NewTimer(size)
Register(name, timer)
return timer
}