-
Notifications
You must be signed in to change notification settings - Fork 177
/
inmem_endpoint_test.go
352 lines (317 loc) · 8.28 KB
/
inmem_endpoint_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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MIT
package metrics
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/pascaldekloe/goe/verify"
)
func TestDisplayMetrics(t *testing.T) {
interval := 10 * time.Millisecond
inm := NewInmemSink(interval, 50*time.Millisecond)
// Add data points
inm.SetGauge([]string{"foo", "bar"}, 42)
inm.SetGaugeWithLabels([]string{"foo", "bar"}, 23, []Label{{"a", "b"}})
inm.EmitKey([]string{"foo", "bar"}, 42)
inm.IncrCounter([]string{"foo", "bar"}, 20)
inm.IncrCounter([]string{"foo", "bar"}, 22)
inm.IncrCounterWithLabels([]string{"foo", "bar"}, 20, []Label{{"a", "b"}})
inm.IncrCounterWithLabels([]string{"foo", "bar"}, 40, []Label{{"a", "b"}})
inm.AddSample([]string{"foo", "bar"}, 20)
inm.AddSample([]string{"foo", "bar"}, 24)
inm.AddSampleWithLabels([]string{"foo", "bar"}, 23, []Label{{"a", "b"}})
inm.AddSampleWithLabels([]string{"foo", "bar"}, 33, []Label{{"a", "b"}})
data := inm.Data()
if len(data) != 1 {
t.Fatalf("bad: %v", data)
}
expected := MetricsSummary{
Timestamp: data[0].Interval.Round(time.Second).UTC().String(),
Gauges: []GaugeValue{
{
Name: "foo.bar",
Hash: "foo.bar",
Value: float32(42),
DisplayLabels: map[string]string{},
},
{
Name: "foo.bar",
Hash: "foo.bar;a=b",
Value: float32(23),
DisplayLabels: map[string]string{"a": "b"},
},
},
Points: []PointValue{
{
Name: "foo.bar",
Points: []float32{42},
},
},
Counters: []SampledValue{
{
Name: "foo.bar",
Hash: "foo.bar",
AggregateSample: &AggregateSample{
Count: 2,
Min: 20,
Max: 22,
Sum: 42,
SumSq: 884,
Rate: 4200,
},
Mean: 21,
Stddev: 1.4142135623730951,
},
{
Name: "foo.bar",
Hash: "foo.bar;a=b",
AggregateSample: &AggregateSample{
Count: 2,
Min: 20,
Max: 40,
Sum: 60,
SumSq: 2000,
Rate: 6000,
},
Mean: 30,
Stddev: 14.142135623730951,
DisplayLabels: map[string]string{"a": "b"},
},
},
Samples: []SampledValue{
{
Name: "foo.bar",
Hash: "foo.bar",
AggregateSample: &AggregateSample{
Count: 2,
Min: 20,
Max: 24,
Sum: 44,
SumSq: 976,
Rate: 4400,
},
Mean: 22,
Stddev: 2.8284271247461903,
},
{
Name: "foo.bar",
Hash: "foo.bar;a=b",
AggregateSample: &AggregateSample{
Count: 2,
Min: 23,
Max: 33,
Sum: 56,
SumSq: 1618,
Rate: 5600,
},
Mean: 28,
Stddev: 7.0710678118654755,
DisplayLabels: map[string]string{"a": "b"},
},
},
}
raw, err := inm.DisplayMetrics(nil, nil)
if err != nil {
t.Fatalf("err: %v", err)
}
result := raw.(MetricsSummary)
// Ignore the LastUpdated field, we don't export that anyway
for i, got := range result.Counters {
expected.Counters[i].LastUpdated = got.LastUpdated
}
for i, got := range result.Samples {
expected.Samples[i].LastUpdated = got.LastUpdated
}
verify.Values(t, "all", result, expected)
}
func TestDisplayMetrics_RaceSetGauge(t *testing.T) {
interval := 200 * time.Millisecond
inm := NewInmemSink(interval, 10*interval)
result := make(chan float32)
go func() {
for {
time.Sleep(150 * time.Millisecond)
inm.SetGauge([]string{"foo", "bar"}, float32(42))
}
}()
go func() {
start := time.Now()
var summary MetricsSummary
// test for twenty intervals
for time.Now().Sub(start) < 20*interval {
time.Sleep(100 * time.Millisecond)
raw, _ := inm.DisplayMetrics(nil, nil)
summary = raw.(MetricsSummary)
}
// save result
for _, g := range summary.Gauges {
if g.Name == "foo.bar" {
result <- g.Value
}
}
close(result)
}()
got := <-result
verify.Values(t, "all", got, float32(42))
}
func TestDisplayMetrics_RaceAddSample(t *testing.T) {
interval := 200 * time.Millisecond
inm := NewInmemSink(interval, 10*interval)
result := make(chan float32)
go func() {
for {
time.Sleep(75 * time.Millisecond)
inm.AddSample([]string{"foo", "bar"}, float32(0.0))
}
}()
go func() {
start := time.Now()
var summary MetricsSummary
// test for twenty intervals
for time.Now().Sub(start) < 20*interval {
time.Sleep(100 * time.Millisecond)
raw, _ := inm.DisplayMetrics(nil, nil)
summary = raw.(MetricsSummary)
}
// save result
for _, g := range summary.Gauges {
if g.Name == "foo.bar" {
result <- g.Value
}
}
close(result)
}()
got := <-result
verify.Values(t, "all", got, float32(0.0))
}
func TestDisplayMetrics_RaceIncrCounter(t *testing.T) {
interval := 200 * time.Millisecond
inm := NewInmemSink(interval, 10*interval)
result := make(chan float32)
go func() {
for {
time.Sleep(75 * time.Millisecond)
inm.IncrCounter([]string{"foo", "bar"}, float32(0.0))
}
}()
go func() {
start := time.Now()
var summary MetricsSummary
// test for twenty intervals
for time.Now().Sub(start) < 20*interval {
time.Sleep(30 * time.Millisecond)
raw, _ := inm.DisplayMetrics(nil, nil)
summary = raw.(MetricsSummary)
}
// save result for testing
for _, g := range summary.Gauges {
if g.Name == "foo.bar" {
result <- g.Value
}
}
close(result)
}()
got := <-result
verify.Values(t, "all", got, float32(0.0))
}
func TestDisplayMetrics_RaceMetricsSetGauge(t *testing.T) {
interval := 200 * time.Millisecond
inm := NewInmemSink(interval, 10*interval)
met := &Metrics{Config: Config{FilterDefault: true}, sink: inm}
result := make(chan float32)
labels := []Label{
{"name1", "value1"},
{"name2", "value2"},
}
go func() {
for {
time.Sleep(75 * time.Millisecond)
met.SetGaugeWithLabels([]string{"foo", "bar"}, float32(42), labels)
}
}()
go func() {
start := time.Now()
var summary MetricsSummary
// test for twenty intervals
for time.Now().Sub(start) < 40*interval {
time.Sleep(150 * time.Millisecond)
raw, _ := inm.DisplayMetrics(nil, nil)
summary = raw.(MetricsSummary)
}
// save result
for _, g := range summary.Gauges {
if g.Name == "foo.bar" {
result <- g.Value
}
}
close(result)
}()
got := <-result
verify.Values(t, "all", got, float32(42))
}
func TestInmemSink_Stream(t *testing.T) {
interval := 10 * time.Millisecond
total := 50 * time.Millisecond
inm := NewInmemSink(interval, total)
ctx, cancel := context.WithTimeout(context.Background(), total*2)
defer cancel()
chDone := make(chan struct{})
go func() {
for i := float32(0); ctx.Err() == nil; i++ {
inm.SetGaugeWithLabels([]string{"gauge", "foo"}, 20+i, []Label{{"a", "b"}})
inm.EmitKey([]string{"key", "foo"}, 30+i)
inm.IncrCounterWithLabels([]string{"counter", "bar"}, 40+i, []Label{{"a", "b"}})
inm.IncrCounterWithLabels([]string{"counter", "bar"}, 50+i, []Label{{"a", "b"}})
inm.AddSampleWithLabels([]string{"sample", "bar"}, 60+i, []Label{{"a", "b"}})
inm.AddSampleWithLabels([]string{"sample", "bar"}, 70+i, []Label{{"a", "b"}})
time.Sleep(interval / 3)
}
close(chDone)
}()
resp := httptest.NewRecorder()
enc := encoder{
encoder: json.NewEncoder(resp),
flusher: resp,
}
inm.Stream(ctx, enc)
<-chDone
decoder := json.NewDecoder(resp.Body)
var prevGaugeValue float32
for i := 0; i < 8; i++ {
var summary MetricsSummary
if err := decoder.Decode(&summary); err != nil {
t.Fatalf("expected no error while decoding response %d, got %v", i, err)
}
if count := len(summary.Gauges); count != 1 {
t.Fatalf("expected at least one gauge in response %d, got %v", i, count)
}
value := summary.Gauges[0].Value
// The upper bound of the gauge value is not known, but we can expect it
// to be less than 50 because it increments by 3 every interval and we run
// for ~10 intervals.
if value < 20 || value > 50 {
t.Fatalf("expected interval %d guage value between 20 and 50, got %v", i, value)
}
if value <= prevGaugeValue {
t.Fatalf("expected interval %d guage value to be greater than previous, %v == %v", i, value, prevGaugeValue)
}
prevGaugeValue = value
}
}
type encoder struct {
flusher http.Flusher
encoder *json.Encoder
}
func (e encoder) Encode(metrics interface{}) error {
if err := e.encoder.Encode(metrics); err != nil {
fmt.Println("failed to encode metrics summary", "error", err)
return err
}
e.flusher.Flush()
return nil
}