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