-
Notifications
You must be signed in to change notification settings - Fork 0
/
timing_counter.go
65 lines (52 loc) · 1.33 KB
/
timing_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
package rolling
import (
"fmt"
"time"
)
var _ Metric = &rollingCounter{}
var _ Aggregation = &rollingCounter{}
type TimingCounter interface {
Metric
Aggregation
// Reduce applies the reduction function to all buckets within the window.
Reduce(func(Iterator) float64) float64
}
// TimingCounterOpts contains the arguments for creating TimingCounter.
type TimingCounterOpts struct {
Size int
BucketDuration time.Duration
}
type timingCounter struct {
policy *TimingPolicy
}
func NewTimingCounter(opts TimingCounterOpts) TimingCounter {
window := NewWindow(WindowOpts{Size: opts.Size})
policy := NewTimingPolicy(window, TimingPolicyOpts{BucketDuration: opts.BucketDuration})
return &timingCounter{
policy: policy,
}
}
func (t *timingCounter) Add(val int64) {
if val < 0 {
panic(fmt.Errorf("rolling: cannot decrease in value. val: %d", val))
}
t.policy.Add(float64(val))
}
func (t *timingCounter) Reduce(f func(Iterator) float64) float64 {
return t.policy.Reduce(f)
}
func (t *timingCounter) Avg() float64 {
return t.policy.Reduce(Avg)
}
func (t *timingCounter) Min() float64 {
return t.policy.Reduce(Min)
}
func (t *timingCounter) Max() float64 {
return t.policy.Reduce(Max)
}
func (t *timingCounter) Sum() float64 {
return t.policy.Reduce(Sum)
}
func (t *timingCounter) Value() int64 {
return int64(t.Sum())
}