forked from okx/xlayer-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
677 lines (550 loc) · 15.9 KB
/
prometheus.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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
package metrics
import (
"net/http"
"sync"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
storageMutex sync.RWMutex
registerer prometheus.Registerer
gauges map[string]prometheus.Gauge
counters map[string]prometheus.Counter
counterVecs map[string]*prometheus.CounterVec
histograms map[string]prometheus.Histogram
histogramVecs map[string]*prometheus.HistogramVec
summaries map[string]prometheus.Summary
initialized bool
initOnce sync.Once
)
// CounterVecOpts holds options for the CounterVec type.
type CounterVecOpts struct {
prometheus.CounterOpts
Labels []string
}
// HistogramVecOpts holds options for the HistogramVec type.
type HistogramVecOpts struct {
prometheus.HistogramOpts
Labels []string
}
// Init initializes the package variables.
func Init() {
initOnce.Do(func() {
storageMutex = sync.RWMutex{}
registerer = prometheus.DefaultRegisterer
gauges = make(map[string]prometheus.Gauge)
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)
initialized = true
})
}
// Handler returns the Prometheus http handler.
func Handler() http.Handler {
return promhttp.Handler()
}
// RegisterGauges registers the provided gauge metrics to the Prometheus
// registerer.
func RegisterGauges(opts ...prometheus.GaugeOpts) {
if !initialized {
return
}
storageMutex.Lock()
defer storageMutex.Unlock()
for _, options := range opts {
registerGaugeIfNotExists(options)
}
}
// UnregisterGauges unregisters the provided gauge metrics from the Prometheus
// registerer.
func UnregisterGauges(names ...string) {
if !initialized {
return
}
storageMutex.Lock()
defer storageMutex.Unlock()
for _, name := range names {
unregisterGaugeIfExists(name)
}
}
// Gauge retrieves gauge metric by name
func Gauge(name string) (gauge prometheus.Gauge, exist bool) {
if !initialized {
return
}
storageMutex.RLock()
defer storageMutex.RUnlock()
if gauge, exist = gauges[name]; !exist {
return nil, exist
}
return gauge, exist
}
// GaugeSet sets the value for gauge with the given name.
func GaugeSet(name string, value float64) {
if !initialized {
return
}
if c, ok := Gauge(name); ok {
c.Set(value)
}
}
// GaugeInc increments the gauge with the given name.
func GaugeInc(name string) {
if !initialized {
return
}
if g, ok := Gauge(name); ok {
g.Inc()
}
}
// GaugeDec decrements the gauge with the given name.
func GaugeDec(name string) {
if !initialized {
return
}
if g, ok := Gauge(name); ok {
g.Dec()
}
}
// RegisterCounters registers the provided counter metrics to the Prometheus
// registerer.
func RegisterCounters(opts ...prometheus.CounterOpts) {
if !initialized {
return
}
storageMutex.Lock()
defer storageMutex.Unlock()
for _, options := range opts {
registerCounterIfNotExists(options)
}
}
// Counter retrieves counter metric by name
func Counter(name string) (counter prometheus.Counter, exist bool) {
if !initialized {
return
}
storageMutex.RLock()
defer storageMutex.RUnlock()
if counter, exist = counters[name]; !exist {
return nil, exist
}
return counter, exist
}
// CounterInc increments the counter with the given name.
func CounterInc(name string) {
if !initialized {
return
}
if c, ok := Counter(name); ok {
c.Inc()
}
}
// CounterAdd increments the counter with the given name.
func CounterAdd(name string, value float64) {
if !initialized {
return
}
if c, ok := Counter(name); ok {
c.Add(value)
}
}
// UnregisterCounters unregisters the provided counter metrics from the
// Prometheus registerer.
func UnregisterCounters(names ...string) {
if !initialized {
return
}
storageMutex.Lock()
defer storageMutex.Unlock()
for _, name := range names {
unregisterCounterIfExists(name)
}
}
// RegisterCounterVecs registers the provided counter vec metrics to the
// Prometheus registerer.
func RegisterCounterVecs(opts ...CounterVecOpts) {
if !initialized {
return
}
storageMutex.Lock()
defer storageMutex.Unlock()
for _, options := range opts {
registerCounterVecIfNotExists(options)
}
}
// CounterVec retrieves counter ver metric by name
func CounterVec(name string) (counterVec *prometheus.CounterVec, exist bool) {
if !initialized {
return
}
storageMutex.RLock()
defer storageMutex.RUnlock()
counterVec, exist = counterVecs[name]
return counterVec, exist
}
// CounterVecInc increments the counter vec with the given name and label.
func CounterVecInc(name string, label string) {
if !initialized {
return
}
if cv, ok := CounterVec(name); ok {
cv.WithLabelValues(label).Inc()
}
}
// CounterVecAdd increments the counter vec by the given value, with the given
// name and label.
func CounterVecAdd(name string, label string, value float64) {
if !initialized {
return
}
if cv, ok := CounterVec(name); ok {
cv.WithLabelValues(label).Add(value)
}
}
// UnregisterCounterVecs unregisters the provided counter vec metrics from the
// Prometheus registerer.
func UnregisterCounterVecs(names ...string) {
if !initialized {
return
}
storageMutex.Lock()
defer storageMutex.Unlock()
for _, name := range names {
unregisterCounterVecIfExists(name)
}
}
// RegisterHistograms registers the provided histogram metrics to the
// Prometheus registerer.
func RegisterHistograms(opts ...prometheus.HistogramOpts) {
if !initialized {
return
}
storageMutex.Lock()
defer storageMutex.Unlock()
for _, options := range opts {
registerHistogramIfNotExists(options)
}
}
// Histogram retrieves histogram metric by name
func Histogram(name string) (histogram prometheus.Histogram, exist bool) {
if !initialized {
return
}
storageMutex.RLock()
defer storageMutex.RUnlock()
if histogram, exist = histograms[name]; !exist {
return nil, exist
}
return histogram, exist
}
// HistogramObserve observes the histogram from the given start time.
func HistogramObserve(name string, value float64) {
if !initialized {
return
}
if histo, ok := Histogram(name); ok {
histo.Observe(value)
}
}
// UnregisterHistogram unregisters the provided histogram metrics from the
// Prometheus registerer.
func UnregisterHistogram(names ...string) {
if !initialized {
return
}
storageMutex.Lock()
defer storageMutex.Unlock()
for _, name := range names {
unregisterHistogramIfExists(name)
}
}
// RegisterHistogramVecs registers the provided histogram vec metrics to the
// Prometheus registerer.
func RegisterHistogramVecs(opts ...HistogramVecOpts) {
if !initialized {
return
}
storageMutex.Lock()
defer storageMutex.Unlock()
for _, options := range opts {
registerHistogramVecIfNotExists(options)
}
}
// HistogramVec retrieves histogram ver metric by name
func HistogramVec(name string) (histgramVec *prometheus.HistogramVec, exist bool) {
if !initialized {
return
}
storageMutex.RLock()
defer storageMutex.RUnlock()
histgramVec, exist = histogramVecs[name]
return histgramVec, exist
}
// HistogramVecObserve observes the histogram vec with the given name, label and value.
func HistogramVecObserve(name string, label string, value float64) {
if !initialized {
return
}
if cv, ok := HistogramVec(name); ok {
cv.WithLabelValues(label).Observe(value)
}
}
// UnregisterHistogramVecs unregisters the provided histogram vec metrics from the
// Prometheus registerer.
func UnregisterHistogramVecs(names ...string) {
if !initialized {
return
}
storageMutex.Lock()
defer storageMutex.Unlock()
for _, name := range names {
unregisterHistogramVecIfExists(name)
}
}
// RegisterSummaries registers the provided summary metrics to the Prometheus
// registerer.
func RegisterSummaries(opts ...prometheus.SummaryOpts) {
if !initialized {
return
}
storageMutex.Lock()
defer storageMutex.Unlock()
for _, options := range opts {
registerSummaryIfNotExists(options)
}
}
// Summary retrieves summary metric by name
func Summary(name string) (summary prometheus.Summary, exist bool) {
if !initialized {
return
}
storageMutex.RLock()
defer storageMutex.RUnlock()
if summary, exist = summaries[name]; !exist {
return nil, exist
}
return summary, exist
}
// UnregisterSummaries unregisters the provided summary metrics from the
// Prometheus registerer.
func UnregisterSummaries(names ...string) {
if !initialized {
return
}
storageMutex.Lock()
defer storageMutex.Unlock()
for _, name := range names {
unregisterSummaryIfExists(name)
}
}
// registerGaugeIfNotExists registers single gauge metric if not exists
func registerGaugeIfNotExists(opts prometheus.GaugeOpts) {
log := log.WithFields("metricName", opts.Name)
if _, exist := gauges[opts.Name]; exist {
log.Warn("Gauge metric already exists.")
return
}
log.Debug("Creating Gauge Metric...")
gauge := prometheus.NewGauge(opts)
log.Debugf("Gauge Metric successfully created! Labels: %p", opts.ConstLabels)
log.Debug("Registering Gauge Metric...")
registerer.MustRegister(gauge)
log.Debug("Gauge Metric successfully registered!")
gauges[opts.Name] = gauge
}
// unregisterGaugeIfExists unregisters single gauge metric if exists
func unregisterGaugeIfExists(name string) {
var (
gauge prometheus.Gauge
ok bool
)
log := log.WithFields("metricName", name)
if gauge, ok = gauges[name]; !ok {
log.Warn("Trying to delete non-existing Gauge metrics.")
return
}
log.Debug("Unregistering Gauge Metric...")
ok = registerer.Unregister(gauge)
if !ok {
log.Error("Failed to unregister Gauge Metric.")
return
}
delete(gauges, name)
log.Debug("Gauge Metric successfully unregistered!")
}
// registerCounterIfNotExists registers single counter metric if not exists
func registerCounterIfNotExists(opts prometheus.CounterOpts) {
log := log.WithFields("metricName", opts.Name)
if _, exist := counters[opts.Name]; exist {
log.Warn("Counter metric already exists.")
return
}
log.Debug("Creating Counter Metric...")
counter := prometheus.NewCounter(opts)
log.Debugf("Counter Metric successfully created! Labels: %p", opts.ConstLabels)
log.Debug("Registering Counter Metric...")
registerer.MustRegister(counter)
log.Debug("Counter Metric successfully registered!")
counters[opts.Name] = counter
}
// unregisterCounterIfExists unregisters single counter metric if exists
func unregisterCounterIfExists(name string) {
var (
counter prometheus.Counter
ok bool
)
log := log.WithFields("metricName", name)
if counter, ok = counters[name]; !ok {
log.Warn("Trying to delete non-existing Counter counter.")
return
}
log.Debug("Unregistering Counter Metric...")
ok = registerer.Unregister(counter)
if !ok {
log.Error("Failed to unregister Counter Metric.")
return
}
delete(counters, name)
log.Debugf("Counter Metric '%v' successfully unregistered!", name)
}
// registerCounterVecIfNotExists registers single counter vec metric if not exists
func registerCounterVecIfNotExists(opts CounterVecOpts) {
log := log.WithFields("metricName", opts.Name)
if _, exist := counterVecs[opts.Name]; exist {
log.Warn("Counter vec metric already exists.")
return
}
log.Debug("Creating Counter Vec Metric...")
counterVec := prometheus.NewCounterVec(opts.CounterOpts, opts.Labels)
log.Debugf("Counter Vec Metric successfully created! Labels: %p", opts.ConstLabels)
log.Debug("Registering Counter Vec Metric...")
registerer.MustRegister(counterVec)
log.Debug("Counter Vec Metric successfully registered!")
counterVecs[opts.Name] = counterVec
}
// unregisterCounterVecIfExists unregisters single counter metric if exists
func unregisterCounterVecIfExists(name string) {
var (
counterVec *prometheus.CounterVec
ok bool
)
log := log.WithFields("metricName", name)
if counterVec, ok = counterVecs[name]; !ok {
log.Warn("Trying to delete non-existing Counter Vec counter.")
return
}
log.Debug("Unregistering Counter Vec Metric...")
ok = registerer.Unregister(counterVec)
if !ok {
log.Error("Failed to unregister Counter Vec Metric.")
return
}
delete(counterVecs, name)
log.Debug("Counter Vec Metric successfully unregistered!")
}
// registerHistogramIfNotExists registers single histogram metric if not exists
func registerHistogramIfNotExists(opts prometheus.HistogramOpts) {
log := log.WithFields("metricName", opts.Name)
if _, exist := histograms[opts.Name]; exist {
log.Warn("Histogram metric already exists.")
return
}
log.Debug("Creating Histogram Metric...")
histogram := prometheus.NewHistogram(opts)
log.Debugf("Histogram Metric successfully created! Labels: %p", opts.ConstLabels)
log.Debug("Registering Histogram Metric...")
registerer.MustRegister(histogram)
log.Debug("Histogram Metric successfully registered!")
histograms[opts.Name] = histogram
}
// unregisterHistogramIfExists unregisters single histogram metric if exists
func unregisterHistogramIfExists(name string) {
var (
histogram prometheus.Histogram
ok bool
)
log := log.WithFields("metricName", name)
if histogram, ok = histograms[name]; !ok {
log.Warn("Trying to delete non-existing Histogram histogram.")
return
}
log.Debug("Unregistering Histogram Metric...")
ok = registerer.Unregister(histogram)
if !ok {
log.Error("Failed to unregister Histogram Metric.")
return
}
delete(histograms, name)
log.Debug("Histogram Metric successfully unregistered!")
}
// registerHistogramVecIfNotExists unregisters single counter metric if exists
func registerHistogramVecIfNotExists(opts HistogramVecOpts) {
if _, exist := histogramVecs[opts.Name]; exist {
log.Warnf("Histogram vec metric '%v' already exists.", opts.Name)
return
}
log.Infof("Creating Histogram Vec Metric '%v' ...", opts.Name)
histogramVec := prometheus.NewHistogramVec(opts.HistogramOpts, opts.Labels)
log.Infof("Histogram Vec Metric '%v' successfully created! Labels: %p", opts.Name, opts.ConstLabels)
log.Infof("Registering Histogram Vec Metric '%v' ...", opts.Name)
registerer.MustRegister(histogramVec)
log.Infof("Histogram Vec Metric '%v' successfully registered!", opts.Name)
histogramVecs[opts.Name] = histogramVec
}
// unregisterHistogramVecIfExists unregisters single histogram metric if exists
func unregisterHistogramVecIfExists(name string) {
var (
histogramVec *prometheus.HistogramVec
ok bool
)
if histogramVec, ok = histogramVecs[name]; !ok {
log.Warnf("Trying to delete non-existing Histogram Vec '%v'.", name)
return
}
log.Infof("Unregistering Histogram Vec Metric '%v' ...", name)
ok = registerer.Unregister(histogramVec)
if !ok {
log.Errorf("Failed to unregister Histogram Vec Metric '%v'.", name)
return
}
delete(histogramVecs, name)
log.Infof("Histogram Vec Metric '%v' successfully unregistered!", name)
}
// registerSummaryIfNotExists registers single summary metric if not exists
func registerSummaryIfNotExists(opts prometheus.SummaryOpts) {
log := log.WithFields("metricName", opts.Name)
if _, exist := summaries[opts.Name]; exist {
log.Warn("Summary metric already exists.")
return
}
log.Debug("Creating Summary Metric...")
summary := prometheus.NewSummary(opts)
log.Debugf("Summary Metric successfully created! Labels: %p", opts.ConstLabels)
log.Debug("Registering Summary Metric...")
registerer.MustRegister(summary)
log.Debug("Summary Metric successfully registered!")
summaries[opts.Name] = summary
}
// unregisterSummaryIfExists unregisters single summary metric if exists
func unregisterSummaryIfExists(name string) {
var (
summary prometheus.Summary
ok bool
)
log := log.WithFields("metricName", name)
if summary, ok = summaries[name]; !ok {
log.Warn("Trying to delete non-existing Summary summary.")
return
}
log.Debug("Unregistering Summary Metric...")
ok = registerer.Unregister(summary)
if !ok {
log.Error("Failed to unregister Summary Metric.")
return
}
delete(summaries, name)
log.Debug("Summary Metric successfully unregistered!")
}