This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from stripe/veneur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flusher_test.go
180 lines (158 loc) · 4.73 KB
/
flusher_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
package veneur
import (
"context"
"testing"
"time"
"github.com/stripe/veneur/samplers"
"github.com/stripe/veneur/ssf"
"github.com/stripe/veneur/trace"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stripe/veneur/internal/forwardtest"
"github.com/stripe/veneur/samplers/metricpb"
)
func TestServerFlushGRPC(t *testing.T) {
done := make(chan []string)
testServer := forwardtest.NewServer(func(ms []*metricpb.Metric) {
var names []string
for _, m := range ms {
names = append(names, m.Name)
}
done <- names
})
testServer.Start(t)
defer testServer.Stop()
localCfg := localConfig()
localCfg.DebugFlushedMetrics = true
localCfg.ForwardAddress = testServer.Addr().String()
localCfg.ForwardUseGrpc = true
local := setupVeneurServer(t, localCfg, nil, nil, nil, nil)
defer local.Shutdown()
inputs := forwardGRPCTestMetrics()
for _, input := range inputs {
local.Workers[0].ProcessMetric(input)
}
expected := []string{
testGRPCMetric("histogram"),
testGRPCMetric("histogram_global"),
testGRPCMetric("timer"),
testGRPCMetric("timer_mixed"),
testGRPCMetric("counter"),
testGRPCMetric("gauge"),
testGRPCMetric("set"),
}
// Wait until the running server has flushed out the metrics for us:
select {
case v := <-done:
log.Print("got the goods")
assert.ElementsMatch(t, expected, v,
"Flush didn't output the right metrics")
case <-time.After(time.Second):
log.Print("timed out")
t.Fatal("Timed out waiting for the gRPC server to receive the flush")
}
}
func TestServerFlushGRPCTimeout(t *testing.T) {
testServer := forwardtest.NewServer(func(ms []*metricpb.Metric) {
time.Sleep(500 * time.Millisecond)
})
testServer.Start(t)
defer testServer.Stop()
spanCh := make(chan *ssf.SSFSpan, 900)
cl, err := trace.NewChannelClient(spanCh)
require.NoError(t, err)
defer func() {
cl.Close()
}()
got := make(chan *ssf.SSFSample)
go func() {
for span := range spanCh {
for _, sample := range span.Metrics {
if sample.Name == "forward.error_total" && span.Tags != nil && sample.Tags["cause"] == "deadline_exceeded" {
got <- sample
return
}
}
}
}()
localCfg := localConfig()
localCfg.Interval = "20us"
localCfg.DebugFlushedMetrics = true
localCfg.ForwardAddress = testServer.Addr().String()
localCfg.ForwardUseGrpc = true
local := setupVeneurServer(t, localCfg, nil, nil, nil, cl)
defer local.Shutdown()
inputs := forwardGRPCTestMetrics()
for _, input := range inputs {
local.Workers[0].ProcessMetric(input)
}
// Wait until the running server has flushed out the metrics for us:
select {
case v := <-got:
assert.Equal(t, float32(1.0), v.Value)
case <-time.After(time.Second):
t.Fatal("The server didn't time out flushing.")
}
}
// Just test that a flushing to a bad address is handled without panicing
func TestServerFlushGRPCBadAddress(t *testing.T) {
rcv := make(chan []samplers.InterMetric, 10)
sink, err := NewChannelMetricSink(rcv)
require.NoError(t, err)
localCfg := localConfig()
localCfg.ForwardAddress = "bad-address:123"
localCfg.ForwardUseGrpc = true
local := setupVeneurServer(t, localCfg, nil, sink, nil, nil)
defer local.Shutdown()
local.Workers[0].ProcessMetric(forwardGRPCTestMetrics()[0])
m := samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "counter",
Type: counterTypeName,
},
Value: 20.0,
Digest: 12345,
SampleRate: 1.0,
Scope: samplers.MixedScope,
}
local.Workers[0].ProcessMetric(&m)
select {
case <-rcv:
t.Log("Received my metric, assume this is working.")
case <-time.After(time.Second * 5):
t.Fatal("timed out waiting for global veneur flush")
}
}
// Ensure that if someone sends a histogram to the global stats box directly,
// it emits both aggregates and percentiles (basically behaves like a global
// histo).
func TestGlobalAcceptsHistogramsOverUDP(t *testing.T) {
rcv := make(chan []samplers.InterMetric, 10)
sink, err := NewChannelMetricSink(rcv)
require.NoError(t, err)
cfg := globalConfig()
cfg.Percentiles = []float64{}
cfg.Aggregates = []string{"min"}
global := setupVeneurServer(t, cfg, nil, sink, nil, nil)
defer global.Shutdown()
// simulate introducing a histogram to a global veneur.
m := samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "histo",
Type: histogramTypeName,
},
Value: 20.0,
Digest: 12345,
SampleRate: 1.0,
Scope: samplers.MixedScope,
}
global.Workers[0].ProcessMetric(&m)
global.Flush(context.Background())
select {
case results := <-rcv:
assert.Len(t, results, 1, "too many metrics for global histo flush")
assert.Equal(t, "histo.min", results[0].Name, "flushed global metric was incorrect aggregate")
case <-time.After(time.Second * 5):
t.Fatal("timed out waiting for global veneur flush")
}
}