-
Notifications
You must be signed in to change notification settings - Fork 12
/
counter.go
90 lines (70 loc) · 1.88 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
package rollup
import (
"sync"
"time"
"github.com/cloudfoundry/metric-store-release/src/pkg/logger"
"github.com/cloudfoundry/metric-store-release/src/pkg/rpc"
)
type counterRollup struct {
log *logger.Logger
nodeIndex string
rollupTags []string
counters map[string]int64
lastSeen map[string]int64
expiration time.Duration
mu sync.Mutex
}
func NewCounterRollup(log *logger.Logger, nodeIndex string, rollupTags []string, opts ...CounterRollupOption) *counterRollup {
counterRollup := &counterRollup{
log: log,
nodeIndex: nodeIndex,
rollupTags: rollupTags,
counters: make(map[string]int64),
lastSeen: make(map[string]int64),
expiration: EXPIRATION,
}
for _, o := range opts {
o(counterRollup)
}
return counterRollup
}
type CounterRollupOption func(*counterRollup)
func WithCounterRollupExpiration(expiration time.Duration) CounterRollupOption {
return func(rollup *counterRollup) {
rollup.expiration = expiration
}
}
func (r *counterRollup) Record(timestamp int64, sourceId string, tags map[string]string, value int64) {
key := keyFromTags(r.rollupTags, sourceId, tags)
r.mu.Lock()
defer r.mu.Unlock()
r.lastSeen[key] = timestamp
r.counters[key] += value
}
func (r *counterRollup) Rollup(timestamp int64) []*PointsBatch {
var batches []*PointsBatch
r.mu.Lock()
defer r.mu.Unlock()
for k := range r.counters {
if expired(r.lastSeen[k], timestamp, r.expiration) {
delete(r.lastSeen, k)
delete(r.counters, k)
continue
}
labels, err := labelsFromKey(k, r.nodeIndex, r.rollupTags, r.log)
if err != nil {
continue
}
countPoint := &rpc.Point{
Name: GorouterHttpMetricName + "_total",
Timestamp: timestamp,
Value: float64(r.counters[k]),
Labels: labels,
}
batches = append(batches, &PointsBatch{
Points: []*rpc.Point{countPoint},
Size: countPoint.EstimatePointSize(),
})
}
return batches
}