-
Notifications
You must be signed in to change notification settings - Fork 1
/
registry.go
365 lines (305 loc) · 9.44 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
Copyright NetFoundry 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
https://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 metrics
import (
"fmt"
"reflect"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/metrics/metrics_pb"
cmap "github.com/orcaman/concurrent-map/v2"
"github.com/rcrowley/go-metrics"
)
// Metric is the base functionality for all metrics types
type Metric interface {
Dispose()
}
// Registry allows for configuring and accessing metrics for an application
type Registry interface {
// SourceId returns the source id of this Registry
SourceId() string
// Gauge returns a Gauge for the given name. If one does not yet exist, one will be created
Gauge(name string) Gauge
// FuncGauge returns a Gauge for the given name. If one does not yet exist, one will be created using
// the given function
FuncGauge(name string, f func() int64) Gauge
// Meter returns a Meter for the given name. If one does not yet exist, one will be created
Meter(name string) Meter
// Histogram returns a Histogram for the given name. If one does not yet exist, one will be created
Histogram(name string) Histogram
// Timer returns a Timer for the given name. If one does not yet exist, one will be created
Timer(name string) Timer
// EachMetric calls the given visitor function for each Metric in this registry
EachMetric(visitor func(name string, metric Metric))
// GetGauge returns the Gauge for the given name or nil if a Gauge with that name doesn't exist
GetGauge(name string) Gauge
// GetMeter returns the Meter for the given name or nil if a Meter with that name doesn't exist
GetMeter(name string) Meter
// GetHistogram returns the Histogram for the given name or nil if a Histogram with that name doesn't exist
GetHistogram(name string) Histogram
// GetTimer returns the Timer for the given name or nil if a Timer with that name doesn't exist
GetTimer(name string) Timer
// IsValidMetric returns true if a metric with the given name exists in the registry, false otherwise
IsValidMetric(name string) bool
// Poll returns a MetricsMessage with a snapshot of the metrics in the Registry
Poll() *metrics_pb.MetricsMessage
// DisposeAll removes and cleans up all metrics currently in the Registry
DisposeAll()
}
func NewRegistry(sourceId string, tags map[string]string) Registry {
return ®istryImpl{
sourceId: sourceId,
tags: tags,
metricMap: cmap.New[any](),
}
}
type registryImpl struct {
sourceId string
tags map[string]string
metricMap cmap.ConcurrentMap[string, any]
}
func (registry *registryImpl) dispose(name string) {
registry.metricMap.Remove(name)
}
func (registry *registryImpl) DisposeAll() {
registry.EachMetric(func(name string, metric Metric) {
metric.Dispose()
})
}
func (registry *registryImpl) IsValidMetric(name string) bool {
return registry.metricMap.Has(name)
}
func (registry *registryImpl) SourceId() string {
return registry.sourceId
}
func (registry *registryImpl) GetGauge(name string) Gauge {
metric, found := registry.metricMap.Get(name)
if !found {
return nil
}
if gauge, ok := metric.(Gauge); ok {
return gauge
}
return nil
}
func (registry *registryImpl) GetMeter(name string) Meter {
metric, found := registry.metricMap.Get(name)
if !found {
return nil
}
if meter, ok := metric.(Meter); ok {
return meter
}
return nil
}
func (registry *registryImpl) GetHistogram(name string) Histogram {
metric, found := registry.metricMap.Get(name)
if !found {
return nil
}
if histogram, ok := metric.(Histogram); ok {
return histogram
}
return nil
}
func (registry *registryImpl) GetTimer(name string) Timer {
metric, found := registry.metricMap.Get(name)
if !found {
return nil
}
if timer, ok := metric.(Timer); ok {
return timer
}
return nil
}
func getOrCreateMetric[T Metric](registry *registryImpl, name string, newMetric func() T) T {
var result T
for {
metric, present := registry.metricMap.Get(name)
if present {
var ok bool
result, ok = metric.(T)
if !ok {
panic(fmt.Errorf("metric '%v' already exists and is not a %T. It is a %T", name, new(T), metric))
}
return result
}
result = newMetric()
if registry.metricMap.SetIfAbsent(name, result) {
return result
}
}
}
func (registry *registryImpl) Gauge(name string) Gauge {
return getOrCreateMetric(registry, name, func() Gauge {
return &gaugeImpl{
Gauge: metrics.NewGauge(),
dispose: func() {
registry.dispose(name)
},
}
})
}
func (registry *registryImpl) FuncGauge(name string, f func() int64) Gauge {
return getOrCreateMetric(registry, name, func() Gauge {
return &gaugeImpl{
Gauge: metrics.NewFunctionalGauge(f),
dispose: func() {
registry.dispose(name)
},
}
})
}
func (registry *registryImpl) newMeter(name string) *meterImpl {
return &meterImpl{
Meter: metrics.NewMeter(),
registry: registry,
name: name,
}
}
func (registry *registryImpl) Meter(name string) Meter {
metric := registry.getRefCounted(name, func() refCounted {
return registry.newMeter(name)
})
meter, ok := metric.(Meter)
if !ok {
panic(fmt.Errorf("metric '%v' already exists and is not a meter. It is a %v", name, reflect.TypeOf(metric).Name()))
}
return meter
}
func (registry *registryImpl) newHistogram(name string) *histogramImpl {
return &histogramImpl{
Histogram: metrics.NewHistogram(metrics.NewExpDecaySample(128, 0.015)),
registry: registry,
name: name,
}
}
func (registry *registryImpl) Histogram(name string) Histogram {
metric := registry.getRefCounted(name, func() refCounted {
return registry.newHistogram(name)
})
histogram, ok := metric.(Histogram)
if !ok {
panic(fmt.Errorf("metric '%v' already exists and is not a histogram. It is a %v", name, reflect.TypeOf(metric).Name()))
}
return histogram
}
func (registry *registryImpl) getRefCounted(name string, factory func() refCounted) refCounted {
metric := registry.metricMap.Upsert(name, nil, func(exist bool, valueInMap interface{}, newValue interface{}) interface{} {
if exist {
if h, ok := valueInMap.(refCounted); ok {
h.IncrRefCount()
}
return valueInMap
}
newVal := factory()
newVal.IncrRefCount()
return newVal
})
histogram, ok := metric.(refCounted)
if !ok {
panic(fmt.Errorf("metric '%v' already exists and is not an instance of refCouted. It is a %v", name, reflect.TypeOf(metric).Name()))
}
return histogram
}
func (registry *registryImpl) disposeRefCounted(metric refCounted) {
registry.metricMap.RemoveCb(metric.Name(), func(key string, v interface{}, exists bool) bool {
if !exists {
return true
}
return v == metric && metric.DecrRefCount() < 1
})
}
func (registry *registryImpl) Timer(name string) Timer {
return getOrCreateMetric(registry, name, func() Timer {
return &timerImpl{
Timer: metrics.NewTimer(),
dispose: func() {
registry.dispose(name)
},
}
})
}
func (registry *registryImpl) EachMetric(visitor func(name string, metric Metric)) {
for entry := range registry.metricMap.IterBuffered() {
visitor(entry.Key, entry.Val.(Metric))
}
}
func (registry *registryImpl) Each(visitor func(string, interface{})) {
for entry := range registry.metricMap.IterBuffered() {
visitor(entry.Key, entry.Val)
}
}
// Provide rest of go-metrics Registry interface, so we can use go-metrics reporters if desired
func (registry *registryImpl) Get(s string) interface{} {
val, _ := registry.metricMap.Get(s)
return val
}
func (registry *registryImpl) GetAll() map[string]map[string]interface{} {
return nil
}
func (registry *registryImpl) GetOrRegister(s string, i interface{}) interface{} {
return registry.metricMap.Upsert(s, i, func(exist bool, valueInMap interface{}, newValue interface{}) interface{} {
if exist {
return valueInMap
}
return newValue
})
}
func (registry *registryImpl) Register(s string, i interface{}) error {
if registry.metricMap.SetIfAbsent(s, i) {
return fmt.Errorf("duplicate metric %v", s)
}
return nil
}
func (registry *registryImpl) RunHealthchecks() {
}
func (registry *registryImpl) Unregister(s string) {
registry.metricMap.Remove(s)
}
func (registry *registryImpl) UnregisterAll() {
for _, key := range registry.metricMap.Keys() {
registry.Unregister(key)
}
}
func (registry *registryImpl) Poll() *metrics_pb.MetricsMessage {
// If there's nothing to report, skip it
if registry.metricMap.Count() == 0 {
return nil
}
builder := newMessageBuilder(registry.sourceId, registry.tags)
registry.EachMetric(func(name string, i Metric) {
switch metric := i.(type) {
case *gaugeImpl:
builder.addIntGauge(name, metric.Snapshot())
case *meterImpl:
builder.addMeter(name, metric.Snapshot())
case *histogramImpl:
builder.addHistogram(name, metric.Snapshot())
case *timerImpl:
builder.addTimer(name, metric.Snapshot())
case *intervalCounterImpl:
// ignore, handled below
case *usageCounterImpl:
// ignore, handled below
default:
pfxlog.Logger().Errorf("Unsupported metric type %v", reflect.TypeOf(i))
}
})
return (*metrics_pb.MetricsMessage)(builder)
}
type refCounted interface {
IncrRefCount() int32
DecrRefCount() int32
Name() string
stop()
}