...
1 package metrics
2
3 import "sync/atomic"
4
5
6 type Gauge interface {
7 Snapshot() Gauge
8 Update(int64)
9 Value() int64
10 }
11
12
13
14 func GetOrRegisterGauge(name string, r Registry) Gauge {
15 if nil == r {
16 r = DefaultRegistry
17 }
18 return r.GetOrRegister(name, NewGauge).(Gauge)
19 }
20
21
22 func NewGauge() Gauge {
23 if UseNilMetrics {
24 return NilGauge{}
25 }
26 return &StandardGauge{0}
27 }
28
29
30 func NewRegisteredGauge(name string, r Registry) Gauge {
31 c := NewGauge()
32 if nil == r {
33 r = DefaultRegistry
34 }
35 r.Register(name, c)
36 return c
37 }
38
39
40 func NewFunctionalGauge(f func() int64) Gauge {
41 if UseNilMetrics {
42 return NilGauge{}
43 }
44 return &FunctionalGauge{value: f}
45 }
46
47
48 func NewRegisteredFunctionalGauge(name string, r Registry, f func() int64) Gauge {
49 c := NewFunctionalGauge(f)
50 if nil == r {
51 r = DefaultRegistry
52 }
53 r.Register(name, c)
54 return c
55 }
56
57
58 type GaugeSnapshot int64
59
60
61 func (g GaugeSnapshot) Snapshot() Gauge { return g }
62
63
64 func (GaugeSnapshot) Update(int64) {
65 panic("Update called on a GaugeSnapshot")
66 }
67
68
69 func (g GaugeSnapshot) Value() int64 { return int64(g) }
70
71
72 type NilGauge struct{}
73
74
75 func (NilGauge) Snapshot() Gauge { return NilGauge{} }
76
77
78 func (NilGauge) Update(v int64) {}
79
80
81 func (NilGauge) Value() int64 { return 0 }
82
83
84
85 type StandardGauge struct {
86 value int64
87 }
88
89
90 func (g *StandardGauge) Snapshot() Gauge {
91 return GaugeSnapshot(g.Value())
92 }
93
94
95 func (g *StandardGauge) Update(v int64) {
96 atomic.StoreInt64(&g.value, v)
97 }
98
99
100 func (g *StandardGauge) Value() int64 {
101 return atomic.LoadInt64(&g.value)
102 }
103
104
105 type FunctionalGauge struct {
106 value func() int64
107 }
108
109
110 func (g FunctionalGauge) Value() int64 {
111 return g.value()
112 }
113
114
115 func (g FunctionalGauge) Snapshot() Gauge { return GaugeSnapshot(g.Value()) }
116
117
118 func (FunctionalGauge) Update(int64) {
119 panic("Update called on a FunctionalGauge")
120 }
121
View as plain text