/
gauge_u.go
174 lines (144 loc) · 4.19 KB
/
gauge_u.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package metrics
import "sync/atomic"
// UGauges hold an int64 value that can be set arbitrarily.
//
// Graphite naming scheme
//
// Plain: {PREFIX}.{NAME}
//
// Tagged: {TAG_PREFIX}.{NAME}
type UGauge interface {
Snapshot() UGauge
Clear() uint64
Update(uint64)
Value() uint64
}
// GetOrRegisterUGauge returns an existing UGauge or constructs and registers a
// new StandardUGauge.
func GetOrRegisterUGauge(name string, r Registry) UGauge {
if nil == r {
r = DefaultRegistry
}
return r.GetOrRegister(name, NewUGauge).(UGauge)
}
// GetOrRegisterUGaugeT returns an existing UGauge or constructs and registers a
// new StandardUGauge.
func GetOrRegisterUGaugeT(name string, tagsMap map[string]string, r Registry) UGauge {
if nil == r {
r = DefaultRegistry
}
return r.GetOrRegisterT(name, tagsMap, NewUGauge).(UGauge)
}
// NewUGauge constructs a new StandardUGauge.
func NewUGauge() UGauge {
if UseNilMetrics {
return NilUGauge{}
}
return &StandardUGauge{0}
}
// NewRegisteredUGauge constructs and registers a new StandardUGauge.
func NewRegisteredUGauge(name string, r Registry) UGauge {
c := NewUGauge()
if nil == r {
r = DefaultRegistry
}
r.Register(name, c)
return c
}
// NewRegisteredUGaugeT constructs and registers a new StandardUGauge.
func NewRegisteredUGaugeT(name string, tagsMap map[string]string, r Registry) UGauge {
c := NewUGauge()
if nil == r {
r = DefaultRegistry
}
r.RegisterT(name, tagsMap, c)
return c
}
// NewFunctionalUGauge constructs a new FunctionalUGauge.
func NewFunctionalUGauge(f func() uint64) UGauge {
if UseNilMetrics {
return NilUGauge{}
}
return &FunctionalUGauge{value: f}
}
// NewRegisteredFunctionalUGauge constructs and registers a new StandardUGauge.
func NewRegisteredFunctionalUGauge(name string, r Registry, f func() uint64) UGauge {
c := NewFunctionalUGauge(f)
if nil == r {
r = DefaultRegistry
}
r.Register(name, c)
return c
}
// NewRegisteredFunctionalUGaugeT constructs and registers a new StandardUGauge.
func NewRegisteredFunctionalUGaugeT(name string, tagsMap map[string]string, r Registry, f func() uint64) UGauge {
c := NewFunctionalUGauge(f)
if nil == r {
r = DefaultRegistry
}
r.RegisterT(name, tagsMap, c)
return c
}
// UGaugeSnapshot is a read-only copy of another UGauge.
type UGaugeSnapshot uint64
// Snapshot returns the snapshot.
func (g UGaugeSnapshot) Snapshot() UGauge { return g }
// Clear panics.
func (g UGaugeSnapshot) Clear() uint64 {
panic("Clear called on a UGaugeSnapshot")
}
// Update panics.
func (UGaugeSnapshot) Update(uint64) {
panic("Update called on a UGaugeSnapshot")
}
// Value returns the value at the time the snapshot was taken.
func (g UGaugeSnapshot) Value() uint64 { return uint64(g) }
// NilUGauge is a no-op UGauge.
type NilUGauge struct{}
// Snapshot is a no-op.
func (NilUGauge) Snapshot() UGauge { return NilUGauge{} }
// Clear is a no-op.
func (NilUGauge) Clear() uint64 { return 0 }
// Update is a no-op.
func (NilUGauge) Update(v uint64) {}
// Value is a no-op.
func (NilUGauge) Value() uint64 { return 0 }
// StandardUGauge is the standard implementation of a UGauge and uses the
// sync/atomic package to manage a single int64 value.
type StandardUGauge struct {
value uint64
}
// Snapshot returns a read-only copy of the gauge.
func (g *StandardUGauge) Snapshot() UGauge {
return UGaugeSnapshot(g.Value())
}
// Clear sets the DownCounter to zero.
func (g *StandardUGauge) Clear() uint64 {
return atomic.SwapUint64(&g.value, 0)
}
// Update updates the gauge's value.
func (g *StandardUGauge) Update(v uint64) {
atomic.StoreUint64(&g.value, v)
}
// Value returns the gauge's current value.
func (g *StandardUGauge) Value() uint64 {
return atomic.LoadUint64(&g.value)
}
// FunctionalUGauge returns value from given function
type FunctionalUGauge struct {
value func() uint64
}
// Value returns the gauge's current value.
func (g FunctionalUGauge) Value() uint64 {
return g.value()
}
// Snapshot returns the snapshot.
func (g FunctionalUGauge) Snapshot() UGauge { return UGaugeSnapshot(g.Value()) }
// Update panics.
func (FunctionalUGauge) Update(uint64) {
panic("Update called on a FunctionalUGauge")
}
// Clear panics.
func (g FunctionalUGauge) Clear() uint64 {
panic("Clear called on a FunctionalUGauge")
}