forked from Netflix/spectator-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.go
61 lines (53 loc) · 1.57 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
package spectator
// Counter is used to measure the rate at which some event is occurring. This
// type is safe for concurrent use.
//
// You can find more about this type by viewing the relevant Java Spectator
// documentation here:
//
// https://netflix.github.io/spectator/en/latest/intro/counter/
type Counter struct {
count uint64
// Pointers need to be after counters to ensure 64-bit alignment. See
// note in atomicnum.go
id *Id
}
// NewCounter generates a new counter, using the provided meter identifier.
func NewCounter(id *Id) *Counter {
return &Counter{0, id}
}
// MeterId returns the meter identifier.
func (c *Counter) MeterId() *Id {
return c.id
}
// Measure returns the list of measurements known by the counter. This will
// either contain one item, the last value, or no items. This also resets the
// internal counter to 0.0.
func (c *Counter) Measure() []Measurement {
cnt := swapFloat64(&c.count, 0.0)
if cnt > 0 {
return []Measurement{{c.id.WithDefaultStat("count"), cnt}}
} else {
return []Measurement{}
}
}
// Increment increments the counter.
func (c *Counter) Increment() {
addFloat64(&c.count, 1)
}
// AddFloat adds a specific float64 delta to the current measurement.
func (c *Counter) AddFloat(delta float64) {
if delta > 0.0 {
addFloat64(&c.count, delta)
}
}
// Add is to add a specific int64 delta to the current measurement.
func (c *Counter) Add(delta int64) {
if delta > 0 {
addFloat64(&c.count, float64(delta))
}
}
// Count returns the current value for the counter.
func (c *Counter) Count() float64 {
return loadFloat64(&c.count)
}