forked from kawamuray/prometheus-exporter-harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
49 lines (41 loc) · 1.16 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
package harness
import (
log "github.com/Sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
)
type MetricRegistry struct {
metrics map[string]prometheus.Collector
}
func newRegistry() *MetricRegistry {
return &MetricRegistry{
metrics: make(map[string]prometheus.Collector),
}
}
func (reg *MetricRegistry) Register(name string, metric prometheus.Collector) {
log.Infof("metric registered;name:<%s>", name)
reg.metrics[name] = metric
prometheus.MustRegister(metric)
}
func (reg *MetricRegistry) Unregister(name string) {
if metric := reg.metrics[name]; metric != nil {
log.Infof("metric unregistered;name:<%s>", name)
prometheus.Unregister(metric)
delete(reg.metrics, name)
}
}
func (reg *MetricRegistry) Get(name string) prometheus.Collector {
return reg.metrics[name]
}
// Since prometheus.MetricVec is a struct but not interface,
// need to intrduce an interface to check if we can call Reset() on a metric.
type resettable interface {
Reset()
}
func (reg *MetricRegistry) Reset() {
for name, metric := range reg.metrics {
if vec, ok := metric.(resettable); ok {
log.Debugf("resetting metric;name:<%s>", name)
vec.Reset()
}
}
}