github.com/calmw/ethereum@v0.1.1/metrics/gauge_float64.go (about) 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 !Enabled { 27 return NilGaugeFloat64{} 28 } 29 return &StandardGaugeFloat64{} 30 } 31 32 // NewRegisteredGaugeFloat64 constructs and registers a new StandardGaugeFloat64. 33 func NewRegisteredGaugeFloat64(name string, r Registry) GaugeFloat64 { 34 c := NewGaugeFloat64() 35 if nil == r { 36 r = DefaultRegistry 37 } 38 r.Register(name, c) 39 return c 40 } 41 42 // NewFunctionalGauge constructs a new FunctionalGauge. 43 func NewFunctionalGaugeFloat64(f func() float64) GaugeFloat64 { 44 if !Enabled { 45 return NilGaugeFloat64{} 46 } 47 return &FunctionalGaugeFloat64{value: f} 48 } 49 50 // NewRegisteredFunctionalGauge constructs and registers a new StandardGauge. 51 func NewRegisteredFunctionalGaugeFloat64(name string, r Registry, f func() float64) GaugeFloat64 { 52 c := NewFunctionalGaugeFloat64(f) 53 if nil == r { 54 r = DefaultRegistry 55 } 56 r.Register(name, c) 57 return c 58 } 59 60 // GaugeFloat64Snapshot is a read-only copy of another GaugeFloat64. 61 type GaugeFloat64Snapshot float64 62 63 // Snapshot returns the snapshot. 64 func (g GaugeFloat64Snapshot) Snapshot() GaugeFloat64 { return g } 65 66 // Update panics. 67 func (GaugeFloat64Snapshot) Update(float64) { 68 panic("Update called on a GaugeFloat64Snapshot") 69 } 70 71 // Value returns the value at the time the snapshot was taken. 72 func (g GaugeFloat64Snapshot) Value() float64 { return float64(g) } 73 74 // NilGauge is a no-op Gauge. 75 type NilGaugeFloat64 struct{} 76 77 // Snapshot is a no-op. 78 func (NilGaugeFloat64) Snapshot() GaugeFloat64 { return NilGaugeFloat64{} } 79 80 // Update is a no-op. 81 func (NilGaugeFloat64) Update(v float64) {} 82 83 // Value is a no-op. 84 func (NilGaugeFloat64) Value() float64 { return 0.0 } 85 86 // StandardGaugeFloat64 is the standard implementation of a GaugeFloat64 and uses 87 // atomic to manage a single float64 value. 88 type StandardGaugeFloat64 struct { 89 floatBits atomic.Uint64 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.floatBits.Store(math.Float64bits(v)) 100 } 101 102 // Value returns the gauge's current value. 103 func (g *StandardGaugeFloat64) Value() float64 { 104 return math.Float64frombits(g.floatBits.Load()) 105 } 106 107 // FunctionalGaugeFloat64 returns value from given function 108 type FunctionalGaugeFloat64 struct { 109 value func() float64 110 } 111 112 // Value returns the gauge's current value. 113 func (g FunctionalGaugeFloat64) Value() float64 { 114 return g.value() 115 } 116 117 // Snapshot returns the snapshot. 118 func (g FunctionalGaugeFloat64) Snapshot() GaugeFloat64 { return GaugeFloat64Snapshot(g.Value()) } 119 120 // Update panics. 121 func (FunctionalGaugeFloat64) Update(float64) { 122 panic("Update called on a FunctionalGaugeFloat64") 123 }