|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | +) |
| 6 | + |
| 7 | +// CounterFloat64 holds a float64 value that can be incremented and decremented. |
| 8 | +type CounterFloat64 interface { |
| 9 | + Clear() |
| 10 | + Count() float64 |
| 11 | + Dec(float64) |
| 12 | + Inc(float64) |
| 13 | + Snapshot() CounterFloat64 |
| 14 | +} |
| 15 | + |
| 16 | +// GetOrRegisterCounterFloat64 returns an existing CounterFloat64 or constructs and registers |
| 17 | +// a new StandardCounterFloat64. |
| 18 | +func GetOrRegisterCounterFloat64(name string, r Registry) CounterFloat64 { |
| 19 | + if nil == r { |
| 20 | + r = DefaultRegistry |
| 21 | + } |
| 22 | + return r.GetOrRegister(name, NewCounterFloat64).(CounterFloat64) |
| 23 | +} |
| 24 | + |
| 25 | +// GetOrRegisterCounterFloat64Forced returns an existing CounterFloat64 or constructs and registers a |
| 26 | +// new CounterFloat64 no matter the global switch is enabled or not. |
| 27 | +// Be sure to unregister the counter from the registry once it is of no use to |
| 28 | +// allow for garbage collection. |
| 29 | +func GetOrRegisterCounterFloat64Forced(name string, r Registry) CounterFloat64 { |
| 30 | + if nil == r { |
| 31 | + r = DefaultRegistry |
| 32 | + } |
| 33 | + return r.GetOrRegister(name, NewCounterFloat64Forced).(CounterFloat64) |
| 34 | +} |
| 35 | + |
| 36 | +// NewCounterFloat64 constructs a new StandardCounterFloat64. |
| 37 | +func NewCounterFloat64() CounterFloat64 { |
| 38 | + if !Enabled { |
| 39 | + return NilCounterFloat64{} |
| 40 | + } |
| 41 | + return &StandardCounterFloat64{count: 0.0} |
| 42 | +} |
| 43 | + |
| 44 | +// NewCounterFloat64Forced constructs a new StandardCounterFloat64 and returns it no matter if |
| 45 | +// the global switch is enabled or not. |
| 46 | +func NewCounterFloat64Forced() CounterFloat64 { |
| 47 | + return &StandardCounterFloat64{count: 0.0} |
| 48 | +} |
| 49 | + |
| 50 | +// NewRegisteredCounterFloat64 constructs and registers a new StandardCounterFloat64. |
| 51 | +func NewRegisteredCounterFloat64(name string, r Registry) CounterFloat64 { |
| 52 | + c := NewCounterFloat64() |
| 53 | + if nil == r { |
| 54 | + r = DefaultRegistry |
| 55 | + } |
| 56 | + r.Register(name, c) |
| 57 | + return c |
| 58 | +} |
| 59 | + |
| 60 | +// NewRegisteredCounterFloat64Forced constructs and registers a new StandardCounterFloat64 |
| 61 | +// and launches a goroutine no matter the global switch is enabled or not. |
| 62 | +// Be sure to unregister the counter from the registry once it is of no use to |
| 63 | +// allow for garbage collection. |
| 64 | +func NewRegisteredCounterFloat64Forced(name string, r Registry) CounterFloat64 { |
| 65 | + c := NewCounterFloat64Forced() |
| 66 | + if nil == r { |
| 67 | + r = DefaultRegistry |
| 68 | + } |
| 69 | + r.Register(name, c) |
| 70 | + return c |
| 71 | +} |
| 72 | + |
| 73 | +// CounterFloat64Snapshot is a read-only copy of another CounterFloat64. |
| 74 | +type CounterFloat64Snapshot float64 |
| 75 | + |
| 76 | +// Clear panics. |
| 77 | +func (CounterFloat64Snapshot) Clear() { |
| 78 | + panic("Clear called on a CounterFloat64Snapshot") |
| 79 | +} |
| 80 | + |
| 81 | +// Count returns the value at the time the snapshot was taken. |
| 82 | +func (c CounterFloat64Snapshot) Count() float64 { return float64(c) } |
| 83 | + |
| 84 | +// Dec panics. |
| 85 | +func (CounterFloat64Snapshot) Dec(float64) { |
| 86 | + panic("Dec called on a CounterFloat64Snapshot") |
| 87 | +} |
| 88 | + |
| 89 | +// Inc panics. |
| 90 | +func (CounterFloat64Snapshot) Inc(float64) { |
| 91 | + panic("Inc called on a CounterFloat64Snapshot") |
| 92 | +} |
| 93 | + |
| 94 | +// Snapshot returns the snapshot. |
| 95 | +func (c CounterFloat64Snapshot) Snapshot() CounterFloat64 { return c } |
| 96 | + |
| 97 | +// NilCounterFloat64 is a no-op CounterFloat64. |
| 98 | +type NilCounterFloat64 struct{} |
| 99 | + |
| 100 | +// Clear is a no-op. |
| 101 | +func (NilCounterFloat64) Clear() {} |
| 102 | + |
| 103 | +// Count is a no-op. |
| 104 | +func (NilCounterFloat64) Count() float64 { return 0.0 } |
| 105 | + |
| 106 | +// Dec is a no-op. |
| 107 | +func (NilCounterFloat64) Dec(i float64) {} |
| 108 | + |
| 109 | +// Inc is a no-op. |
| 110 | +func (NilCounterFloat64) Inc(i float64) {} |
| 111 | + |
| 112 | +// Snapshot is a no-op. |
| 113 | +func (NilCounterFloat64) Snapshot() CounterFloat64 { return NilCounterFloat64{} } |
| 114 | + |
| 115 | +// StandardCounterFloat64 is the standard implementation of a CounterFloat64 and uses the |
| 116 | +// sync.Mutex package to manage a single float64 value. |
| 117 | +type StandardCounterFloat64 struct { |
| 118 | + mutex sync.Mutex |
| 119 | + count float64 |
| 120 | +} |
| 121 | + |
| 122 | +// Clear sets the counter to zero. |
| 123 | +func (c *StandardCounterFloat64) Clear() { |
| 124 | + c.mutex.Lock() |
| 125 | + defer c.mutex.Unlock() |
| 126 | + c.count = 0.0 |
| 127 | +} |
| 128 | + |
| 129 | +// Count returns the current value. |
| 130 | +func (c *StandardCounterFloat64) Count() float64 { |
| 131 | + c.mutex.Lock() |
| 132 | + defer c.mutex.Unlock() |
| 133 | + return c.count |
| 134 | +} |
| 135 | + |
| 136 | +// Dec decrements the counter by the given amount. |
| 137 | +func (c *StandardCounterFloat64) Dec(v float64) { |
| 138 | + c.mutex.Lock() |
| 139 | + defer c.mutex.Unlock() |
| 140 | + c.count -= v |
| 141 | +} |
| 142 | + |
| 143 | +// Inc increments the counter by the given amount. |
| 144 | +func (c *StandardCounterFloat64) Inc(v float64) { |
| 145 | + c.mutex.Lock() |
| 146 | + defer c.mutex.Unlock() |
| 147 | + c.count += v |
| 148 | +} |
| 149 | + |
| 150 | +// Snapshot returns a read-only copy of the counter. |
| 151 | +func (c *StandardCounterFloat64) Snapshot() CounterFloat64 { |
| 152 | + return CounterFloat64Snapshot(c.Count()) |
| 153 | +} |
0 commit comments