-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetrics_set.go
612 lines (484 loc) · 14.5 KB
/
metrics_set.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// Copyright (c) 2020 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
// Package rkprom has couple of utility functions to start prometheus and pushgateway client locally.
package rkprom
import (
"fmt"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"strings"
"sync"
)
const (
maxKeyLength = 256
separator = "::"
namespaceDefault = "rk"
subSystemDefault = "svc"
)
// SummaryObjectives will track quantile of P50, P90, P99, P9999 by default.
var SummaryObjectives = map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001, 0.999: 0.0001}
// MetricsSet is a collections of counter, gauge, summary, histogram and link to certain registerer.
// User need to provide own prometheus.Registerer.
//
// 1: namespace: the namespace of prometheus metrics
// 2: sysSystem: the subSystem of prometheus metrics
// 3: keys: a map stores all keys
// 4: counters: map of counters
// 5: gauges: map of gauges
// 6: summaries: map of summaries
// 7: histograms: map of histograms
// 8: lock: lock for thread safety
// 9: registerer prometheus.Registerer
type MetricsSet struct {
namespace string
subSystem string
keys map[string]bool
counters map[string]*prometheus.CounterVec
gauges map[string]*prometheus.GaugeVec
summaries map[string]*prometheus.SummaryVec
histograms map[string]*prometheus.HistogramVec
lock sync.Mutex
registerer prometheus.Registerer
}
// NewMetricsSet creates metrics set with namespace, subSystem and registerer.
//
// If no registerer was provided, then prometheus.DefaultRegisterer would be used.
//
// Important!
// namespace, subSystem, labels should match prometheus regex as bellow
// ^[a-zA-Z_:][a-zA-Z0-9_:]*$
// If provided name is not valid, then default ones would be assigned
func NewMetricsSet(namespace, subSystem string, registerer prometheus.Registerer) *MetricsSet {
if !model.IsValidMetricName(model.LabelValue(namespace)) {
namespace = namespaceDefault
}
if !model.IsValidMetricName(model.LabelValue(subSystem)) {
subSystem = subSystemDefault
}
metrics := MetricsSet{
namespace: namespace,
subSystem: subSystem,
keys: make(map[string]bool),
counters: make(map[string]*prometheus.CounterVec),
gauges: make(map[string]*prometheus.GaugeVec),
summaries: make(map[string]*prometheus.SummaryVec),
histograms: make(map[string]*prometheus.HistogramVec),
lock: sync.Mutex{},
registerer: registerer,
}
if metrics.registerer == nil {
metrics.registerer = prometheus.DefaultRegisterer
}
return &metrics
}
// GetNamespace returns namespace
func (set *MetricsSet) GetNamespace() string {
return set.namespace
}
// GetSubSystem returns subsystem
func (set *MetricsSet) GetSubSystem() string {
return set.subSystem
}
//GetRegisterer returns registerer
func (set *MetricsSet) GetRegisterer() prometheus.Registerer {
return set.registerer
}
// RegisterCounter is thread safe
// Register a counter with namespace and subsystem in MetricsSet
func (set *MetricsSet) RegisterCounter(name string, labelKeys ...string) error {
set.lock.Lock()
defer set.lock.Unlock()
if err := validateName(name); err != nil {
return err
}
// check existence
key := set.getKey(name)
if set.containsKey(key) {
return errors.New(fmt.Sprintf("duplicate counter name:%s", name))
}
// create a new one with default options
opts := prometheus.CounterOpts{
Namespace: set.namespace,
Subsystem: set.subSystem,
Name: name,
Help: fmt.Sprintf("counter for name:%s and labels:%s", name, labelKeys),
}
// panic if labels are not matching
counterVec := prometheus.NewCounterVec(opts, labelKeys)
err := set.registerer.Register(counterVec)
if err == nil {
set.counters[key] = counterVec
set.keys[key] = true
}
return err
}
// UnRegisterCounter is thread safe
// Unregister metrics, error would be thrown only when invalid name was provided
func (set *MetricsSet) UnRegisterCounter(name string) {
set.lock.Lock()
defer set.lock.Unlock()
key := set.getKey(name)
// check existence
if set.containsKey(key) {
prometheus.Unregister(set.counters[key])
delete(set.counters, key)
delete(set.keys, key)
}
}
// RegisterGauge thread safe
// Register a gauge with namespace and subsystem in MetricsSet
func (set *MetricsSet) RegisterGauge(name string, labelKeys ...string) error {
set.lock.Lock()
defer set.lock.Unlock()
if err := validateName(name); err != nil {
return err
}
// check existence
key := set.getKey(name)
if set.containsKey(key) {
return errors.New(fmt.Sprintf("duplicate gauge name:%s", name))
}
// create a new one with default options
opts := prometheus.GaugeOpts{
Namespace: set.namespace,
Subsystem: set.subSystem,
Name: name,
Help: fmt.Sprintf("Gauge for name:%s and labels:%s", name, labelKeys),
}
// panic if labels are not matching
gaugeVec := prometheus.NewGaugeVec(opts, labelKeys)
err := set.registerer.Register(gaugeVec)
if err == nil {
set.gauges[key] = gaugeVec
set.keys[key] = true
}
return err
}
// UnRegisterGauge thread safe
// Unregister metrics, error would be thrown only when invalid name was provided
func (set *MetricsSet) UnRegisterGauge(name string) {
set.lock.Lock()
defer set.lock.Unlock()
key := set.getKey(name)
// check existence
if set.containsKey(key) {
set.registerer.Unregister(set.gauges[key])
delete(set.gauges, key)
delete(set.keys, key)
}
}
// RegisterHistogram thread safe
// Register a histogram with namespace, subsystem and objectives in MetricsSet
// If bucket is nil, then empty bucket would be applied
func (set *MetricsSet) RegisterHistogram(name string, bucket []float64, labelKeys ...string) error {
set.lock.Lock()
defer set.lock.Unlock()
if err := validateName(name); err != nil {
return err
}
// check existence
key := set.getKey(name)
if set.containsKey(key) {
return errors.New(fmt.Sprintf("duplicate histogram name:%s", name))
}
if bucket == nil {
bucket = make([]float64, 0)
}
// create a new one with default options
opts := prometheus.HistogramOpts{
Namespace: set.namespace,
Subsystem: set.subSystem,
Name: name,
Buckets: bucket,
Help: fmt.Sprintf("Histogram for name:%s and labels:%s", name, labelKeys),
}
// It will panic if labels are not matching
hisVec := prometheus.NewHistogramVec(opts, labelKeys)
err := set.registerer.Register(hisVec)
if err == nil {
set.histograms[key] = hisVec
set.keys[key] = true
}
return err
}
// UnRegisterHistogram thread safe
// Unregister metrics, error would be thrown only when invalid name was provided
func (set *MetricsSet) UnRegisterHistogram(name string) {
set.lock.Lock()
defer set.lock.Unlock()
key := set.getKey(name)
// check existence
if set.containsKey(key) {
hisVec := set.histograms[key]
set.registerer.Unregister(*hisVec)
delete(set.histograms, key)
delete(set.keys, key)
}
}
// RegisterSummary thread safe
// Register a summary with namespace, subsystem and objectives in MetricsSet
// If objectives is nil, then default SummaryObjectives would be applied
func (set *MetricsSet) RegisterSummary(name string, objectives map[float64]float64, labelKeys ...string) error {
set.lock.Lock()
defer set.lock.Unlock()
if err := validateName(name); err != nil {
return err
}
// check existence
key := set.getKey(name)
if set.containsKey(key) {
return errors.New(fmt.Sprintf("duplicate summary name:%s", name))
}
if objectives == nil {
objectives = SummaryObjectives
}
// create a new one with default options
opts := prometheus.SummaryOpts{
Namespace: set.namespace,
Subsystem: set.subSystem,
Name: name,
Objectives: objectives,
Help: fmt.Sprintf("Summary for name:%s and labels:%s", name, labelKeys),
}
// panic if labels are not matching
summaryVec := prometheus.NewSummaryVec(opts, labelKeys)
err := set.registerer.Register(summaryVec)
if err == nil {
set.summaries[key] = summaryVec
set.keys[key] = true
}
return err
}
// UnRegisterSummary thread safe
// Unregister metrics, error would be thrown only when invalid name was provided
func (set *MetricsSet) UnRegisterSummary(name string) {
set.lock.Lock()
defer set.lock.Unlock()
key := set.getKey(name)
// check existence
if set.containsKey(key) {
summaryVec := set.summaries[key]
set.registerer.Unregister(*summaryVec)
//prometheus.Unregister(*summaryVec)
delete(set.summaries, key)
delete(set.keys, key)
}
}
// GetCounter is thread safe
func (set *MetricsSet) GetCounter(name string) *prometheus.CounterVec {
set.lock.Lock()
defer set.lock.Unlock()
return set.counters[set.getKey(name)]
}
// GetGauge is thread safe
func (set *MetricsSet) GetGauge(name string) *prometheus.GaugeVec {
set.lock.Lock()
defer set.lock.Unlock()
return set.gauges[set.getKey(name)]
}
// GetHistogram is thread safe
func (set *MetricsSet) GetHistogram(name string) *prometheus.HistogramVec {
set.lock.Lock()
defer set.lock.Unlock()
return set.histograms[set.getKey(name)]
}
// GetSummary is thread safe
func (set *MetricsSet) GetSummary(name string) *prometheus.SummaryVec {
set.lock.Lock()
defer set.lock.Unlock()
return set.summaries[set.getKey(name)]
}
// ListCounters is thread safe
func (set *MetricsSet) ListCounters() []*prometheus.CounterVec {
set.lock.Lock()
defer set.lock.Unlock()
res := make([]*prometheus.CounterVec, 0)
for _, v := range set.counters {
res = append(res, v)
}
return res
}
// ListGauges is thread safe
func (set *MetricsSet) ListGauges() []*prometheus.GaugeVec {
set.lock.Lock()
defer set.lock.Unlock()
res := make([]*prometheus.GaugeVec, 0)
for _, v := range set.gauges {
res = append(res, v)
}
return res
}
// ListHistograms is thread safe
func (set *MetricsSet) ListHistograms() []*prometheus.HistogramVec {
set.lock.Lock()
defer set.lock.Unlock()
res := make([]*prometheus.HistogramVec, 0)
for _, v := range set.histograms {
res = append(res, v)
}
return res
}
// ListSummaries is thread safe
func (set *MetricsSet) ListSummaries() []*prometheus.SummaryVec {
set.lock.Lock()
defer set.lock.Unlock()
res := make([]*prometheus.SummaryVec, 0)
for _, v := range set.summaries {
res = append(res, v)
}
return res
}
// GetCounterWithValues is thread safe
//
// Get counter with values matched with labels
// Users should always be sure about the number of labels.
func (set *MetricsSet) GetCounterWithValues(name string, values ...string) prometheus.Counter {
set.lock.Lock()
defer set.lock.Unlock()
key := set.getKey(name)
if set.containsKey(key) {
counterVec := set.counters[key]
// ignore err
counter, _ := counterVec.GetMetricWithLabelValues(values...)
return counter
}
return nil
}
// GetCounterWithLabels is thread safe
//
// Get counter with values matched with labels
// Users should always be sure about the number of labels.
func (set *MetricsSet) GetCounterWithLabels(name string, labels prometheus.Labels) prometheus.Counter {
set.lock.Lock()
defer set.lock.Unlock()
key := set.getKey(name)
if set.containsKey(key) {
counterVec := set.counters[key]
// ignore error
counter, _ := counterVec.GetMetricWith(labels)
return counter
}
return nil
}
// GetGaugeWithValues is thread safe
//
// Get gauge with values matched with labels
// Users should always be sure about the number of labels.
func (set *MetricsSet) GetGaugeWithValues(name string, values ...string) prometheus.Gauge {
set.lock.Lock()
defer set.lock.Unlock()
key := set.getKey(name)
if set.containsKey(key) {
gaugeVec := set.gauges[key]
// ignore error
gauge, _ := gaugeVec.GetMetricWithLabelValues(values...)
return gauge
}
return nil
}
// GetGaugeWithLabels is thread safe
// Get gauge with values matched with labels
// Users should always be sure about the number of labels.
func (set *MetricsSet) GetGaugeWithLabels(name string, labels prometheus.Labels) prometheus.Gauge {
set.lock.Lock()
defer set.lock.Unlock()
key := set.getKey(name)
if set.containsKey(key) {
gaugeVec := set.gauges[key]
// ignore error
gauge, _ := gaugeVec.GetMetricWith(labels)
return gauge
}
return nil
}
// GetSummaryWithValues is thread safe
//
// Get summary with values matched with labels
// Users should always be sure about the number of labels.
func (set *MetricsSet) GetSummaryWithValues(name string, values ...string) prometheus.Observer {
set.lock.Lock()
defer set.lock.Unlock()
key := set.getKey(name)
if set.containsKey(key) {
summaryVec := set.summaries[key]
// ignore error
observer, _ := summaryVec.GetMetricWithLabelValues(values...)
return observer
}
return nil
}
// GetSummaryWithLabels is thread safe
//
// Get summary with values matched with labels
// Users should always be sure about the number of labels.
func (set *MetricsSet) GetSummaryWithLabels(name string, labels prometheus.Labels) prometheus.Observer {
set.lock.Lock()
defer set.lock.Unlock()
key := set.getKey(name)
if set.containsKey(key) {
summaryVec := set.summaries[key]
// ignore error
observer, _ := summaryVec.GetMetricWith(labels)
return observer
}
return nil
}
// GetHistogramWithValues is thread safe
//
// Get histogram with values matched with labels
// Users should always be sure about the number of labels.
func (set *MetricsSet) GetHistogramWithValues(name string, values ...string) prometheus.Observer {
set.lock.Lock()
defer set.lock.Unlock()
key := set.getKey(name)
if set.containsKey(key) {
hisVec := set.histograms[key]
// ignore error
observer, _ := hisVec.GetMetricWithLabelValues(values...)
return observer
}
return nil
}
// GetHistogramWithLabels is thread safe
//
// Get histogram with values matched with labels
// Users should always be sure about the number of labels.
func (set *MetricsSet) GetHistogramWithLabels(name string, labels prometheus.Labels) prometheus.Observer {
set.lock.Lock()
defer set.lock.Unlock()
key := set.getKey(name)
if set.containsKey(key) {
hisVec := set.histograms[key]
// ignore error
observer, _ := hisVec.GetMetricWith(labels)
return observer
}
return nil
}
// Construct key with format of namespace::subSystem::name
func (set *MetricsSet) getKey(name string) string {
key := strings.Join([]string{
set.namespace,
set.subSystem,
name}, separator)
return key
}
// Check existence
func (set *MetricsSet) containsKey(key string) bool {
_, contains := set.keys[key]
return contains
}
// Validate input name
func validateName(name string) error {
name = strings.TrimSpace(name)
if len(name) < 1 {
return errors.New("empty name")
}
if len(name) > maxKeyLength {
return errors.New(fmt.Sprintf("exceed max name length:%d", maxKeyLength))
}
return nil
}