github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/utilities/metrics/gauge_float64.go (about) 1 package metrics 2 3 import "sync" 4 5 type GaugeFloat64 interface { 6 Snapshot() GaugeFloat64 7 Update(float64) 8 Value() float64 9 } 10 11 func GetOrRegisterGaugeFloat64(name string, r Registry) GaugeFloat64 { 12 if nil == r { 13 r = DefaultRegistry 14 } 15 return r.GetOrRegister(name, NewGaugeFloat64()).(GaugeFloat64) 16 } 17 18 func NewGaugeFloat64() GaugeFloat64 { 19 if !Enabled { 20 return NilGaugeFloat64{} 21 } 22 return &StandardGaugeFloat64{ 23 value: 0.0, 24 } 25 } 26 27 func NewRegisteredGaugeFloat64(name string, r Registry) GaugeFloat64 { 28 c := NewGaugeFloat64() 29 if nil == r { 30 r = DefaultRegistry 31 } 32 r.Register(name, c) 33 return c 34 } 35 36 func NewFunctionalGaugeFloat64(f func() float64) GaugeFloat64 { 37 if !Enabled { 38 return NilGaugeFloat64{} 39 } 40 return &FunctionalGaugeFloat64{value: f} 41 } 42 43 func NewRegisteredFunctionalGaugeFloat64(name string, r Registry, f func() float64) GaugeFloat64 { 44 c := NewFunctionalGaugeFloat64(f) 45 if nil == r { 46 r = DefaultRegistry 47 } 48 r.Register(name, c) 49 return c 50 } 51 52 type GaugeFloat64Snapshot float64 53 54 func (g GaugeFloat64Snapshot) Snapshot() GaugeFloat64 { return g } 55 56 func (GaugeFloat64Snapshot) Update(float64) { 57 panic("Update called on a GaugeFloat64Snapshot") 58 } 59 60 func (g GaugeFloat64Snapshot) Value() float64 { return float64(g) } 61 62 type NilGaugeFloat64 struct{} 63 64 func (NilGaugeFloat64) Snapshot() GaugeFloat64 { return NilGaugeFloat64{} } 65 66 func (NilGaugeFloat64) Update(v float64) {} 67 68 func (NilGaugeFloat64) Value() float64 { return 0.0 } 69 70 type StandardGaugeFloat64 struct { 71 mutex sync.Mutex 72 value float64 73 } 74 75 func (g *StandardGaugeFloat64) Snapshot() GaugeFloat64 { 76 return GaugeFloat64Snapshot(g.Value()) 77 } 78 79 func (g *StandardGaugeFloat64) Update(v float64) { 80 g.mutex.Lock() 81 defer g.mutex.Unlock() 82 g.value = v 83 } 84 85 func (g *StandardGaugeFloat64) Value() float64 { 86 g.mutex.Lock() 87 defer g.mutex.Unlock() 88 return g.value 89 } 90 91 type FunctionalGaugeFloat64 struct { 92 value func() float64 93 } 94 95 func (g FunctionalGaugeFloat64) Value() float64 { 96 return g.value() 97 } 98 99 func (g FunctionalGaugeFloat64) Snapshot() GaugeFloat64 { return GaugeFloat64Snapshot(g.Value()) } 100 101 func (FunctionalGaugeFloat64) Update(float64) { 102 panic("Update called on a FunctionalGaugeFloat64") 103 }