-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
counter.go
58 lines (48 loc) · 1.42 KB
/
counter.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
package metrics
import "sync/atomic"
// Counter stores information about something "countable".
// Store
type Counter struct {
name string
desc string
value uint64
lastIntervalValue uint64
lastIntervalDelta uint64
}
// NewCounter creates new Counter.
func NewCounter(name string, desc string) *Counter {
return &Counter{name: name, desc: desc, value: 0}
}
// Name returns counter name
func (c *Counter) Name() string {
return c.name
}
// Desc returns counter description
func (c *Counter) Desc() string {
return c.desc
}
// Value allows to get raw counter value.
func (c *Counter) Value() uint64 {
return atomic.LoadUint64(&c.value)
}
// IntervalValue allows to get last interval value for counter.
func (c *Counter) IntervalValue() uint64 {
if c.lastIntervalValue == 0 {
return c.Value()
}
return atomic.LoadUint64(&c.lastIntervalDelta)
}
// Inc is equivalent to Add(name, 1)
func (c *Counter) Inc() uint64 {
return c.Add(1)
}
// Add adds the given number to the counter and returns the new value.
func (c *Counter) Add(n uint64) uint64 {
return atomic.AddUint64(&c.value, n)
}
// UpdateDelta updates the delta value for last interval based on current value and previous value.
func (c *Counter) UpdateDelta() {
now := atomic.LoadUint64(&c.value)
atomic.StoreUint64(&c.lastIntervalDelta, now-atomic.LoadUint64(&c.lastIntervalValue))
atomic.StoreUint64(&c.lastIntervalValue, now)
}