-
Notifications
You must be signed in to change notification settings - Fork 208
/
accumulator.go
55 lines (44 loc) · 1.41 KB
/
accumulator.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
package metrics
// Accumulator for statistical metrics for use in a single job. Requires Flush()-ing to Reporter.
// Not concurrency-safe.
type Accumulator struct {
jobID string
stats [NumStats]map[string]uint64 // Array of metrics by Stat. Each metric is a map of uint64 values by target.
metrics *Metrics
}
type dimensions struct {
jobID string
target string
}
// Add n to the Accumulator Stat value. Returns self for chaining.
func (a *Accumulator) Add(target string, s Stat, n uint64) *Accumulator {
a.stats[s][target] += n
return a
}
// Inc increases Accumulator Stat value by 1. Returns self for chaining.
func (a *Accumulator) Inc(target string, s Stat) *Accumulator { return a.Add(target, s, 1) }
// Flush Accumulator contents to the Reporter.
func (a *Accumulator) Flush() {
for stat := RequestsAttemptedStat; stat < NumStats; stat++ {
for target, value := range a.stats[stat] {
a.metrics[stat].Store(dimensions{jobID: a.jobID, target: target}, value)
}
}
}
// Clone a new, blank metrics Accumulator with the same Reporter as the original.
func (a *Accumulator) Clone(jobID string) *Accumulator {
if a == nil {
return nil
}
return newAccumulator(jobID, a.metrics)
}
func newAccumulator(jobID string, data *Metrics) *Accumulator {
res := &Accumulator{
jobID: jobID,
metrics: data,
}
for s := RequestsAttemptedStat; s < NumStats; s++ {
res.stats[s] = make(map[string]uint64)
}
return res
}