forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expvar.go
329 lines (281 loc) · 6.13 KB
/
expvar.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// This package is a fork of the golang expvar expvar.Var types.
// Adding extra support for deleting and accessing raw typed values.
package expvar
import (
"bytes"
"expvar"
"fmt"
"math"
"sort"
"strconv"
"sync"
"sync/atomic"
"github.com/influxdata/kapacitor/uuid"
)
type IntVar interface {
expvar.Var
IntValue() int64
}
type FloatVar interface {
expvar.Var
FloatValue() float64
}
type StringVar interface {
expvar.Var
StringValue() string
}
// Int is a 64-bit integer variable that satisfies the expvar.Var interface.
type Int struct {
i int64
}
func (v *Int) String() string {
return strconv.FormatInt(v.IntValue(), 10)
}
func (v *Int) Add(delta int64) {
atomic.AddInt64(&v.i, delta)
}
func (v *Int) Set(value int64) {
atomic.StoreInt64(&v.i, value)
}
func (v *Int) IntValue() int64 {
return atomic.LoadInt64(&v.i)
}
// IntFuncGauge is a 64-bit integer variable that satisfies the expvar.Var interface.
type IntFuncGauge struct {
ValueF func() int64
}
func (v *IntFuncGauge) String() string {
return strconv.FormatInt(v.IntValue(), 10)
}
func (v *IntFuncGauge) Add(delta int64) {}
func (v *IntFuncGauge) Set(value int64) {}
func (v *IntFuncGauge) IntValue() int64 {
if v == nil || v.ValueF == nil {
return 0
}
return v.ValueF()
}
func NewIntFuncGauge(fn func() int64) *IntFuncGauge {
return &IntFuncGauge{fn}
}
// IntSum is a 64-bit integer variable that consists of multiple different parts
// and satisfies the expvar.Var interface.
// The value of the var is the sum of all its parts.
// The part names are opaque and are simply used to identfy each part.
type IntSum struct {
mu sync.Mutex
parts map[string]int64
sum int64
}
func NewIntSum() *IntSum {
return &IntSum{
parts: make(map[string]int64),
}
}
func (v *IntSum) String() string {
return strconv.FormatInt(v.IntValue(), 10)
}
func (v *IntSum) Add(part string, delta int64) {
v.mu.Lock()
v.parts[part] += delta
v.sum += delta
v.mu.Unlock()
}
func (v *IntSum) Set(part string, value int64) {
v.mu.Lock()
old := v.parts[part]
delta := value - old
v.parts[part] = value
v.sum += delta
v.mu.Unlock()
}
func (v *IntSum) IntValue() int64 {
v.mu.Lock()
s := v.sum
v.mu.Unlock()
return s
}
// Float is a 64-bit float variable that satisfies the expvar.Var interface.
type Float struct {
f uint64
}
func (v *Float) String() string {
return strconv.FormatFloat(v.FloatValue(), 'g', -1, 64)
}
func (v *Float) FloatValue() float64 {
return math.Float64frombits(atomic.LoadUint64(&v.f))
}
// Add adds delta to v.
func (v *Float) Add(delta float64) {
for {
cur := atomic.LoadUint64(&v.f)
curVal := math.Float64frombits(cur)
nxtVal := curVal + delta
nxt := math.Float64bits(nxtVal)
if atomic.CompareAndSwapUint64(&v.f, cur, nxt) {
return
}
}
}
// Set sets v to value.
func (v *Float) Set(value float64) {
atomic.StoreUint64(&v.f, math.Float64bits(value))
}
// Map is a string-to-expvar.Var map variable that satisfies the expvar.Var interface.
type Map struct {
mu sync.RWMutex
m map[string]expvar.Var
}
func (v *Map) String() string {
v.mu.RLock()
defer v.mu.RUnlock()
var b bytes.Buffer
fmt.Fprintf(&b, "{")
first := true
v.doLocked(func(kv expvar.KeyValue) {
if !first {
fmt.Fprintf(&b, ", ")
}
fmt.Fprintf(&b, "%q: %v", kv.Key, kv.Value)
first = false
})
fmt.Fprintf(&b, "}")
return b.String()
}
func (v *Map) Init() *Map {
v.m = make(map[string]expvar.Var)
return v
}
func (v *Map) Get(key string) expvar.Var {
v.mu.RLock()
defer v.mu.RUnlock()
return v.m[key]
}
func (v *Map) Set(key string, av expvar.Var) {
v.mu.Lock()
defer v.mu.Unlock()
v.m[key] = av
}
func (v *Map) Delete(key string) {
v.mu.Lock()
defer v.mu.Unlock()
delete(v.m, key)
}
func (v *Map) Add(key string, delta int64) {
v.mu.RLock()
av, ok := v.m[key]
v.mu.RUnlock()
if !ok {
// check again under the write lock
v.mu.Lock()
av, ok = v.m[key]
if !ok {
av = new(Int)
v.m[key] = av
}
v.mu.Unlock()
}
// Add to Int; ignore otherwise.
if iv, ok := av.(*Int); ok {
iv.Add(delta)
}
}
// AddFloat adds delta to the *Float value stored under the given map key.
func (v *Map) AddFloat(key string, delta float64) {
v.mu.RLock()
av, ok := v.m[key]
v.mu.RUnlock()
if !ok {
// check again under the write lock
v.mu.Lock()
av, ok = v.m[key]
if !ok {
av = new(Float)
v.m[key] = av
}
v.mu.Unlock()
}
// Add to Float; ignore otherwise.
if iv, ok := av.(*Float); ok {
iv.Add(delta)
}
}
// Do calls f for each entry in the map.
// The map is locked during the iteration,
// but existing entries may be concurrently updated.
func (v *Map) Do(f func(expvar.KeyValue)) {
v.mu.RLock()
defer v.mu.RUnlock()
v.doLocked(f)
}
// DoSorted calls f for each entry in the map in sorted order.
// The map is locked during the iteration,
// but existing entries may be concurrently updated.
func (v *Map) DoSorted(f func(expvar.KeyValue)) {
v.mu.RLock()
defer v.mu.RUnlock()
keys := make([]string, len(v.m))
i := 0
for key := range v.m {
keys[i] = key
i++
}
sort.Strings(keys)
for _, k := range keys {
f(expvar.KeyValue{k, v.m[k]})
}
}
// doLocked calls f for each entry in the map.
// v.mu must be held for reads.
func (v *Map) doLocked(f func(expvar.KeyValue)) {
for k, v := range v.m {
f(expvar.KeyValue{k, v})
}
}
// String is a string variable, and satisfies the expvar.Var interface.
type String struct {
mu sync.RWMutex
s string
}
func (v *String) String() string {
v.mu.RLock()
defer v.mu.RUnlock()
return strconv.Quote(v.s)
}
func (v *String) Set(value string) {
v.mu.Lock()
defer v.mu.Unlock()
v.s = value
}
func (v *String) StringValue() string {
v.mu.RLock()
defer v.mu.RUnlock()
return v.s
}
// UUID is a string variable that contain an UUID and satisfies the expvar.Var interface.
type UUID struct {
mu sync.RWMutex
id uuid.UUID
s string
}
func (v *UUID) String() string {
v.mu.RLock()
defer v.mu.RUnlock()
return strconv.Quote(v.s)
}
func (v *UUID) Set(value uuid.UUID) {
v.mu.Lock()
defer v.mu.Unlock()
v.id = value
v.s = value.String()
}
func (v *UUID) StringValue() string {
v.mu.RLock()
defer v.mu.RUnlock()
return v.s
}
func (v *UUID) UUIDValue() uuid.UUID {
v.mu.RLock()
defer v.mu.RUnlock()
return v.id
}