-
Notifications
You must be signed in to change notification settings - Fork 20
/
registry.go
132 lines (123 loc) · 4.47 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
package monitoring
import (
"sort"
"strings"
"sync"
"github.com/go-masonry/mortar/interfaces/monitor"
)
type externalRegistry struct {
cm sync.RWMutex
gm sync.RWMutex
tm sync.RWMutex
hm sync.RWMutex
external monitor.BricksMetrics
// TODO perhaps change this to self evicting cache that will remove metrics if unused for a long time to save space
counters *sync.Map
gauges *sync.Map
histograms *sync.Map
timers *sync.Map
}
func newRegistry(externalMetrics monitor.BricksMetrics) *externalRegistry {
return &externalRegistry{
external: externalMetrics,
counters: new(sync.Map),
gauges: new(sync.Map),
histograms: new(sync.Map),
timers: new(sync.Map),
}
}
func (r *externalRegistry) loadOrStoreCounter(name, desc string, keys ...string) (bricksCounter monitor.BricksCounter, err error) {
ID := calcID(name, keys...)
if known, ok := r.counters.Load(ID); ok {
return known.(monitor.BricksCounter), nil
}
r.cm.Lock()
defer r.cm.Unlock()
bricksCounter, err = r.external.Counter(name, desc, keys...)
if err == nil {
cacheValue, _ := r.counters.LoadOrStore(ID, bricksCounter) // if a previous duplicate is already there (was created by other go routine)
bricksCounter = cacheValue.(monitor.BricksCounter)
} else {
if known, ok := r.counters.Load(ID); ok { // perhaps it's already there (was created by other go routine) and the underlying impl have a dup check
bricksCounter, err = known.(monitor.BricksCounter), nil
return
}
}
return
}
func (r *externalRegistry) loadOrStoreGauge(name, desc string, keys ...string) (bricksGauge monitor.BricksGauge, err error) {
ID := calcID(name, keys...)
if known, ok := r.gauges.Load(ID); ok {
return known.(monitor.BricksGauge), nil
}
r.gm.Lock()
defer r.gm.Unlock()
bricksGauge, err = r.external.Gauge(name, desc, keys...)
if err == nil {
cacheValue, _ := r.gauges.LoadOrStore(ID, bricksGauge) // if a previous duplicate is already there (was created by other go routine)
bricksGauge = cacheValue.(monitor.BricksGauge)
} else {
if known, ok := r.gauges.Load(ID); ok { // perhaps it's already there (was created by other go routine) and the underlying impl have a dup check
bricksGauge, err = known.(monitor.BricksGauge), nil
return
}
}
return
}
func (r *externalRegistry) loadOrStoreHistogram(name, desc string, buckets monitor.Buckets, keys ...string) (bricksHistogram monitor.BricksHistogram, err error) {
ID := calcID(name, keys...)
if known, ok := r.histograms.Load(ID); ok {
return known.(monitor.BricksHistogram), nil
}
r.hm.Lock()
defer r.hm.Unlock()
bricksHistogram, err = r.external.Histogram(name, desc, buckets, keys...)
if err == nil {
cacheValue, _ := r.histograms.LoadOrStore(ID, bricksHistogram) // if a previous duplicate is already there (was created by other go routine)
bricksHistogram = cacheValue.(monitor.BricksHistogram)
} else {
if known, ok := r.histograms.Load(ID); ok { // perhaps it's already there (was created by other go routine) and the underlying impl have a dup check
bricksHistogram, err = known.(monitor.BricksHistogram), nil
return
}
}
return
}
func (r *externalRegistry) loadOrStoreTimer(name, desc string, keys ...string) (bricksTimer monitor.BricksTimer, err error) {
ID := calcID(name, keys...)
if known, ok := r.timers.Load(ID); ok {
return known.(monitor.BricksTimer), nil
}
r.tm.Lock()
defer r.tm.Unlock()
bricksTimer, err = r.external.Timer(name, desc, keys...)
if err == nil {
cacheValue, _ := r.timers.LoadOrStore(ID, bricksTimer) // if a previous duplicate is already there (was created by other go routine)
bricksTimer = cacheValue.(monitor.BricksTimer)
} else {
if known, ok := r.timers.Load(ID); ok { // perhaps it's already there (was created by other go routine) and the underlying impl have a dup check
bricksTimer, err = known.(monitor.BricksTimer), nil
return
}
}
return
}
func calcID(name string, keys ...string) (ID string) {
if len(keys) > 0 {
var stringsSet = make(map[string]struct{}, len(keys))
var parts = make([]string, 0, len(keys)+1) // preallocate slice with extra space
for _, key := range keys {
if _, ok := stringsSet[key]; !ok {
stringsSet[key] = struct{}{}
parts = append(parts, key)
}
}
sort.Strings(parts)
// avoid allocation and prepend name
parts = append(parts, "") // add empty string to the end -> len++
copy(parts[1:], parts[0:]) // shift
parts[0] = name // name is the first string now
return strings.Join(parts, "_")
}
return name
}