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