-
Notifications
You must be signed in to change notification settings - Fork 2
/
counter.go
48 lines (39 loc) · 1.2 KB
/
counter.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
package collectors
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
)
type Counter struct{}
func (c *Counter) Update(collector prometheus.Collector, value float64, labels ...string) error {
if len(labels) > 0 {
if metricVec, ok := collector.(*prometheus.CounterVec); ok {
metric := metricVec.WithLabelValues(labels...)
metric.Add(value)
return nil
}
return fmt.Errorf("invalid metric type, expected CounterVec for labels")
}
if metric, ok := collector.(prometheus.Counter); ok {
metric.Add(value)
return nil
}
return fmt.Errorf("invalid metric type, expected Counter")
}
func (c *Counter) Inc(collector prometheus.Collector, labels ...string) error {
if len(labels) > 0 {
if metricVec, ok := collector.(*prometheus.CounterVec); ok {
metric := metricVec.WithLabelValues(labels...)
metric.Inc()
return nil
}
return fmt.Errorf("invalid metric type, expected CounterVec for labels")
}
if metric, ok := collector.(prometheus.Counter); ok {
metric.Inc()
return nil
}
return fmt.Errorf("invalid metric type, expected Counter")
}
func (c *Counter) Dec(_ prometheus.Collector, _ ...string) error {
return fmt.Errorf("dec is not supported by counter metric")
}