github.com/theQRL/go-zond@v0.1.1/metrics/gauge_float64.go (about)

     1  package metrics
     2  
     3  import (
     4  	"math"
     5  	"sync/atomic"
     6  )
     7  
     8  type GaugeFloat64Snapshot interface {
     9  	Value() float64
    10  }
    11  
    12  // GaugeFloat64 hold a float64 value that can be set arbitrarily.
    13  type GaugeFloat64 interface {
    14  	Snapshot() GaugeFloat64Snapshot
    15  	Update(float64)
    16  }
    17  
    18  // GetOrRegisterGaugeFloat64 returns an existing GaugeFloat64 or constructs and registers a
    19  // new StandardGaugeFloat64.
    20  func GetOrRegisterGaugeFloat64(name string, r Registry) GaugeFloat64 {
    21  	if nil == r {
    22  		r = DefaultRegistry
    23  	}
    24  	return r.GetOrRegister(name, NewGaugeFloat64()).(GaugeFloat64)
    25  }
    26  
    27  // NewGaugeFloat64 constructs a new StandardGaugeFloat64.
    28  func NewGaugeFloat64() GaugeFloat64 {
    29  	if !Enabled {
    30  		return NilGaugeFloat64{}
    31  	}
    32  	return &StandardGaugeFloat64{}
    33  }
    34  
    35  // NewRegisteredGaugeFloat64 constructs and registers a new StandardGaugeFloat64.
    36  func NewRegisteredGaugeFloat64(name string, r Registry) GaugeFloat64 {
    37  	c := NewGaugeFloat64()
    38  	if nil == r {
    39  		r = DefaultRegistry
    40  	}
    41  	r.Register(name, c)
    42  	return c
    43  }
    44  
    45  // gaugeFloat64Snapshot is a read-only copy of another GaugeFloat64.
    46  type gaugeFloat64Snapshot float64
    47  
    48  // Value returns the value at the time the snapshot was taken.
    49  func (g gaugeFloat64Snapshot) Value() float64 { return float64(g) }
    50  
    51  // NilGauge is a no-op Gauge.
    52  type NilGaugeFloat64 struct{}
    53  
    54  func (NilGaugeFloat64) Snapshot() GaugeFloat64Snapshot { return NilGaugeFloat64{} }
    55  func (NilGaugeFloat64) Update(v float64)               {}
    56  func (NilGaugeFloat64) Value() float64                 { return 0.0 }
    57  
    58  // StandardGaugeFloat64 is the standard implementation of a GaugeFloat64 and uses
    59  // atomic to manage a single float64 value.
    60  type StandardGaugeFloat64 struct {
    61  	floatBits atomic.Uint64
    62  }
    63  
    64  // Snapshot returns a read-only copy of the gauge.
    65  func (g *StandardGaugeFloat64) Snapshot() GaugeFloat64Snapshot {
    66  	v := math.Float64frombits(g.floatBits.Load())
    67  	return gaugeFloat64Snapshot(v)
    68  }
    69  
    70  // Update updates the gauge's value.
    71  func (g *StandardGaugeFloat64) Update(v float64) {
    72  	g.floatBits.Store(math.Float64bits(v))
    73  }