Added support for counter vectors
This commit is contained in:
parent
61f88aff44
commit
4988fbcf28
47
metrics.go
47
metrics.go
|
@ -23,19 +23,21 @@ var DefObjectives = map[float64]float64{
|
|||
type Metrics struct {
|
||||
sync.RWMutex
|
||||
|
||||
namespace string
|
||||
metrics map[string]prometheus.Metric
|
||||
guagevecs map[string]*prometheus.GaugeVec
|
||||
sumvecs map[string]*prometheus.SummaryVec
|
||||
namespace string
|
||||
metrics map[string]prometheus.Metric
|
||||
countervecs map[string]*prometheus.CounterVec
|
||||
guagevecs map[string]*prometheus.GaugeVec
|
||||
sumvecs map[string]*prometheus.SummaryVec
|
||||
}
|
||||
|
||||
// NewMetrics ...
|
||||
func NewMetrics(namespace string) *Metrics {
|
||||
return &Metrics{
|
||||
namespace: namespace,
|
||||
metrics: make(map[string]prometheus.Metric),
|
||||
guagevecs: make(map[string]*prometheus.GaugeVec),
|
||||
sumvecs: make(map[string]*prometheus.SummaryVec),
|
||||
namespace: namespace,
|
||||
metrics: make(map[string]prometheus.Metric),
|
||||
countervecs: make(map[string]*prometheus.CounterVec),
|
||||
guagevecs: make(map[string]*prometheus.GaugeVec),
|
||||
sumvecs: make(map[string]*prometheus.SummaryVec),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,6 +82,27 @@ func (m *Metrics) NewCounterFunc(subsystem, name, help string, f func() float64)
|
|||
return counter
|
||||
}
|
||||
|
||||
// NewCounterVec ...
|
||||
func (m *Metrics) NewCounterVec(subsystem, name, help string, labels []string) *prometheus.CounterVec {
|
||||
countervec := prometheus.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Namespace: m.namespace,
|
||||
Subsystem: subsystem,
|
||||
Name: name,
|
||||
Help: help,
|
||||
},
|
||||
labels,
|
||||
)
|
||||
|
||||
key := fmt.Sprintf("%s_%s", subsystem, name)
|
||||
m.Lock()
|
||||
m.countervecs[key] = countervec
|
||||
m.Unlock()
|
||||
prometheus.MustRegister(countervec)
|
||||
|
||||
return countervec
|
||||
}
|
||||
|
||||
// NewGauge ...
|
||||
func (m *Metrics) NewGauge(subsystem, name, help string) prometheus.Gauge {
|
||||
guage := prometheus.NewGauge(
|
||||
|
@ -191,6 +214,14 @@ func (m *Metrics) Counter(subsystem, name string) prometheus.Counter {
|
|||
return m.metrics[key].(prometheus.Counter)
|
||||
}
|
||||
|
||||
// CounterVec ...
|
||||
func (m *Metrics) CounterVec(subsystem, name string) *prometheus.CounterVec {
|
||||
key := fmt.Sprintf("%s_%s", subsystem, name)
|
||||
m.RLock()
|
||||
defer m.RUnlock()
|
||||
return m.countervecs[key]
|
||||
}
|
||||
|
||||
// Gauge ...
|
||||
func (m *Metrics) Gauge(subsystem, name string) prometheus.Gauge {
|
||||
key := fmt.Sprintf("%s_%s", subsystem, name)
|
||||
|
|
|
@ -14,11 +14,13 @@ func TestMetrics(t *testing.T) {
|
|||
m := NewMetrics("test")
|
||||
m.NewCounter("foo", "counter", "help")
|
||||
m.NewCounterFunc("foo", "counter_func", "help", func() float64 { return 1.0 })
|
||||
m.NewCounterVec("foo", "counter_vec", "help", []string{"test"})
|
||||
m.NewGauge("foo", "gauge", "help")
|
||||
m.NewGaugeFunc("foo", "gauge_func", "help", func() float64 { return 1.0 })
|
||||
m.NewGaugeVec("foo", "gauge_vec", "help", []string{"test"})
|
||||
|
||||
m.Counter("foo", "counter").Inc()
|
||||
m.CounterVec("foo", "counter_vec").WithLabelValues("test").Add(1)
|
||||
m.Gauge("foo", "gauge").Add(1)
|
||||
m.GaugeVec("foo", "gauge_vec").WithLabelValues("test").Add(1)
|
||||
|
||||
|
@ -36,6 +38,9 @@ test_foo_counter 1
|
|||
# HELP test_foo_counter_func help
|
||||
# TYPE test_foo_counter_func counter
|
||||
test_foo_counter_func 1
|
||||
# HELP test_foo_counter_vec help
|
||||
# TYPE test_foo_counter_vec counter
|
||||
test_foo_counter_vec{test="test"} 1
|
||||
# HELP test_foo_gauge help
|
||||
# TYPE test_foo_gauge gauge
|
||||
test_foo_gauge 1
|
||||
|
|
Loading…
Reference in New Issue