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