-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.go
62 lines (53 loc) · 1.62 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package epimetheus
import (
"strings"
"github.com/cactus/go-statsd-client/statsd"
"github.com/prometheus/client_golang/prometheus"
)
// Counter keeps the contents of underlying counter, including labels
type Counter struct {
watcher *prometheus.CounterVec
client *statsd.Statter
prefix string
labels []string
}
// StaticCounter keeps the contents of underlying counter, excluding labels
type StaticCounter struct {
Base *Counter
values []string
}
// newCounter creates a prometheus.CounterVec and register it only if isPrometheusEnabled is true otherwise it keeps
// watcher unregistered to avoid multiple register error in development setups.
func newCounter(namespace, subsystem, name string, labelNames []string, client *statsd.Statter, isPrometheusEnabled bool) *Counter {
opts := prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
}
vec := prometheus.NewCounterVec(opts, labelNames)
if isPrometheusEnabled {
prometheus.MustRegister(vec)
}
return &Counter{
watcher: vec,
labels: labelNames,
client: client,
prefix: strings.Join([]string{namespace, subsystem, name}, "."),
}
}
// Inc increments the value of current Counter
func (w *Counter) Inc(labelValues ...string) {
w.watcher.WithLabelValues(labelValues...).Inc()
metaLabel := w.prefix + "." + strings.Join(labelValues, ".")
(*w.client).Inc(metaLabel, 1, 1.0)
}
func (w *Counter) newStaticCounter(labelValues ...string) *StaticCounter {
return &StaticCounter{
Base: w,
values: labelValues,
}
}
// Inc increments the value of current Counter
func (sc *StaticCounter) Inc() {
sc.Base.Inc(sc.values...)
}