-
Notifications
You must be signed in to change notification settings - Fork 33
/
counter.go
128 lines (115 loc) · 3.21 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
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
// Copyright (C) 2015 Space Monkey, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package monkit
import (
"math"
"sync"
)
// Counter keeps track of running totals, along with the highest and lowest
// values seen. The overall value can increment or decrement. Counter
// implements StatSource. Should be constructed with NewCounter(), though it
// may be more convenient to use the Counter accessor on a given Scope.
// Expected creation is like:
//
// var mon = monkit.Package()
//
// func MyFunc() {
// mon.Counter("beans").Inc(1)
// }
type Counter struct {
mtx sync.Mutex
val, low, high int64
nonempty bool
key SeriesKey
}
// NewCounter constructs a counter
func NewCounter(key SeriesKey) *Counter {
return &Counter{key: key}
}
func (c *Counter) set(val int64) {
c.val = val
if !c.nonempty || val < c.low {
c.low = val
}
if !c.nonempty || c.high < val {
c.high = val
}
c.nonempty = true
}
// Set will immediately change the value of the counter to whatever val is. It
// will appropriately update the high and low values, and return the former
// value.
func (c *Counter) Set(val int64) (former int64) {
c.mtx.Lock()
former = c.val
c.set(val)
c.mtx.Unlock()
return former
}
// Inc will atomically increment the counter by delta and return the new value.
func (c *Counter) Inc(delta int64) (current int64) {
c.mtx.Lock()
c.set(c.val + delta)
current = c.val
c.mtx.Unlock()
return current
}
// Dec will atomically decrement the counter by delta and return the new value.
func (c *Counter) Dec(delta int64) (current int64) {
return c.Inc(-delta)
}
// High returns the highest value seen since construction or the last reset
func (c *Counter) High() (h int64) {
c.mtx.Lock()
h = c.high
c.mtx.Unlock()
return h
}
// Low returns the lowest value seen since construction or the last reset
func (c *Counter) Low() (l int64) {
c.mtx.Lock()
l = c.low
c.mtx.Unlock()
return l
}
// Current returns the current value
func (c *Counter) Current() (cur int64) {
c.mtx.Lock()
cur = c.val
c.mtx.Unlock()
return cur
}
// Reset resets all values including high/low counters and returns what they
// were.
func (c *Counter) Reset() (val, low, high int64) {
c.mtx.Lock()
val, low, high = c.val, c.low, c.high
c.val, c.low, c.high, c.nonempty = 0, 0, 0, false
c.mtx.Unlock()
return val, low, high
}
// Stats implements the StatSource interface
func (c *Counter) Stats(cb func(key SeriesKey, field string, val float64)) {
c.mtx.Lock()
val, low, high, nonempty := c.val, c.low, c.high, c.nonempty
c.mtx.Unlock()
if nonempty {
cb(c.key, "high", float64(high))
cb(c.key, "low", float64(low))
} else {
cb(c.key, "high", math.NaN())
cb(c.key, "low", math.NaN())
}
cb(c.key, "value", float64(val))
}