Debian Codegraph @

gauge_float64.go

raw symbols SCIP
1 package metrics
2
3 import "sync"
4
5 // GaugeFloat64s hold a float64 value that can be set arbitrarily.
6 type GaugeFloat64 interface {
7 Snapshot() GaugeFloat64
8 Update(float64)
9 Value() float64
10 }
11
12 // GetOrRegisterGaugeFloat64 returns an existing GaugeFloat64 or constructs and registers a
13 // new StandardGaugeFloat64.
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 // NewGaugeFloat64 constructs a new StandardGaugeFloat64.
22 func NewGaugeFloat64() GaugeFloat64 {
23 if UseNilMetrics {
24 return NilGaugeFloat64{}
25 }
26 return &StandardGaugeFloat64{
27 value: 0.0,
28 }
29 }
30
31 // NewRegisteredGaugeFloat64 constructs and registers a new StandardGaugeFloat64.
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 // GaugeFloat64Snapshot is a read-only copy of another GaugeFloat64.
42 type GaugeFloat64Snapshot float64
43
44 // Snapshot returns the snapshot.
45 func (g GaugeFloat64Snapshot) Snapshot() GaugeFloat64 { return g }
46
47 // Update panics.
48 func (GaugeFloat64Snapshot) Update(float64) {
49 panic("Update called on a GaugeFloat64Snapshot")
50 }
51
52 // Value returns the value at the time the snapshot was taken.
53 func (g GaugeFloat64Snapshot) Value() float64 { return float64(g) }
54
55 // NilGauge is a no-op Gauge.
56 type NilGaugeFloat64 struct{}
57
58 // Snapshot is a no-op.
59 func (NilGaugeFloat64) Snapshot() GaugeFloat64 { return NilGaugeFloat64{} }
60
61 // Update is a no-op.
62 func (NilGaugeFloat64) Update(v float64) {}
63
64 // Value is a no-op.
65 func (NilGaugeFloat64) Value() float64 { return 0.0 }
66
67 // StandardGaugeFloat64 is the standard implementation of a GaugeFloat64 and uses
68 // sync.Mutex to manage a single float64 value.
69 type StandardGaugeFloat64 struct {
70 mutex sync.Mutex
71 value float64
72 }
73
74 // Snapshot returns a read-only copy of the gauge.
75 func (g *StandardGaugeFloat64) Snapshot() GaugeFloat64 {
76 return GaugeFloat64Snapshot(g.Value())
77 }
78
79 // Update updates the gauge's value.
80 func (g *StandardGaugeFloat64) Update(v float64) {
81 g.mutex.Lock()
82 defer g.mutex.Unlock()
83 g.value = v
84 }
85
86 // Value returns the gauge's current value.
87 func (g *StandardGaugeFloat64) Value() float64 {
88 g.mutex.Lock()
89 defer g.mutex.Unlock()
90 return g.value
91 }