-
Notifications
You must be signed in to change notification settings - Fork 0
/
hist_impl.go
88 lines (77 loc) · 1.93 KB
/
hist_impl.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
package metric
// Histogram is a libary for recording data distribution.
import (
"sort"
"sync"
"time"
)
// NewHistogram accepts StatsName, histogram parameter min, max slice, and export flag
func NewHistogram(windowDur, bucketDur time.Duration) (Histogram, error) {
if err := check(windowDur, bucketDur); err != nil {
return nil, err
}
return &histImpl{
bucketDur: bucketDur,
windowDur: windowDur,
binMap: map[int]*simpleCounter{},
bins: []int{},
bound: &exponential{},
}, nil
}
type binVal struct {
bin int
count int64
}
type histImpl struct {
bucketDur time.Duration
windowDur time.Duration
bound binBound // binBound manage bucket range and size
binMap map[int]*simpleCounter // binMap maps bin to edc
bins []int // bins stored all used bin ids
sync.RWMutex // rwMutext protects binMap and bins
}
// Update incr the corresponding bin in the histogram
func (h *histImpl) Update(value float64) {
idx := h.bound.Bin(value)
h.Lock()
defer h.Unlock()
b, ok := h.binMap[idx]
if ok {
b.incr()
return
}
b = newSimpleCounter(h.windowDur, h.bucketDur)
h.binMap[idx] = b
// TODO: better data-struct
h.bins = append(h.bins, idx)
sort.Ints(h.bins)
b.incr()
}
func (h *histImpl) Snapshot() HistSnapshot {
return &histSnapshot{
bound: h.bound,
bins: h.values(),
}
}
func (h *histImpl) values() []binVal {
h.RLock()
defer h.RUnlock()
values := make([]binVal, 0, len(h.bins))
for _, i := range h.bins {
values = append(values, binVal{
bin: i,
count: h.binMap[i].get(),
})
}
return values
}
type binBound interface {
// ValueRange returns min and max value
ValueRange() (float64, float64)
// BinRange returns min and max bin ID
BinRange() (int, int)
// Bin returns bin ID of the given value
Bin(v float64) int
// Bound returns the upper and lower bound of given bin index
Bound(bin int) (float64, float64)
}