blob: cb57a93889fc23b01222be3181510a169b856e76 [file] [log] [blame]
Scott Baker2d897982019-09-24 11:50:08 -07001package metrics
2
3import "sync/atomic"
4
5// Gauges hold an int64 value that can be set arbitrarily.
6type Gauge interface {
7 Snapshot() Gauge
8 Update(int64)
9 Value() int64
10}
11
12// GetOrRegisterGauge returns an existing Gauge or constructs and registers a
13// new StandardGauge.
14func GetOrRegisterGauge(name string, r Registry) Gauge {
15 if nil == r {
16 r = DefaultRegistry
17 }
18 return r.GetOrRegister(name, NewGauge).(Gauge)
19}
20
21// NewGauge constructs a new StandardGauge.
22func NewGauge() Gauge {
23 if UseNilMetrics {
24 return NilGauge{}
25 }
26 return &StandardGauge{0}
27}
28
29// NewRegisteredGauge constructs and registers a new StandardGauge.
30func NewRegisteredGauge(name string, r Registry) Gauge {
31 c := NewGauge()
32 if nil == r {
33 r = DefaultRegistry
34 }
35 r.Register(name, c)
36 return c
37}
38
39// NewFunctionalGauge constructs a new FunctionalGauge.
40func NewFunctionalGauge(f func() int64) Gauge {
41 if UseNilMetrics {
42 return NilGauge{}
43 }
44 return &FunctionalGauge{value: f}
45}
46
47// NewRegisteredFunctionalGauge constructs and registers a new StandardGauge.
48func NewRegisteredFunctionalGauge(name string, r Registry, f func() int64) Gauge {
49 c := NewFunctionalGauge(f)
50 if nil == r {
51 r = DefaultRegistry
52 }
53 r.Register(name, c)
54 return c
55}
56
57// GaugeSnapshot is a read-only copy of another Gauge.
58type GaugeSnapshot int64
59
60// Snapshot returns the snapshot.
61func (g GaugeSnapshot) Snapshot() Gauge { return g }
62
63// Update panics.
64func (GaugeSnapshot) Update(int64) {
65 panic("Update called on a GaugeSnapshot")
66}
67
68// Value returns the value at the time the snapshot was taken.
69func (g GaugeSnapshot) Value() int64 { return int64(g) }
70
71// NilGauge is a no-op Gauge.
72type NilGauge struct{}
73
74// Snapshot is a no-op.
75func (NilGauge) Snapshot() Gauge { return NilGauge{} }
76
77// Update is a no-op.
78func (NilGauge) Update(v int64) {}
79
80// Value is a no-op.
81func (NilGauge) Value() int64 { return 0 }
82
83// StandardGauge is the standard implementation of a Gauge and uses the
84// sync/atomic package to manage a single int64 value.
85type StandardGauge struct {
86 value int64
87}
88
89// Snapshot returns a read-only copy of the gauge.
90func (g *StandardGauge) Snapshot() Gauge {
91 return GaugeSnapshot(g.Value())
92}
93
94// Update updates the gauge's value.
95func (g *StandardGauge) Update(v int64) {
96 atomic.StoreInt64(&g.value, v)
97}
98
99// Value returns the gauge's current value.
100func (g *StandardGauge) Value() int64 {
101 return atomic.LoadInt64(&g.value)
102}
103
104// FunctionalGauge returns value from given function
105type FunctionalGauge struct {
106 value func() int64
107}
108
109// Value returns the gauge's current value.
110func (g FunctionalGauge) Value() int64 {
111 return g.value()
112}
113
114// Snapshot returns the snapshot.
115func (g FunctionalGauge) Snapshot() Gauge { return GaugeSnapshot(g.Value()) }
116
117// Update panics.
118func (FunctionalGauge) Update(int64) {
119 panic("Update called on a FunctionalGauge")
120}