forked from draios/veneur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforward_grpc_test.go
355 lines (325 loc) · 9.38 KB
/
forward_grpc_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
353
354
355
package veneur_test
import (
"context"
"errors"
"net"
"net/url"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stripe/veneur/v14"
"github.com/stripe/veneur/v14/samplers"
"github.com/stripe/veneur/v14/sinks"
"github.com/stripe/veneur/v14/trace"
"github.com/stripe/veneur/v14/util"
)
type forwardGRPCFixture struct {
t testing.TB
proxy *veneur.Proxy
global *veneur.Server
local *veneur.Server
}
// generateConfig is not called config to avoid
// accidental variable shadowing
func generateConfig() veneur.Config {
return veneur.Config{
Debug: true,
// Use a shorter interval for tests
Interval: veneur.DefaultFlushInterval,
Percentiles: []float64{.5, .75, .99},
Aggregates: []string{"min", "max", "count"},
ReadBufferSizeBytes: 2097152,
HTTPAddress: "localhost:0",
NumWorkers: 4,
// Use only one reader, so that we can run tests
// on platforms which do not support SO_REUSEPORT
NumReaders: 1,
// Currently this points nowhere, which is intentional.
// We don't need internal metrics for the tests, and they make testing
// more complicated.
StatsAddress: "localhost:8125",
// Don't use the default port 8128: Veneur sends its own traces there, causing failures
SsfListenAddresses: []util.Url{{
Value: &url.URL{
Scheme: "udp",
Host: "127.0.0.1:0",
},
}},
TraceMaxLengthBytes: 4096,
}
}
// setupVeneurServer creates a local server from the specified config
// and starts listening for requests. It returns the server for
// inspection. If no metricSink or spanSink are provided then a
// `black hole` sink will be used so that flushes to these sinks do
// "nothing".
func setupVeneurServer(
t testing.TB, config veneur.Config,
) *veneur.Server {
logger := logrus.New()
server, err := veneur.NewFromConfig(veneur.ServerConfig{
Logger: logger,
Config: config,
MetricSinkTypes: veneur.MetricSinkTypes{
"channel": {
Create: func(
server *veneur.Server, name string, logger *logrus.Entry,
config veneur.Config, sinkConfig veneur.MetricSinkConfig,
) (sinks.MetricSink, error) {
ch, ok := sinkConfig.(chan []samplers.InterMetric)
if !ok {
return nil, errors.New("invalid config")
}
return veneur.NewChannelMetricSink(ch)
},
ParseConfig: func(
name string, config interface{},
) (veneur.MetricSinkConfig, error) {
return config, nil
},
},
},
})
if err != nil {
t.Fatal(err)
}
// Make sure we don't send internal metrics when testing:
trace.NeutralizeClient(server.TraceClient)
server.Start()
return server
}
type testHTTPStarter interface {
IsListeningHTTP() bool
}
// waitForHTTPStart blocks until the Server's HTTP server is started, or until
// the specified duration is elapsed.
func waitForHTTPStart(t testing.TB, s testHTTPStarter, timeout time.Duration) {
tickCh := time.Tick(10 * time.Millisecond)
timeoutCh := time.After(timeout)
for {
select {
case <-tickCh:
if s.IsListeningHTTP() {
return
}
case <-timeoutCh:
t.Errorf("The HTTP server did not start within the specified duration")
}
}
}
// newForwardGRPCFixture creates a set of resources that forward to each other
// over gRPC. Specifically this includes a local Server, which forwards
// metrics over gRPC to a Proxy, which then forwards over gRPC again to a
// global Server.
func newForwardGRPCFixture(
t testing.TB, ch chan []samplers.InterMetric,
) *forwardGRPCFixture {
// Create a global Veneur
globalConfig := generateConfig()
globalConfig.GrpcAddress = unusedLocalTCPAddress(t)
globalConfig.MetricSinks = []veneur.SinkConfig{{
Name: "channel",
Kind: "channel",
Config: ch,
}}
global := setupVeneurServer(t, globalConfig)
go func() {
global.Serve()
}()
waitForHTTPStart(t, global, 3*time.Second)
// Create a proxy Veneur
proxyConfig := veneur.ProxyConfig{
Debug: false,
ConsulRefreshInterval: "86400s",
ConsulTraceServiceName: "traceServiceName",
TraceAddress: "127.0.0.1:8128",
TraceAPIAddress: "127.0.0.1:8135",
HTTPAddress: "127.0.0.1:0",
GrpcAddress: unusedLocalTCPAddress(t),
StatsAddress: "127.0.0.1:8201",
GrpcForwardAddress: globalConfig.GrpcAddress,
}
proxy, err := veneur.NewProxyFromConfig(logrus.New(), proxyConfig)
assert.NoError(t, err)
go func() {
proxy.Serve()
}()
waitForHTTPStart(t, proxy, 3*time.Second)
localConfig := generateConfig()
localConfig.ForwardAddress = proxyConfig.GrpcAddress
localConfig.ForwardUseGrpc = true
local := setupVeneurServer(t, localConfig)
return &forwardGRPCFixture{t: t, proxy: proxy, global: global, local: local}
}
// stop stops all of the various servers inside the fixture.
func (ff *forwardGRPCFixture) stop() {
ff.proxy.Shutdown()
ff.global.Shutdown()
ff.local.Shutdown()
}
// IngestMetric synchronously writes a metric to the forwarding
// fixture's local veneur span worker. The fixture must be flushed via
// the (*forwardFixture).local.Flush method so the ingestion effects can be
// observed.
func (ff *forwardGRPCFixture) IngestMetric(m *samplers.UDPMetric) {
ff.local.Workers[0].ProcessMetric(m)
}
// unusedLocalTCPAddress returns a host:port combination on the loopback
// interface that *should* be available for usage.
// This is definitely pretty hacky, but I was having a tricky time coming
// up with a good way to expose the gRPC listening address of the server or
// proxy without awkwardly saving the listener or listen address, and then
// modifying the gRPC serve function to create the network listener before
// spawning the goroutine.
func unusedLocalTCPAddress(t testing.TB) string {
ln, err := net.Listen("tcp", "127.0.0.1:")
if err != nil {
t.Fatalf("Failed to bind to a test port: %v", err)
}
defer ln.Close()
return ln.Addr().String()
}
func forwardGRPCTestMetrics() []*samplers.UDPMetric {
return []*samplers.UDPMetric{{
MetricKey: samplers.MetricKey{
Name: "test.grpc.histogram",
Type: veneur.HistogramTypeName,
},
Value: 20.0,
Digest: 12345,
SampleRate: 1.0,
Scope: samplers.MixedScope,
}, {
MetricKey: samplers.MetricKey{
Name: "test.grpc.histogram_global",
Type: veneur.HistogramTypeName,
},
Value: 20.0,
Digest: 12345,
SampleRate: 1.0,
Scope: samplers.GlobalOnly,
}, {
MetricKey: samplers.MetricKey{
Name: "test.grpc.gauge",
Type: veneur.GaugeTypeName,
},
Value: 1.0,
SampleRate: 1.0,
Scope: samplers.GlobalOnly,
}, {
MetricKey: samplers.MetricKey{
Name: "test.grpc.counter",
Type: veneur.CounterTypeName,
},
Value: 2.0,
SampleRate: 1.0,
Scope: samplers.GlobalOnly,
}, {
MetricKey: samplers.MetricKey{
Name: "test.grpc.timer_mixed",
Type: veneur.TimerTypeName,
},
Value: 100.0,
Digest: 12345,
SampleRate: 1.0,
Scope: samplers.MixedScope,
}, {
MetricKey: samplers.MetricKey{
Name: "test.grpc.timer",
Type: veneur.TimerTypeName,
},
Value: 100.0,
Digest: 12345,
SampleRate: 1.0,
Scope: samplers.GlobalOnly,
}, {
MetricKey: samplers.MetricKey{
Name: "test.grpc.set",
Type: veneur.SetTypeName,
},
Value: "test",
SampleRate: 1.0,
Scope: samplers.GlobalOnly,
}, {
MetricKey: samplers.MetricKey{
Name: "test.grpc.counter.local",
Type: veneur.CounterTypeName,
},
Value: 100.0,
Digest: 12345,
SampleRate: 1.0,
Scope: samplers.MixedScope,
}}
}
// TestE2EForwardingGRPCMetrics inputs a set of metrics to a local Veneur,
// and verifies that the same metrics are later flushed by the global Veneur
// after passing through a proxy.
func TestE2EForwardingGRPCMetrics(t *testing.T) {
ch := make(chan []samplers.InterMetric)
ff := newForwardGRPCFixture(t, ch)
defer ff.stop()
input := forwardGRPCTestMetrics()
for _, metric := range input {
ff.IngestMetric(metric)
}
done := make(chan struct{})
go func() {
defer close(done)
expected := map[string]bool{}
for _, name := range []string{
"test.grpc.histogram.50percentile",
"test.grpc.histogram.75percentile",
"test.grpc.histogram.99percentile",
"test.grpc.histogram_global.99percentile",
"test.grpc.histogram_global.50percentile",
"test.grpc.histogram_global.75percentile",
"test.grpc.histogram_global.max",
"test.grpc.histogram_global.min",
"test.grpc.histogram_global.count",
"test.grpc.timer_mixed.50percentile",
"test.grpc.timer_mixed.75percentile",
"test.grpc.timer_mixed.99percentile",
"test.grpc.timer.50percentile",
"test.grpc.timer.75percentile",
"test.grpc.timer.99percentile",
"test.grpc.timer.max",
"test.grpc.timer.min",
"test.grpc.timer.count",
"test.grpc.counter",
"test.grpc.gauge",
"test.grpc.set",
} {
expected[name] = false
}
metrics:
for {
metrics := <-ch
for _, metric := range metrics {
_, ok := expected[metric.Name]
if !ok {
t.Errorf("unexpected metric %q", metric.Name)
continue
}
expected[metric.Name] = true
}
for name, got := range expected {
if !got {
// we have more metrics to read:
t.Logf("metric %q still missing", name)
continue metrics
}
}
// if there had been metrics to read, we'd
// have restarted the loop:
return
}
}()
ff.local.Flush(context.TODO())
ff.global.Flush(context.TODO())
select {
case <-done:
case <-time.After(3 * time.Second):
t.Fatal("Timed out waiting for a metric after 3 seconds")
}
}