-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregator_test.go
313 lines (275 loc) · 7.74 KB
/
aggregator_test.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package graphite
import (
"bytes"
"errors"
"fmt"
"strconv"
"sync"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("graphite aggregator", func() {
var (
client Graphite
agg Aggregator
testMetric = "metric"
failMetric = "force.fail"
mutex = &sync.Mutex{}
)
var getTotalSent = func(client Graphite) int {
mutex.Lock()
defer mutex.Unlock()
total := 0
for _, value := range client.(*MockGraphite).Data {
number, _ := strconv.Atoi(value)
total += number
}
return total
}
var getFlushSent = func(client Graphite) int {
mutex.Lock()
defer mutex.Unlock()
return len(client.(*MockGraphite).Data)
}
var getMetricInfo = func(received string) (string, int, int) {
var metric string
var value int
var timestamp int
fmt.Sscanf(received, "%s %d %d\n", &metric, &value, ×tamp)
return metric, value, timestamp
}
BeforeEach(func() {
client = &MockGraphite{
Extra: map[string]interface{}{},
Data: map[string]string{},
MethodReconnect: func(m *MockGraphite) error {
m.Extra["reconnected"] = "yes, it tried!"
return nil
},
MethodSendBuffer: func(m *MockGraphite, buffer *bytes.Buffer) (int, error) {
mutex.Lock()
defer mutex.Unlock()
metric, value, _ := getMetricInfo(buffer.String())
entry := strconv.FormatInt(time.Now().UnixNano(), 10)
m.Data[entry] = strconv.FormatInt(int64(value), 10)
if metric == failMetric {
return 0, errors.New("Unable to send metrics to graphite")
}
return 0, nil
},
}
agg = &aggregator{
config: &Config{},
client: client,
metrics: map[string]Metric{},
}
})
It("should implement Aggregator interface", func() {
var _ Aggregator = (*aggregator)(nil)
})
It("is returned by the graphite client properly configured", func() {
client = NewGraphiteTCP(&Config{Host: "test.com"})
aggregator := client.NewAggregator().(*aggregator)
Expect(aggregator.config).To(Equal(client.(*graphite).config))
Expect(aggregator.client).To(Equal(client))
})
Context("sum aggregates", func() {
It("should initialise metric with first given value", func() {
agg.AddSum(testMetric, 5)
metrics := agg.(*aggregator).GetMetrics()
Expect(metrics[testMetric].Calculate()).To(Equal("5"))
})
It("should sum subsequent metrics", func() {
agg.AddSum(testMetric, 5)
agg.AddSum(testMetric, 3)
metrics := agg.(*aggregator).GetMetrics()
Expect(metrics[testMetric].Calculate()).To(Equal("8"))
})
It("is thread-safe", func() {
var wg sync.WaitGroup
wg.Add(200)
for i := 0; i < 200; i++ {
go func(value int) {
agg.AddSum(testMetric, value)
wg.Done()
}(i)
}
wg.Wait()
metrics := agg.(*aggregator).GetMetrics()
Expect(metrics[testMetric].Calculate()).To(Equal("19900"))
})
})
Context("increase aggregates", func() {
It("should initialise metric with 1", func() {
agg.Increase(testMetric)
metrics := agg.(*aggregator).GetMetrics()
Expect(metrics[testMetric].Calculate()).To(Equal("1"))
})
It("should increase subsequent calls by 1", func() {
for i := 0; i < 200; i++ {
agg.Increase(testMetric)
}
metrics := agg.(*aggregator).GetMetrics()
Expect(metrics[testMetric].Calculate()).To(Equal("200"))
})
It("is thread-safe", func() {
var wg sync.WaitGroup
wg.Add(200)
for i := 0; i < 200; i++ {
go func() {
agg.Increase(testMetric)
wg.Done()
}()
}
wg.Wait()
metrics := agg.(*aggregator).GetMetrics()
Expect(metrics[testMetric].Calculate()).To(Equal("200"))
})
})
Context("average aggregates", func() {
It("should initialise metric with first given value", func() {
agg.AddAverage(testMetric, 5)
metrics := agg.(*aggregator).GetMetrics()
Expect(metrics[testMetric].Calculate()).To(Equal("5.000000"))
})
It("should calculate the average from the subsequent metrics", func() {
agg.AddAverage(testMetric, 5)
agg.AddAverage(testMetric, 3)
metrics := agg.(*aggregator).GetMetrics()
Expect(metrics[testMetric].Calculate()).To(Equal("4.000000"))
})
It("is thread-safe", func() {
var wg sync.WaitGroup
wg.Add(200)
for i := 0; i < 200; i++ {
go func(value int) {
agg.AddAverage(testMetric, value)
wg.Done()
}(i)
}
wg.Wait()
metrics := agg.(*aggregator).GetMetrics()
Expect(metrics[testMetric].Calculate()).To(Equal("99.500000"))
})
})
Context("active/inactive aggregates", func() {
It("should set metric to active", func() {
agg.SetActive(testMetric)
metrics := agg.(*aggregator).GetMetrics()
Expect(metrics[testMetric].Calculate()).To(Equal("1"))
})
It("should set metric to inactive", func() {
agg.SetInactive(testMetric)
metrics := agg.(*aggregator).GetMetrics()
Expect(metrics[testMetric].Calculate()).To(Equal("0"))
})
It("should keep the latest metric set", func() {
agg.SetActive(testMetric)
agg.SetInactive(testMetric)
metrics := agg.(*aggregator).GetMetrics()
Expect(metrics[testMetric].Calculate()).To(Equal("0"))
})
It("is thread-safe", func() {
var wg sync.WaitGroup
wg.Add(200)
for i := 0; i < 200; i++ {
go func(value int) {
if value%2 == 0 {
agg.SetActive(testMetric)
} else {
agg.SetInactive(testMetric)
}
wg.Done()
}(i)
}
wg.Wait()
metrics := agg.(*aggregator).GetMetrics()
Expect(metrics[testMetric].Calculate()).To(SatisfyAny(Equal("0"), Equal("1")))
})
})
Context("flushes the aggregates to send them to graphite", func() {
It("is thread-safe", func() {
var wg sync.WaitGroup
wg.Add(200)
for i := 0; i < 200; i++ {
go func(value int) {
agg.AddSum(testMetric, value)
if value%50 == 0 {
agg.Flush()
}
wg.Done()
}(i)
}
wg.Wait()
agg.Flush()
Expect(getTotalSent(client)).To(Equal(19900))
})
})
Context("runs periodically", func() {
var (
stop chan bool
)
BeforeEach(func() {
stop = make(chan bool)
agg = &aggregator{
config: &Config{},
client: client,
metrics: map[string]Metric{},
}
})
It("flushes every tick", func() {
agg.Run(2*time.Second, stop)
agg.AddSum(testMetric, 15)
agg.AddSum(testMetric, 25)
time.Sleep(3 * time.Second)
Expect(getTotalSent(client)).To(Equal(40))
agg.AddSum(testMetric, 30)
time.Sleep(3 * time.Second)
Expect(getTotalSent(client)).To(Equal(70))
stop <- true
})
It("uses a chan bool to stop the periodic flushing", func() {
agg.Run(2*time.Second, stop)
agg.AddSum(testMetric, 15)
time.Sleep(3 * time.Second)
Expect(getTotalSent(client)).To(Equal(15))
stop <- true
agg.AddSum(testMetric, 25)
time.Sleep(3 * time.Second)
Expect(getTotalSent(client)).To(Equal(15))
})
It("keeps trying periodically even if one Flush fails, keeping the data", func() {
agg.Run(2*time.Second, stop)
agg.AddSum(failMetric, 15)
time.Sleep(3 * time.Second)
Expect(getFlushSent(client)).To(Equal(2))
agg.AddSum(failMetric, 25)
time.Sleep(2 * time.Second)
Expect(getFlushSent(client)).To(Equal(4))
metrics := agg.(*aggregator).GetMetrics()
Expect(metrics[failMetric].Calculate()).To(Equal("40"))
})
It("retries once if something went wrong", func() {
agg.Run(2*time.Second, stop)
agg.AddSum(failMetric, 15)
time.Sleep(3 * time.Second)
Expect(getFlushSent(client)).To(Equal(2))
Expect(client.(*MockGraphite).Extra).To(HaveKey("reconnected"))
})
})
Context("uses client configuration", func() {
It("uses the namespace/prefix before sending metrics", func() {
agg = &aggregator{
config: &Config{
Namespace: "beta.instance",
},
client: client,
metrics: map[string]Metric{},
}
agg.AddSum(testMetric, 5000)
metrics := agg.(*aggregator).GetMetrics()
Expect(metrics["beta.instance."+testMetric].Calculate()).To(Equal("5000"))
})
})
})