aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/docker/go-metrics/gauge.go
blob: 74296e87740b3d9bf48365da77992f2d4b33795b (plain)
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
63
64
65
66
67
68
69
70
71
72
package metrics

import "github.com/prometheus/client_golang/prometheus"

// Gauge is a metric that allows incrementing and decrementing a value
type Gauge interface {
	Inc(...float64)
	Dec(...float64)

	// Add adds the provided value to the gauge's current value
	Add(float64)

	// Set replaces the gauge's current value with the provided value
	Set(float64)
}

// LabeledGauge describes a gauge the must have values populated before use.
type LabeledGauge interface {
	WithValues(labels ...string) Gauge
}

type labeledGauge struct {
	pg *prometheus.GaugeVec
}

func (lg *labeledGauge) WithValues(labels ...string) Gauge {
	return &gauge{pg: lg.pg.WithLabelValues(labels...)}
}

func (lg *labeledGauge) Describe(c chan<- *prometheus.Desc) {
	lg.pg.Describe(c)
}

func (lg *labeledGauge) Collect(c chan<- prometheus.Metric) {
	lg.pg.Collect(c)
}

type gauge struct {
	pg prometheus.Gauge
}

func (g *gauge) Inc(vs ...float64) {
	if len(vs) == 0 {
		g.pg.Inc()
	}

	g.Add(sumFloat64(vs...))
}

func (g *gauge) Dec(vs ...float64) {
	if len(vs) == 0 {
		g.pg.Dec()
	}

	g.Add(-sumFloat64(vs...))
}

func (g *gauge) Add(v float64) {
	g.pg.Add(v)
}

func (g *gauge) Set(v float64) {
	g.pg.Set(v)
}

func (g *gauge) Describe(c chan<- *prometheus.Desc) {
	g.pg.Describe(c)
}

func (g *gauge) Collect(c chan<- prometheus.Metric) {
	g.pg.Collect(c)
}