-
Notifications
You must be signed in to change notification settings - Fork 12
/
counter.go
71 lines (55 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
62
63
64
65
66
67
68
69
70
71
package rollup
import (
"sync"
"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
metricName string
rollupTags []string
totalsForInterval map[string]struct{}
totals map[string]int64
mu sync.Mutex
}
func NewCounterRollup(log *logger.Logger, nodeIndex, metricName string, rollupTags []string) *counterRollup {
return &counterRollup{
log: log,
nodeIndex: nodeIndex,
metricName: metricName,
rollupTags: rollupTags,
totalsForInterval: make(map[string]struct{}),
totals: make(map[string]int64),
}
}
func (r *counterRollup) Record(sourceId string, tags map[string]string, value int64) {
key := keyFromTags(r.rollupTags, sourceId, tags)
r.mu.Lock()
defer r.mu.Unlock()
r.totalsForInterval[key] = struct{}{}
r.totals[key] += value
}
func (r *counterRollup) Rollup(timestamp int64) []*PointsBatch {
var batches []*PointsBatch
r.mu.Lock()
defer r.mu.Unlock()
for k := range r.totalsForInterval {
labels, err := labelsFromKey(k, r.nodeIndex, r.rollupTags, r.log)
if err != nil {
continue
}
countPoint := &rpc.Point{
Name: r.metricName + "_total",
Timestamp: timestamp,
Value: float64(r.totals[k]),
Labels: labels,
}
batches = append(batches, &PointsBatch{
Points: []*rpc.Point{countPoint},
Size: countPoint.EstimatePointSize(),
})
}
r.totalsForInterval = make(map[string]struct{})
return batches
}