Prince Pereira | c1c21d6 | 2021-04-22 08:38:15 +0000 | [diff] [blame^] | 1 | package metrics |
| 2 | |
| 3 | import ( |
| 4 | "math" |
| 5 | "sync/atomic" |
| 6 | ) |
| 7 | |
| 8 | // GaugeFloat64s hold a float64 value that can be set arbitrarily. |
| 9 | type 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. |
| 17 | func 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. |
| 25 | func 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. |
| 35 | func 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. |
| 45 | func 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. |
| 53 | func 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. |
| 63 | type GaugeFloat64Snapshot float64 |
| 64 | |
| 65 | // Snapshot returns the snapshot. |
| 66 | func (g GaugeFloat64Snapshot) Snapshot() GaugeFloat64 { return g } |
| 67 | |
| 68 | // Update panics. |
| 69 | func (GaugeFloat64Snapshot) Update(float64) { |
| 70 | panic("Update called on a GaugeFloat64Snapshot") |
| 71 | } |
| 72 | |
| 73 | // Value returns the value at the time the snapshot was taken. |
| 74 | func (g GaugeFloat64Snapshot) Value() float64 { return float64(g) } |
| 75 | |
| 76 | // NilGauge is a no-op Gauge. |
| 77 | type NilGaugeFloat64 struct{} |
| 78 | |
| 79 | // Snapshot is a no-op. |
| 80 | func (NilGaugeFloat64) Snapshot() GaugeFloat64 { return NilGaugeFloat64{} } |
| 81 | |
| 82 | // Update is a no-op. |
| 83 | func (NilGaugeFloat64) Update(v float64) {} |
| 84 | |
| 85 | // Value is a no-op. |
| 86 | func (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. |
| 90 | type StandardGaugeFloat64 struct { |
| 91 | value uint64 |
| 92 | } |
| 93 | |
| 94 | // Snapshot returns a read-only copy of the gauge. |
| 95 | func (g *StandardGaugeFloat64) Snapshot() GaugeFloat64 { |
| 96 | return GaugeFloat64Snapshot(g.Value()) |
| 97 | } |
| 98 | |
| 99 | // Update updates the gauge's value. |
| 100 | func (g *StandardGaugeFloat64) Update(v float64) { |
| 101 | atomic.StoreUint64(&g.value, math.Float64bits(v)) |
| 102 | } |
| 103 | |
| 104 | // Value returns the gauge's current value. |
| 105 | func (g *StandardGaugeFloat64) Value() float64 { |
| 106 | return math.Float64frombits(atomic.LoadUint64(&g.value)) |
| 107 | } |
| 108 | |
| 109 | // FunctionalGaugeFloat64 returns value from given function |
| 110 | type FunctionalGaugeFloat64 struct { |
| 111 | value func() float64 |
| 112 | } |
| 113 | |
| 114 | // Value returns the gauge's current value. |
| 115 | func (g FunctionalGaugeFloat64) Value() float64 { |
| 116 | return g.value() |
| 117 | } |
| 118 | |
| 119 | // Snapshot returns the snapshot. |
| 120 | func (g FunctionalGaugeFloat64) Snapshot() GaugeFloat64 { return GaugeFloat64Snapshot(g.Value()) } |
| 121 | |
| 122 | // Update panics. |
| 123 | func (FunctionalGaugeFloat64) Update(float64) { |
| 124 | panic("Update called on a FunctionalGaugeFloat64") |
| 125 | } |