forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
138 lines (120 loc) · 2.68 KB
/
stats.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
package influxdb
import (
"fmt"
"sort"
"sync"
)
// Int representes a 64-bit signed integer which can be updated atomically.
type Int struct {
mu sync.RWMutex
i int64
}
// NewInt returns a new Int
func NewInt(v int64) *Int {
return &Int{i: v}
}
// Add atomically adds the given delta to the Int.
func (i *Int) Add(delta int64) {
i.mu.Lock()
defer i.mu.Unlock()
i.i += delta
}
// Stats represents a collection of metrics, as key-value pairs.
type Stats struct {
name string
m map[string]*Int
mu sync.RWMutex
}
// NewStats returns a Stats object with the given name.
func NewStats(name string) *Stats {
return &Stats{
name: name,
m: make(map[string]*Int),
}
}
// Add adds delta to the stat indiciated by key.
func (s *Stats) Add(key string, delta int64) {
s.mu.RLock()
i, ok := s.m[key]
s.mu.RUnlock()
if !ok {
// check again under the write lock
s.mu.Lock()
i, ok = s.m[key]
if !ok {
i = new(Int)
s.m[key] = i
}
s.mu.Unlock()
}
i.Add(delta)
}
// Inc simply increments the given key by 1.
func (s *Stats) Inc(key string) {
s.Add(key, 1)
}
// Get returns a value for a given key.
func (s *Stats) Get(key string) int64 {
s.mu.RLock()
defer s.mu.RUnlock()
return s.m[key].i
}
// Set sets a value for the given key.
func (s *Stats) Set(key string, v int64) {
s.mu.Lock()
defer s.mu.Unlock()
s.m[key] = NewInt(v)
}
// Name returns the name of the Stats object.
func (s *Stats) Name() string {
return s.name
}
// Walk calls f for each entry in the stats. The stats are locked
// during the walk but existing entries may be concurrently updated.
func (s *Stats) Walk(f func(string, int64)) {
s.mu.RLock()
defer s.mu.RUnlock()
for k, v := range s.m {
f(k, v.i)
}
}
// Diff returns the difference between two sets of stats. The result is undefined
// if the two Stats objects do not contain the same keys.
func (s *Stats) Diff(other *Stats) *Stats {
diff := NewStats(s.name)
s.Walk(func(k string, v int64) {
diff.Set(k, v-other.Get(k))
})
return diff
}
// Snapshot returns a copy of the stats object. Addition and removal of stats keys
// is blocked during the created of the snapshot, but existing entries may be
// concurrently updated.
func (s *Stats) Snapshot() *Stats {
snap := NewStats(s.name)
s.Walk(func(k string, v int64) {
snap.Set(k, s.m[k].i)
})
return snap
}
func (s *Stats) String() string {
var out string
stat := s.Snapshot()
var keys []string
for k, _ := range stat.m {
keys = append(keys, k)
}
sort.Strings(keys)
out += `{"` + stat.name + `":[`
var j int
for _, k := range keys {
v := stat.m[k].i
out += `{"` + k + `":` + fmt.Sprintf("%d", v) + `}`
j++
if j != len(keys) {
out += `,`
}
}
out += `]}`
return out
}