github.com/codingfuture/orig-energi3@v0.8.4/metrics/gauge.go (about) 1 // Copyright 2018 The Energi Core Authors 2 // Copyright 2016 The go-ethereum Authors 3 // This file is part of the Energi Core library. 4 // 5 // The Energi Core library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The Energi Core library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>. 17 18 package metrics 19 20 import "sync/atomic" 21 22 // Gauges hold an int64 value that can be set arbitrarily. 23 type Gauge interface { 24 Snapshot() Gauge 25 Update(int64) 26 Value() int64 27 } 28 29 // GetOrRegisterGauge returns an existing Gauge or constructs and registers a 30 // new StandardGauge. 31 func GetOrRegisterGauge(name string, r Registry) Gauge { 32 if nil == r { 33 r = DefaultRegistry 34 } 35 return r.GetOrRegister(name, NewGauge).(Gauge) 36 } 37 38 // NewGauge constructs a new StandardGauge. 39 func NewGauge() Gauge { 40 if !Enabled { 41 return NilGauge{} 42 } 43 return &StandardGauge{0} 44 } 45 46 // NewRegisteredGauge constructs and registers a new StandardGauge. 47 func NewRegisteredGauge(name string, r Registry) Gauge { 48 c := NewGauge() 49 if nil == r { 50 r = DefaultRegistry 51 } 52 r.Register(name, c) 53 return c 54 } 55 56 // NewFunctionalGauge constructs a new FunctionalGauge. 57 func NewFunctionalGauge(f func() int64) Gauge { 58 if !Enabled { 59 return NilGauge{} 60 } 61 return &FunctionalGauge{value: f} 62 } 63 64 // NewRegisteredFunctionalGauge constructs and registers a new StandardGauge. 65 func NewRegisteredFunctionalGauge(name string, r Registry, f func() int64) Gauge { 66 c := NewFunctionalGauge(f) 67 if nil == r { 68 r = DefaultRegistry 69 } 70 r.Register(name, c) 71 return c 72 } 73 74 // GaugeSnapshot is a read-only copy of another Gauge. 75 type GaugeSnapshot int64 76 77 // Snapshot returns the snapshot. 78 func (g GaugeSnapshot) Snapshot() Gauge { return g } 79 80 // Update panics. 81 func (GaugeSnapshot) Update(int64) { 82 panic("Update called on a GaugeSnapshot") 83 } 84 85 // Value returns the value at the time the snapshot was taken. 86 func (g GaugeSnapshot) Value() int64 { return int64(g) } 87 88 // NilGauge is a no-op Gauge. 89 type NilGauge struct{} 90 91 // Snapshot is a no-op. 92 func (NilGauge) Snapshot() Gauge { return NilGauge{} } 93 94 // Update is a no-op. 95 func (NilGauge) Update(v int64) {} 96 97 // Value is a no-op. 98 func (NilGauge) Value() int64 { return 0 } 99 100 // StandardGauge is the standard implementation of a Gauge and uses the 101 // sync/atomic package to manage a single int64 value. 102 type StandardGauge struct { 103 value int64 104 } 105 106 // Snapshot returns a read-only copy of the gauge. 107 func (g *StandardGauge) Snapshot() Gauge { 108 return GaugeSnapshot(g.Value()) 109 } 110 111 // Update updates the gauge's value. 112 func (g *StandardGauge) Update(v int64) { 113 atomic.StoreInt64(&g.value, v) 114 } 115 116 // Value returns the gauge's current value. 117 func (g *StandardGauge) Value() int64 { 118 return atomic.LoadInt64(&g.value) 119 } 120 121 // FunctionalGauge returns value from given function 122 type FunctionalGauge struct { 123 value func() int64 124 } 125 126 // Value returns the gauge's current value. 127 func (g FunctionalGauge) Value() int64 { 128 return g.value() 129 } 130 131 // Snapshot returns the snapshot. 132 func (g FunctionalGauge) Snapshot() Gauge { return GaugeSnapshot(g.Value()) } 133 134 // Update panics. 135 func (FunctionalGauge) Update(int64) { 136 panic("Update called on a FunctionalGauge") 137 }