-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgauge.go
62 lines (53 loc) · 1.62 KB
/
gauge.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"
)
// Gauge keeps the contents of underlying gauge, including labels
type Gauge struct {
watcher *prometheus.GaugeVec
client *statsd.Statter
prefix string
labels []string
}
// StaticGauge keeps the contents of underlying gauge, excluding labels
type StaticGauge struct {
Base *Gauge
values []string
}
// newGauge creates a prometheus.GaugeVec and register it only if isPrometheusEnabled is true otherwise it keeps
// watcher unregistered to avoid multiple register error in development setups.
func newGauge(namespace, subsystem, name string, labelNames []string, client *statsd.Statter, isPrometheusEnabled bool) *Gauge {
opts := prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
}
vec := prometheus.NewGaugeVec(opts, labelNames)
if isPrometheusEnabled {
prometheus.MustRegister(vec)
}
return &Gauge{
watcher: vec,
labels: labelNames,
client: client,
prefix: strings.Join([]string{namespace, subsystem, name}, "."),
}
}
// Set sets value of the gauge
func (w *Gauge) Set(value float64, labelValues ...string) {
w.watcher.WithLabelValues(labelValues...).Set(value)
metaLabel := w.prefix + "." + strings.Join(labelValues, ".")
(*w.client).Gauge(metaLabel, int64(value), 1.0)
}
func (w *Gauge) newStaticGauge(labelValues ...string) *StaticGauge {
return &StaticGauge{
Base: w,
values: labelValues,
}
}
// Set sets value of the gauge
func (rg *StaticGauge) Set(value float64) {
rg.Base.watcher.WithLabelValues(rg.values...).Set(value)
}