2015-03-12 22:22:48 +00:00
|
|
|
package influxdb
|
|
|
|
|
|
|
|
import (
|
2015-04-22 21:37:59 +00:00
|
|
|
"fmt"
|
2015-04-22 22:03:37 +00:00
|
|
|
"sort"
|
2015-03-12 22:22:48 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2015-03-20 15:15:42 +00:00
|
|
|
// Int representes a 64-bit signed integer which can be updated atomically.
|
2015-03-12 22:22:48 +00:00
|
|
|
type Int struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
i int64
|
|
|
|
}
|
|
|
|
|
2015-03-13 23:17:22 +00:00
|
|
|
// NewInt returns a new Int
|
2015-03-12 22:22:48 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-13 23:17:22 +00:00
|
|
|
// Stats represents a collection of metrics, as key-value pairs.
|
2015-03-12 22:22:48 +00:00
|
|
|
type Stats struct {
|
2015-03-13 21:14:23 +00:00
|
|
|
name string
|
|
|
|
m map[string]*Int
|
|
|
|
mu sync.RWMutex
|
2015-03-12 22:22:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 23:17:22 +00:00
|
|
|
// NewStats returns a Stats object with the given name.
|
2015-03-13 21:14:23 +00:00
|
|
|
func NewStats(name string) *Stats {
|
2015-03-12 22:22:48 +00:00
|
|
|
return &Stats{
|
2015-03-13 21:14:23 +00:00
|
|
|
name: name,
|
|
|
|
m: make(map[string]*Int),
|
2015-03-12 22:22:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2015-03-13 05:16:30 +00:00
|
|
|
// Inc simply increments the given key by 1.
|
|
|
|
func (s *Stats) Inc(key string) {
|
|
|
|
s.Add(key, 1)
|
|
|
|
}
|
|
|
|
|
2015-03-13 23:17:22 +00:00
|
|
|
// Get returns a value for a given key.
|
2015-03-12 22:22:48 +00:00
|
|
|
func (s *Stats) Get(key string) int64 {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.m[key].i
|
|
|
|
}
|
|
|
|
|
2015-03-13 23:17:22 +00:00
|
|
|
// Set sets a value for the given key.
|
2015-03-12 22:22:48 +00:00
|
|
|
func (s *Stats) Set(key string, v int64) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
s.m[key] = NewInt(v)
|
|
|
|
}
|
|
|
|
|
2015-03-13 23:17:22 +00:00
|
|
|
// Name returns the name of the Stats object.
|
2015-03-13 21:14:23 +00:00
|
|
|
func (s *Stats) Name() string {
|
|
|
|
return s.name
|
|
|
|
}
|
|
|
|
|
2015-03-12 22:22:48 +00:00
|
|
|
// 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 {
|
2015-03-13 21:14:23 +00:00
|
|
|
diff := NewStats(s.name)
|
2015-03-12 22:22:48 +00:00
|
|
|
s.Walk(func(k string, v int64) {
|
|
|
|
diff.Set(k, v-other.Get(k))
|
|
|
|
})
|
|
|
|
return diff
|
|
|
|
}
|
2015-03-13 22:01:53 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2015-04-22 21:37:59 +00:00
|
|
|
|
|
|
|
func (s *Stats) String() string {
|
|
|
|
var out string
|
|
|
|
stat := s.Snapshot()
|
2015-04-22 22:03:37 +00:00
|
|
|
var keys []string
|
|
|
|
for k, _ := range stat.m {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
2015-04-22 21:37:59 +00:00
|
|
|
out += `{"` + stat.name + `":[`
|
|
|
|
var j int
|
2015-04-22 22:03:37 +00:00
|
|
|
for _, k := range keys {
|
|
|
|
v := stat.m[k].i
|
|
|
|
out += `{"` + k + `":` + fmt.Sprintf("%d", v) + `}`
|
2015-04-22 21:37:59 +00:00
|
|
|
j++
|
2015-04-22 22:03:37 +00:00
|
|
|
if j != len(keys) {
|
2015-04-22 21:37:59 +00:00
|
|
|
out += `,`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out += `]}`
|
|
|
|
return out
|
|
|
|
}
|