-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathprometheusexporter.go
93 lines (81 loc) · 2.66 KB
/
prometheusexporter.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package exporter
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
const (
RetinaNamespace = "networkobservability"
retinaControlPlaneNamespace = "controlplane_networkobservability"
)
type CallBackFunc func()
var (
// CombinedGatherer is the combined registry for all metrics to be exposed by promhttp
CombinedGatherer *prometheus.Registry
// AdvancedRegistry is used for advanced metrics. This registry can be reset upon metrics config reconciliation
AdvancedRegistry *prometheus.Registry
DefaultRegistry *prometheus.Registry
MetricsServeCallback CallBackFunc
)
func init() {
DefaultRegistry = prometheus.DefaultRegisterer.(*prometheus.Registry)
AdvancedRegistry = prometheus.NewRegistry()
CombinedGatherer = prometheus.NewRegistry()
CombinedGatherer.MustRegister(AdvancedRegistry)
CombinedGatherer.MustRegister(DefaultRegistry)
MetricsServeCallback = func() {}
}
func ResetAdvancedMetricsRegistry() {
CombinedGatherer.Unregister(AdvancedRegistry)
AdvancedRegistry = prometheus.NewRegistry()
CombinedGatherer.MustRegister(AdvancedRegistry)
MetricsServeCallback()
}
func RegisterMetricsServeCallback(callback CallBackFunc) {
MetricsServeCallback = callback
}
func CreatePrometheusCounterVecForMetric(r prometheus.Registerer, name, desc string, labels ...string) *prometheus.CounterVec {
return promauto.With(r).NewCounterVec(
prometheus.CounterOpts{
Namespace: RetinaNamespace,
Name: name,
Help: desc,
},
labels,
)
}
func CreatePrometheusGaugeVecForMetric(r prometheus.Registerer, name, desc string, labels ...string) *prometheus.GaugeVec {
return promauto.With(r).NewGaugeVec(
prometheus.GaugeOpts{
Namespace: RetinaNamespace,
Name: name,
Help: desc,
},
labels,
)
}
func CreatePrometheusCounterVecForControlPlaneMetric(r prometheus.Registerer, name, desc string, labels ...string) *prometheus.CounterVec {
return promauto.With(r).NewCounterVec(
prometheus.CounterOpts{
Namespace: retinaControlPlaneNamespace,
Name: name,
Help: desc,
},
labels,
)
}
func CreatePrometheusHistogramWithLinearBucketsForMetric(r prometheus.Registerer, name, desc string, start, width float64, count int) prometheus.Histogram {
opts := prometheus.HistogramOpts{
Namespace: RetinaNamespace,
Name: name,
Help: desc,
Buckets: prometheus.LinearBuckets(start, width, count),
}
return promauto.With(r).NewHistogram(opts)
}
func UnregisterMetric(r prometheus.Registerer, metric prometheus.Collector) {
if metric != nil {
r.Unregister(metric)
}
}