2015-06-22 12:00:55 +03:00
|
|
|
package metrics
|
|
|
|
|
2023-04-04 16:53:44 +03:00
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
2015-06-22 12:00:55 +03:00
|
|
|
|
|
|
|
// GaugeFloat64s hold a float64 value that can be set arbitrarily.
|
|
|
|
type GaugeFloat64 interface {
|
|
|
|
Snapshot() GaugeFloat64
|
|
|
|
Update(float64)
|
|
|
|
Value() float64
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrRegisterGaugeFloat64 returns an existing GaugeFloat64 or constructs and registers a
|
|
|
|
// new StandardGaugeFloat64.
|
|
|
|
func GetOrRegisterGaugeFloat64(name string, r Registry) GaugeFloat64 {
|
|
|
|
if nil == r {
|
|
|
|
r = DefaultRegistry
|
|
|
|
}
|
|
|
|
return r.GetOrRegister(name, NewGaugeFloat64()).(GaugeFloat64)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGaugeFloat64 constructs a new StandardGaugeFloat64.
|
|
|
|
func NewGaugeFloat64() GaugeFloat64 {
|
2018-02-23 12:56:08 +03:00
|
|
|
if !Enabled {
|
2015-06-22 12:00:55 +03:00
|
|
|
return NilGaugeFloat64{}
|
|
|
|
}
|
2023-04-04 16:53:44 +03:00
|
|
|
return &StandardGaugeFloat64{}
|
2015-06-22 12:00:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRegisteredGaugeFloat64 constructs and registers a new StandardGaugeFloat64.
|
|
|
|
func NewRegisteredGaugeFloat64(name string, r Registry) GaugeFloat64 {
|
|
|
|
c := NewGaugeFloat64()
|
|
|
|
if nil == r {
|
|
|
|
r = DefaultRegistry
|
|
|
|
}
|
|
|
|
r.Register(name, c)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-10-28 20:05:01 +03:00
|
|
|
// NewFunctionalGauge constructs a new FunctionalGauge.
|
|
|
|
func NewFunctionalGaugeFloat64(f func() float64) GaugeFloat64 {
|
2018-02-23 12:56:08 +03:00
|
|
|
if !Enabled {
|
2016-10-28 20:05:01 +03:00
|
|
|
return NilGaugeFloat64{}
|
|
|
|
}
|
|
|
|
return &FunctionalGaugeFloat64{value: f}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRegisteredFunctionalGauge constructs and registers a new StandardGauge.
|
|
|
|
func NewRegisteredFunctionalGaugeFloat64(name string, r Registry, f func() float64) GaugeFloat64 {
|
|
|
|
c := NewFunctionalGaugeFloat64(f)
|
|
|
|
if nil == r {
|
|
|
|
r = DefaultRegistry
|
|
|
|
}
|
|
|
|
r.Register(name, c)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2015-06-22 12:00:55 +03:00
|
|
|
// GaugeFloat64Snapshot is a read-only copy of another GaugeFloat64.
|
|
|
|
type GaugeFloat64Snapshot float64
|
|
|
|
|
|
|
|
// Snapshot returns the snapshot.
|
|
|
|
func (g GaugeFloat64Snapshot) Snapshot() GaugeFloat64 { return g }
|
|
|
|
|
|
|
|
// Update panics.
|
|
|
|
func (GaugeFloat64Snapshot) Update(float64) {
|
|
|
|
panic("Update called on a GaugeFloat64Snapshot")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the value at the time the snapshot was taken.
|
|
|
|
func (g GaugeFloat64Snapshot) Value() float64 { return float64(g) }
|
|
|
|
|
|
|
|
// NilGauge is a no-op Gauge.
|
|
|
|
type NilGaugeFloat64 struct{}
|
|
|
|
|
|
|
|
// Snapshot is a no-op.
|
|
|
|
func (NilGaugeFloat64) Snapshot() GaugeFloat64 { return NilGaugeFloat64{} }
|
|
|
|
|
|
|
|
// Update is a no-op.
|
|
|
|
func (NilGaugeFloat64) Update(v float64) {}
|
|
|
|
|
|
|
|
// Value is a no-op.
|
|
|
|
func (NilGaugeFloat64) Value() float64 { return 0.0 }
|
|
|
|
|
|
|
|
// StandardGaugeFloat64 is the standard implementation of a GaugeFloat64 and uses
|
2023-04-04 16:53:44 +03:00
|
|
|
// atomic to manage a single float64 value.
|
2015-06-22 12:00:55 +03:00
|
|
|
type StandardGaugeFloat64 struct {
|
2023-04-04 16:53:44 +03:00
|
|
|
floatBits atomic.Uint64
|
2015-06-22 12:00:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshot returns a read-only copy of the gauge.
|
|
|
|
func (g *StandardGaugeFloat64) Snapshot() GaugeFloat64 {
|
|
|
|
return GaugeFloat64Snapshot(g.Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates the gauge's value.
|
|
|
|
func (g *StandardGaugeFloat64) Update(v float64) {
|
2023-04-04 16:53:44 +03:00
|
|
|
g.floatBits.Store(math.Float64bits(v))
|
2015-06-22 12:00:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the gauge's current value.
|
|
|
|
func (g *StandardGaugeFloat64) Value() float64 {
|
2023-04-04 16:53:44 +03:00
|
|
|
return math.Float64frombits(g.floatBits.Load())
|
2015-06-22 12:00:55 +03:00
|
|
|
}
|
2016-10-28 20:05:01 +03:00
|
|
|
|
|
|
|
// FunctionalGaugeFloat64 returns value from given function
|
|
|
|
type FunctionalGaugeFloat64 struct {
|
|
|
|
value func() float64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the gauge's current value.
|
|
|
|
func (g FunctionalGaugeFloat64) Value() float64 {
|
|
|
|
return g.value()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshot returns the snapshot.
|
|
|
|
func (g FunctionalGaugeFloat64) Snapshot() GaugeFloat64 { return GaugeFloat64Snapshot(g.Value()) }
|
|
|
|
|
|
|
|
// Update panics.
|
|
|
|
func (FunctionalGaugeFloat64) Update(float64) {
|
|
|
|
panic("Update called on a FunctionalGaugeFloat64")
|
|
|
|
}
|