-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
499 lines (426 loc) · 15.4 KB
/
metrics.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package servicefoundation
import (
"fmt"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
)
type (
// HistogramVec is a wrapper around the HistogramVec from the go-metrics package.
HistogramVec interface {
RecordTimeElapsed(start time.Time)
RecordDuration(start time.Time, unit time.Duration)
}
// SummaryVec is a wrapper around the v from the go-metrics package.
SummaryVec interface {
RecordTimeElapsed(start time.Time)
RecordDuration(start time.Time, unit time.Duration)
}
// Metrics is a wrapper around the Metrics from the go-metrics package.
Metrics interface {
Count(subsystem, name, help string)
SetGauge(value float64, subsystem, name, help string)
CountLabels(subsystem, name, help string, labels, values []string)
IncreaseCounter(subsystem, name, help string, increment int)
AddHistogramVec(subsystem, name, help string, labels, labelValues []string) HistogramVec
AddSummaryVec(subsystem, name, help string, labels, labelValues []string) SummaryVec
}
histogramVecImpl struct {
histogramVec *histogramVec
}
summaryVecImpl struct {
summaryVec *summaryVec
}
metricsImpl struct {
internalMetrics *metrics
externalMetrics *metrics
}
)
// NewMetrics instantiates a new Metrics implementation.
func NewMetrics(namespace string, logger Logger) Metrics {
return &metricsImpl{
internalMetrics: newMetrics("", logger),
externalMetrics: newMetrics(strings.ToLower(namespace), logger),
}
}
/* HistogramVec implementation */
func (h *histogramVecImpl) RecordTimeElapsed(start time.Time) {
h.histogramVec.RecordTimeElapsed(start)
}
func (h *histogramVecImpl) RecordDuration(start time.Time, unit time.Duration) {
h.histogramVec.RecordDuration(start, unit)
}
/* SummaryVec implementation */
func (s *summaryVecImpl) RecordTimeElapsed(start time.Time) {
s.summaryVec.RecordTimeElapsed(start)
}
func (s *summaryVecImpl) RecordDuration(start time.Time, unit time.Duration) {
s.summaryVec.RecordDuration(start, unit)
}
/* Metrics implementation */
func (m *metricsImpl) Count(subsystem, name, help string) {
m.getMetrics(subsystem).Count(subsystem, name, help)
}
func (m *metricsImpl) SetGauge(value float64, subsystem, name, help string) {
m.getMetrics(subsystem).SetGauge(value, subsystem, name, help)
}
func (m *metricsImpl) CountLabels(subsystem, name, help string, labels, values []string) {
m.getMetrics(subsystem).CountLabels(subsystem, name, help, labels, values)
}
func (m *metricsImpl) IncreaseCounter(subsystem, name, help string, increment int) {
m.getMetrics(subsystem).IncreaseCounter(subsystem, name, help, increment)
}
func (m *metricsImpl) AddHistogramVec(subsystem, name, help string, labels, labelValues []string) HistogramVec {
return &histogramVecImpl{m.getMetrics(subsystem).AddHistogramVec(subsystem, name, help, labels, labelValues)}
}
func (m *metricsImpl) AddSummaryVec(subsystem, name, help string, labels, labelValues []string) SummaryVec {
return &summaryVecImpl{m.getMetrics(subsystem).AddSummaryVec(subsystem, name, help, labels, labelValues)}
}
func (m *metricsImpl) getMetrics(subsystem string) *metrics {
if subsystem == "" {
return m.internalMetrics
}
return m.externalMetrics
}
/*
Internal metrics implementation (copied from github.com/Travix-International/go-metrics)
*/
type (
// metrics provides a set of convenience functions that wrap Prometheus
metrics struct {
Namespace string
Counters map[string]prometheus.Counter
CounterVecs map[string]*prometheus.CounterVec
Summaries map[string]prometheus.Summary
SummaryVecs map[string]*prometheus.SummaryVec
Histograms map[string]prometheus.Histogram
HistogramVecs map[string]*prometheus.HistogramVec
Gauges map[string]prometheus.Gauge
Logger Logger
countMutex *sync.RWMutex
countVecMutex *sync.RWMutex
histMutex *sync.RWMutex
histVecMutex *sync.RWMutex
summaryVecMutex *sync.RWMutex
gaugeMutex *sync.RWMutex
}
// metricsHistogram combines a histogram and summary
metricsHistogram struct {
Key string
hist prometheus.Histogram
sum prometheus.Summary
}
// histogramVec wraps prometheus.HistogramVec
histogramVec struct {
Key string
Labels []string
LabelValues []string
histVec *prometheus.HistogramVec
}
// summaryVec wraps prometheus.SummaryVec
summaryVec struct {
Key string
Labels []string
LabelValues []string
summaryVec *prometheus.SummaryVec
}
)
// newMetrics will instantiate a new Metrics wrapper object
func newMetrics(namespace string, logger Logger) *metrics {
m := metrics{
Namespace: namespace,
Logger: logger,
Counters: make(map[string]prometheus.Counter),
CounterVecs: make(map[string]*prometheus.CounterVec),
Histograms: make(map[string]prometheus.Histogram),
HistogramVecs: make(map[string]*prometheus.HistogramVec),
Summaries: make(map[string]prometheus.Summary),
SummaryVecs: make(map[string]*prometheus.SummaryVec),
Gauges: make(map[string]prometheus.Gauge),
countMutex: &sync.RWMutex{},
countVecMutex: &sync.RWMutex{},
histMutex: &sync.RWMutex{},
histVecMutex: &sync.RWMutex{},
summaryVecMutex: &sync.RWMutex{},
gaugeMutex: &sync.RWMutex{},
}
return &m
}
// defaultObjectives returns a default map of quantiles to be used in summaries.
func defaultObjectives() map[float64]float64 {
return map[float64]float64{0.5: 0.05, 0.75: 0.025, 0.9: 0.01, 0.95: 0.005, 0.99: 0.001, 0.999: 0.0001}
}
// Count increases the counter for the specified subsystem and name.
func (m *metrics) Count(subsystem, name, help string) {
m.countMutex.RLock()
key := fmt.Sprintf("%s/%s", subsystem, name)
counter, exists := m.Counters[key]
m.countMutex.RUnlock()
if !exists {
m.countMutex.Lock()
if counter, exists = m.Counters[key]; !exists {
counter = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: name,
Help: help,
})
m.Counters[key] = counter
err := prometheus.Register(counter)
if err != nil {
m.Logger.
Warn("MetricsCounterRegistrationFailed",
"CounterHandler: Counter registration %v failed: %v", counter, err)
}
}
m.countMutex.Unlock()
}
counter.Inc()
}
// SetGauge sets the gauge value for the specified subsystem and name.
func (m *metrics) SetGauge(value float64, subsystem, name, help string) {
m.gaugeMutex.RLock()
key := fmt.Sprintf("%s/%s", subsystem, name)
gauge, exists := m.Gauges[key]
m.gaugeMutex.RUnlock()
if !exists {
m.gaugeMutex.Lock()
if gauge, exists = m.Gauges[key]; !exists {
gauge = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: name,
Help: help,
})
m.Gauges[key] = gauge
err := prometheus.Register(gauge)
if err != nil {
m.Logger.
Warn("MetricsSetGaugeFailed",
"SetGauge: Gauge registration %v failed: %v", gauge, err)
}
}
m.gaugeMutex.Unlock()
}
gauge.Set(value)
}
// CountLabels increases the counter for the specified subsystem and name and adds the specified labels with values.
func (m *metrics) CountLabels(subsystem, name, help string, labels, values []string) {
m.countVecMutex.RLock()
key := fmt.Sprintf("%s/%s", subsystem, name)
counter, exists := m.CounterVecs[key]
m.countVecMutex.RUnlock()
if !exists {
m.countVecMutex.Lock()
if counter, exists = m.CounterVecs[key]; !exists {
counter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: name,
Help: help,
}, labels)
m.CounterVecs[key] = counter
err := prometheus.Register(counter)
if err != nil {
m.Logger.
Warn("MetricsCounterLabelRegistrationFailed",
"CounterLabelHandler: Counter registration %v failed: %v", counter, err)
}
}
m.countVecMutex.Unlock()
}
counter.WithLabelValues(values...).Inc()
}
// IncreaseCounter increases the counter for the specified subsystem and name with the specified increment.
func (m *metrics) IncreaseCounter(subsystem, name, help string, increment int) {
m.countMutex.RLock()
key := fmt.Sprintf("%s/%s", subsystem, name)
counter, exists := m.Counters[key]
m.countMutex.RUnlock()
if !exists {
m.countMutex.Lock()
if counter, exists = m.Counters[key]; !exists {
counter = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: name,
Help: help,
})
m.Counters[key] = counter
err := prometheus.Register(counter)
if err != nil {
m.Logger.
Warn("MetricsIncreaseCounterRegistrationFailed",
"CounterHandler: Counter registration failed: %v: %v", counter, err)
}
}
m.countMutex.Unlock()
}
counter.Add(float64(increment))
}
// AddHistogram returns the MetricsHistogram for the specified subsystem and name.
func (m *metrics) AddHistogram(subsystem, name, help string) *metricsHistogram {
return m.addHistogramWithBuckets(subsystem, name, help, prometheus.DefBuckets)
}
// AddHistogramWithCustomBuckets returns the MetricsHistogram for the specified subsystem and name with the specified buckets.
func (m *metrics) AddHistogramWithCustomBuckets(subsystem, name, help string, buckets []float64) *metricsHistogram {
return m.addHistogramWithBuckets(subsystem, name, help, buckets)
}
func (m *metrics) addHistogramWithBuckets(subsystem, name, help string, buckets []float64) *metricsHistogram {
m.histMutex.RLock()
key := fmt.Sprintf("%s/%s", subsystem, name)
sum, exists := m.Summaries[key]
hist := m.Histograms[key]
m.histMutex.RUnlock()
if !exists {
m.histMutex.Lock()
if sum, exists = m.Summaries[key]; !exists {
// todo: remove Summary creation/observation
sum = prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: name + "_summary",
Help: help,
Objectives: defaultObjectives(),
})
prometheus.MustRegister(sum)
m.Summaries[key] = sum
hist = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: name,
Help: help,
Buckets: buckets,
})
prometheus.MustRegister(hist)
m.Histograms[key] = hist
}
m.histMutex.Unlock()
}
mh := metricsHistogram{
Key: key,
hist: hist,
sum: sum,
}
return &mh
}
// AddHistogramVec returns the HistogramVec for the specified subsystem and name.
func (m *metrics) AddHistogramVec(subsystem, name, help string, labels, labelValues []string) *histogramVec {
return m.addHistogramVecWithBuckets(subsystem, name, help, labels, labelValues, prometheus.DefBuckets)
}
// AddHistogramVecWithCustomBuckets returns the HistogramVec for the specified subsystem and name with the specified buckets.
func (m *metrics) AddHistogramVecWithCustomBuckets(subsystem, name, help string, labels, labelValues []string,
buckets []float64) *histogramVec {
return m.addHistogramVecWithBuckets(subsystem, name, help, labels, labelValues, buckets)
}
func (m *metrics) addHistogramVecWithBuckets(subsystem, name, help string, labels, labelValues []string,
buckets []float64) *histogramVec {
m.histVecMutex.RLock()
key := fmt.Sprintf("%s/%s", subsystem, name)
vec, exists := m.HistogramVecs[key]
m.histVecMutex.RUnlock()
if !exists {
m.histVecMutex.Lock()
vec = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: name,
Help: help,
Buckets: buckets,
}, labels)
prometheus.MustRegister(vec)
m.HistogramVecs[key] = vec
m.histVecMutex.Unlock()
}
mh := histogramVec{
Key: key,
Labels: labels,
LabelValues: labelValues,
histVec: vec,
}
return &mh
}
// AddSummaryVec returns the SummaryVec for the specified subsystem and name.
func (m *metrics) AddSummaryVec(subsystem, name, help string, labels, labelValues []string) *summaryVec {
return m.addSummaryVecWithObjectives(subsystem, name, help, labels, labelValues, defaultObjectives())
}
// AddSummaryVecWithCustomObjectives returns the SummaryVec for the specified subsystem and name with the specified objectives.
func (m *metrics) AddSummaryVecWithCustomObjectives(subsystem, name, help string, labels, labelValues []string,
objectives map[float64]float64) *summaryVec {
return m.addSummaryVecWithObjectives(subsystem, name, help, labels, labelValues, objectives)
}
func (m *metrics) addSummaryVecWithObjectives(subsystem, name, help string, labels, labelValues []string,
objectives map[float64]float64) *summaryVec {
m.summaryVecMutex.RLock()
key := fmt.Sprintf("%s/%s", subsystem, name)
vec, exists := m.SummaryVecs[key]
m.summaryVecMutex.RUnlock()
if !exists {
m.summaryVecMutex.Lock()
vec = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: name,
Help: help,
Objectives: objectives,
}, labels)
prometheus.MustRegister(vec)
m.SummaryVecs[key] = vec
m.summaryVecMutex.Unlock()
}
mh := summaryVec{
Key: key,
Labels: labels,
LabelValues: labelValues,
summaryVec: vec,
}
return &mh
}
// RecordTimeElapsed adds the elapsed time since the specified start to the histogram in seconds and to the linked
// summary in milliseconds.
func (histogram *metricsHistogram) RecordTimeElapsed(start time.Time) {
elapsed := float64(time.Since(start).Seconds())
histogram.hist.Observe(elapsed) // The default histogram buckets are recorded in seconds
histogram.sum.Observe(elapsed * 1000.0) // While we have summaries in milliseconds
}
// RecordDuration adds the elapsed time since the specified start to the histogram in the specified unit of time
// and to the linked summary in milliseconds.
func (histogram *metricsHistogram) RecordDuration(start time.Time, unit time.Duration) {
since := time.Since(start)
elapsedMilliseconds := float64(since.Truncate(time.Millisecond))
elapsedUnits := float64(since.Truncate(unit))
histogram.hist.Observe(elapsedUnits)
histogram.sum.Observe(elapsedMilliseconds)
}
// Observe adds the specified value to the histogram.
func (histogram *metricsHistogram) Observe(value float64) {
histogram.hist.Observe(value)
}
// RecordTimeElapsed adds the elapsed time since the specified start to the histogram in seconds.
func (vec *histogramVec) RecordTimeElapsed(start time.Time) {
elapsed := float64(time.Since(start).Seconds())
vec.Observe(elapsed) // The default histogram buckets are recorded in seconds
}
// RecordDuration adds the elapsed time since the specified start to the histogram in the specified unit of time.
func (vec *histogramVec) RecordDuration(start time.Time, unit time.Duration) {
since := time.Since(start)
elapsedUnits := float64(since.Truncate(unit))
vec.Observe(elapsedUnits)
}
// Observe adds the specified value to the histogram.
func (vec *histogramVec) Observe(value float64) {
vec.histVec.WithLabelValues(vec.LabelValues...).Observe(value)
}
// RecordTimeElapsed adds the elapsed time since the specified start to the summary in milliseconds.
func (vec *summaryVec) RecordTimeElapsed(start time.Time) {
since := time.Since(start)
elapsed := float64(since.Truncate(time.Millisecond))
vec.summaryVec.WithLabelValues(vec.LabelValues...).Observe(elapsed)
}
// RecordDuration adds the elapsed time since the specified start to the histogram in the specified unit of time.
func (vec *summaryVec) RecordDuration(start time.Time, unit time.Duration) {
since := time.Since(start)
elapsedUnits := float64(since.Truncate(unit))
vec.summaryVec.WithLabelValues(vec.LabelValues...).Observe(elapsedUnits)
}