-
Notifications
You must be signed in to change notification settings - Fork 46
/
aggregator.go
172 lines (145 loc) · 4.6 KB
/
aggregator.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package health
import (
"time"
)
type aggregator struct {
// How long is each aggregation interval. Eg, 1 minute
intervalDuration time.Duration
// Retain controls how many metrics interval we keep. Eg, 5 minutes
retain time.Duration
// maxIntervals is the maximum length of intervals.
// It is retain / interval.
maxIntervals int
// intervals is a slice of the retained intervals
intervalAggregations []*IntervalAggregation
}
func startAggregator(intervalDuration time.Duration, retain time.Duration, sink *JsonPollingSink) {
cmdChan := sink.cmdChan
doneChan := sink.doneChan
intervalsChanChan := sink.intervalsChanChan
ticker := time.Tick(1 * time.Second)
agg := newAggregator(intervalDuration, retain)
AGGREGATE_LOOP:
for {
select {
case <-doneChan:
sink.doneDoneChan <- 1
break AGGREGATE_LOOP
case cmd := <-cmdChan:
if cmd.Kind == cmdKindEvent {
agg.EmitEvent(cmd.Job, cmd.Event)
} else if cmd.Kind == cmdKindEventErr {
agg.EmitEventErr(cmd.Job, cmd.Event, cmd.Err)
} else if cmd.Kind == cmdKindTiming {
agg.EmitTiming(cmd.Job, cmd.Event, cmd.Nanos)
} else if cmd.Kind == cmdKindGauge {
agg.EmitGauge(cmd.Job, cmd.Event, cmd.Value)
} else if cmd.Kind == cmdKindComplete {
agg.EmitComplete(cmd.Job, cmd.Status, cmd.Nanos)
}
case <-ticker:
agg.getIntervalAggregation() // this has the side effect of sliding the interval window if necessary.
case intervalsChan := <-intervalsChanChan:
intervalsChan <- agg.memorySafeIntervals()
}
}
}
func newAggregator(intervalDuration time.Duration, retain time.Duration) *aggregator {
maxIntervals := int(retain / intervalDuration)
return &aggregator{
intervalDuration: intervalDuration,
retain: retain,
maxIntervals: maxIntervals,
intervalAggregations: make([]*IntervalAggregation, 0, maxIntervals),
}
}
func (a *aggregator) memorySafeIntervals() []*IntervalAggregation {
ret := make([]*IntervalAggregation, 0, len(a.intervalAggregations))
curAgg := a.getIntervalAggregation()
for _, intAgg := range a.intervalAggregations {
if intAgg == curAgg {
ret = append(ret, intAgg.Clone())
} else {
ret = append(ret, intAgg)
}
}
return ret
}
func (a *aggregator) EmitEvent(job string, event string) {
intAgg := a.getIntervalAggregation()
intAgg.Events[event] = intAgg.Events[event] + 1
jobAgg := intAgg.getJobAggregation(job)
jobAgg.Events[event] = jobAgg.Events[event] + 1
intAgg.SerialNumber++
}
func (a *aggregator) EmitEventErr(job string, event string, inputErr error) {
intAgg := a.getIntervalAggregation()
errc := intAgg.getCounterErrs(event)
errc.incrementAndAddError(inputErr)
jobAgg := intAgg.getJobAggregation(job)
jerrc := jobAgg.getCounterErrs(event)
jerrc.incrementAndAddError(inputErr)
intAgg.SerialNumber++
}
func (a *aggregator) EmitTiming(job string, event string, nanos int64) {
intAgg := a.getIntervalAggregation()
t := intAgg.getTimers(event)
t.ingest(nanos)
jobAgg := intAgg.getJobAggregation(job)
jt := jobAgg.getTimers(event)
jt.ingest(nanos)
intAgg.SerialNumber++
}
func (a *aggregator) EmitGauge(job string, event string, value float64) {
intAgg := a.getIntervalAggregation()
intAgg.Gauges[event] = value
jobAgg := intAgg.getJobAggregation(job)
jobAgg.Gauges[event] = value
intAgg.SerialNumber++
}
func (a *aggregator) EmitComplete(job string, status CompletionStatus, nanos int64) {
intAgg := a.getIntervalAggregation()
jobAgg := intAgg.getJobAggregation(job)
jobAgg.ingest(status, nanos)
intAgg.SerialNumber++
}
func (a *aggregator) getIntervalAggregation() *IntervalAggregation {
intervalStart := now().Truncate(a.intervalDuration)
n := len(a.intervalAggregations)
if n > 0 && a.intervalAggregations[n-1].IntervalStart == intervalStart {
return a.intervalAggregations[n-1]
}
return a.createIntervalAggregation(intervalStart)
}
func (a *aggregator) createIntervalAggregation(interval time.Time) *IntervalAggregation {
// Make new interval:
current := NewIntervalAggregation(interval)
// If we've reached our max intervals, and we're going to shift everything down, then set the last one
n := len(a.intervalAggregations)
if n == a.maxIntervals {
for i := 1; i < n; i++ {
a.intervalAggregations[i-1] = a.intervalAggregations[i]
}
a.intervalAggregations[n-1] = current
} else {
a.intervalAggregations = append(a.intervalAggregations, current)
}
return current
}
var nowMock time.Time
func now() time.Time {
if nowMock.IsZero() {
return time.Now()
}
return nowMock
}
func setNowMock(t string) {
var err error
nowMock, err = time.Parse(time.RFC3339, t)
if err != nil {
panic(err)
}
}
func resetNowMock() {
nowMock = time.Time{}
}