forked from grafana/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e_test.go
517 lines (422 loc) · 15 KB
/
e2e_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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
package e2e
import (
"bytes"
"fmt"
"math/rand"
"os"
"path/filepath"
"reflect"
"sync"
"testing"
"text/template"
"time"
thrift "github.com/jaegertracing/jaeger/thrift-gen/jaeger"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
"github.com/grafana/e2e"
e2edb "github.com/grafana/e2e/db"
"github.com/grafana/tempo/cmd/tempo/app"
util "github.com/grafana/tempo/integration"
"github.com/grafana/tempo/integration/e2e/backend"
"github.com/grafana/tempo/pkg/model/trace"
"github.com/grafana/tempo/pkg/tempopb"
tempoUtil "github.com/grafana/tempo/pkg/util"
)
const (
configMicroservices = "config-microservices.tmpl.yaml"
configHA = "config-scalable-single-binary.yaml"
configAllInOneS3 = "config-all-in-one-s3.yaml"
configAllInOneAzurite = "config-all-in-one-azurite.yaml"
configAllInOneGCS = "config-all-in-one-gcs.yaml"
)
func TestAllInOne(t *testing.T) {
testBackends := []struct {
name string
configFile string
}{
{
name: "s3",
configFile: configAllInOneS3,
},
{
name: "azure",
configFile: configAllInOneAzurite,
},
{
name: "gcs",
configFile: configAllInOneGCS,
},
}
for _, tc := range testBackends {
t.Run(tc.name, func(t *testing.T) {
s, err := e2e.NewScenario("tempo_e2e")
require.NoError(t, err)
defer s.Close()
// set up the backend
cfg := app.Config{}
buff, err := os.ReadFile(tc.configFile)
require.NoError(t, err)
err = yaml.UnmarshalStrict(buff, &cfg)
require.NoError(t, err)
_, err = backend.New(s, cfg)
require.NoError(t, err)
require.NoError(t, util.CopyFileToSharedDir(s, tc.configFile, "config.yaml"))
tempo := util.NewTempoAllInOne()
require.NoError(t, s.StartAndWaitReady(tempo))
// Get port for the Jaeger gRPC receiver endpoint
c, err := util.NewJaegerGRPCClient(tempo.Endpoint(14250))
require.NoError(t, err)
require.NotNil(t, c)
info := tempoUtil.NewTraceInfo(time.Now(), "")
require.NoError(t, info.EmitAllBatches(c))
expected, err := info.ConstructTraceFromEpoch()
require.NoError(t, err)
// test metrics
require.NoError(t, tempo.WaitSumMetrics(e2e.Equals(spanCount(expected)), "tempo_distributor_spans_received_total"))
// test echo
assertEcho(t, "http://"+tempo.Endpoint(3200)+"/api/echo")
apiClient := tempoUtil.NewClient("http://"+tempo.Endpoint(3200), "")
// query an in-memory trace
queryAndAssertTrace(t, apiClient, info)
// search an in-memory trace
util.SearchAndAssertTrace(t, apiClient, info)
// wait trace_idle_time and ensure trace is created in ingester
require.NoError(t, tempo.WaitSumMetricsWithOptions(e2e.Less(3), []string{"tempo_ingester_traces_created_total"}, e2e.WaitMissingMetrics))
// flush trace to backend
callFlush(t, tempo)
// sleep
time.Sleep(10 * time.Second)
// force clear completed block
callFlush(t, tempo)
// test metrics
require.NoError(t, tempo.WaitSumMetrics(e2e.Equals(1), "tempo_ingester_blocks_flushed_total"))
require.NoError(t, tempo.WaitSumMetricsWithOptions(e2e.Equals(1), []string{"tempodb_blocklist_length"}, e2e.WaitMissingMetrics))
require.NoError(t, tempo.WaitSumMetrics(e2e.Equals(4), "tempo_query_frontend_queries_total"))
// query trace - should fetch from backend
queryAndAssertTrace(t, apiClient, info)
// search the backend. this works b/c we're passing a start/end AND setting query ingesters within min/max to 0
now := time.Now()
util.SearchAndAssertTraceBackend(t, apiClient, info, now.Add(-20*time.Minute).Unix(), now.Unix())
})
}
}
func TestMicroservicesWithKVStores(t *testing.T) {
testKVStores := []struct {
name string
kvconfig func(hostname string, port int) string
}{
{
name: "memberlist",
kvconfig: func(string, int) string {
return `
store: memberlist`
},
},
{
name: "etcd",
kvconfig: func(hostname string, port int) string {
return fmt.Sprintf(`
store: etcd
etcd:
endpoints:
- http://%s:%d`, hostname, port)
},
},
{
name: "consul",
kvconfig: func(hostname string, port int) string {
return fmt.Sprintf(`
store: consul
consul:
host: http://%s:%d`, hostname, port)
},
},
}
for _, tc := range testKVStores {
t.Run(tc.name, func(t *testing.T) {
s, err := e2e.NewScenario("tempo_e2e")
require.NoError(t, err)
defer s.Close()
// Set up KVStore
var kvstore *e2e.HTTPService
switch tc.name {
case "etcd":
kvstore = e2edb.NewETCD()
require.NoError(t, s.StartAndWaitReady(kvstore))
case "consul":
kvstore = e2edb.NewConsul()
require.NoError(t, s.StartAndWaitReady(kvstore))
case "memberlist":
default:
t.Errorf("unknown KVStore %s", tc.name)
}
tmpl, err := template.New(filepath.Base(configMicroservices)).ParseFiles(configMicroservices)
require.NoError(t, err)
KVStoreConfig := tc.kvconfig("", 0)
if kvstore != nil {
KVStoreConfig = tc.kvconfig(kvstore.Name(), kvstore.HTTPPort())
}
var buf bytes.Buffer
kvconfig := map[string]interface{}{
"KVStore": KVStoreConfig,
}
require.NoError(t, tmpl.Execute(&buf, kvconfig))
require.NoError(t, util.WriteFileToSharedDir(s, "config.yaml", buf.Bytes()))
minio := e2edb.NewMinio(9000, "tempo")
require.NotNil(t, minio)
require.NoError(t, s.StartAndWaitReady(minio))
tempoIngester1 := util.NewTempoIngester(1)
tempoIngester2 := util.NewTempoIngester(2)
tempoIngester3 := util.NewTempoIngester(3)
tempoDistributor := util.NewTempoDistributor()
tempoQueryFrontend := util.NewTempoQueryFrontend()
tempoQuerier := util.NewTempoQuerier()
require.NoError(t, s.StartAndWaitReady(tempoIngester1, tempoIngester2, tempoIngester3, tempoDistributor, tempoQueryFrontend, tempoQuerier))
// wait for active ingesters
time.Sleep(1 * time.Second)
matchers := []*labels.Matcher{
{
Type: labels.MatchEqual,
Name: "name",
Value: "ingester",
},
{
Type: labels.MatchEqual,
Name: "state",
Value: "ACTIVE",
},
}
require.NoError(t, tempoDistributor.WaitSumMetricsWithOptions(e2e.Equals(3), []string{`cortex_ring_members`}, e2e.WithLabelMatchers(matchers...), e2e.WaitMissingMetrics))
features := []*labels.Matcher{
{
Type: labels.MatchEqual,
Name: "feature",
Value: "search",
},
}
require.NoError(t, tempoDistributor.WaitSumMetricsWithOptions(e2e.Equals(1), []string{`tempo_feature_enabled`}, e2e.WithLabelMatchers(features...), e2e.WaitMissingMetrics))
// Get port for the Jaeger gRPC receiver endpoint
c, err := util.NewJaegerGRPCClient(tempoDistributor.Endpoint(14250))
require.NoError(t, err)
require.NotNil(t, c)
info := tempoUtil.NewTraceInfo(time.Now(), "")
require.NoError(t, info.EmitAllBatches(c))
expected, err := info.ConstructTraceFromEpoch()
require.NoError(t, err)
// test metrics
require.NoError(t, tempoDistributor.WaitSumMetrics(e2e.Equals(spanCount(expected)), "tempo_distributor_spans_received_total"))
// test echo
assertEcho(t, "http://"+tempoQueryFrontend.Endpoint(3200)+"/api/echo")
apiClient := tempoUtil.NewClient("http://"+tempoQueryFrontend.Endpoint(3200), "")
// query an in-memory trace
queryAndAssertTrace(t, apiClient, info)
// search an in-memory trace
util.SearchAndAssertTrace(t, apiClient, info)
// wait trace_idle_time and ensure trace is created in ingester
require.NoError(t, tempoIngester1.WaitSumMetricsWithOptions(e2e.Less(3), []string{"tempo_ingester_traces_created_total"}, e2e.WaitMissingMetrics))
require.NoError(t, tempoIngester2.WaitSumMetricsWithOptions(e2e.Less(3), []string{"tempo_ingester_traces_created_total"}, e2e.WaitMissingMetrics))
require.NoError(t, tempoIngester3.WaitSumMetricsWithOptions(e2e.Less(3), []string{"tempo_ingester_traces_created_total"}, e2e.WaitMissingMetrics))
// flush trace to backend
callFlush(t, tempoIngester1)
callFlush(t, tempoIngester2)
callFlush(t, tempoIngester3)
// sleep for one maintenance cycle
time.Sleep(5 * time.Second)
// test metrics
for _, i := range []*e2e.HTTPService{tempoIngester1, tempoIngester2, tempoIngester3} {
require.NoError(t, i.WaitSumMetrics(e2e.Equals(1), "tempo_ingester_blocks_flushed_total"))
}
require.NoError(t, tempoQuerier.WaitSumMetrics(e2e.Equals(3), "tempodb_blocklist_length"))
require.NoError(t, tempoQueryFrontend.WaitSumMetrics(e2e.Equals(4), "tempo_query_frontend_queries_total"))
// query trace - should fetch from backend
queryAndAssertTrace(t, apiClient, info)
// stop an ingester and confirm we can still write and query
err = tempoIngester2.Kill()
require.NoError(t, err)
// sleep for heartbeat timeout
time.Sleep(1 * time.Second)
info = tempoUtil.NewTraceInfo(time.Now(), "")
require.NoError(t, info.EmitAllBatches(c))
// query an in-memory trace
queryAndAssertTrace(t, apiClient, info)
// search an in-memory trace
util.SearchAndAssertTrace(t, apiClient, info)
// search the backend. this works b/c we're passing a start/end AND setting query ingesters within min/max to 0
now := time.Now()
util.SearchAndAssertTraceBackend(t, apiClient, info, now.Add(-20*time.Minute).Unix(), now.Unix())
// stop another ingester and confirm things fail
err = tempoIngester1.Kill()
require.NoError(t, err)
require.Error(t, info.EmitBatches(c))
})
}
}
func TestScalableSingleBinary(t *testing.T) {
s, err := e2e.NewScenario("tempo_e2e")
require.NoError(t, err)
defer s.Close()
minio := e2edb.NewMinio(9000, "tempo")
require.NotNil(t, minio)
require.NoError(t, s.StartAndWaitReady(minio))
// copy configuration file over to shared dir
require.NoError(t, util.CopyFileToSharedDir(s, configHA, "config.yaml"))
// start three scalable single binary tempos in parallel
var wg sync.WaitGroup
var tempo1, tempo2, tempo3 *e2e.HTTPService
wg.Add(3)
go func() {
tempo1 = util.NewTempoScalableSingleBinary(1)
wg.Done()
}()
go func() {
tempo2 = util.NewTempoScalableSingleBinary(2)
wg.Done()
}()
go func() {
tempo3 = util.NewTempoScalableSingleBinary(3)
wg.Done()
}()
wg.Wait()
require.NoError(t, s.StartAndWaitReady(tempo1, tempo2, tempo3))
// wait for 2 active ingesters
time.Sleep(1 * time.Second)
matchers := []*labels.Matcher{
{
Type: labels.MatchEqual,
Name: "name",
Value: "ingester",
},
{
Type: labels.MatchEqual,
Name: "state",
Value: "ACTIVE",
},
}
t.Logf("tempo1.Endpoint(): %+v", tempo1.Endpoint(3200))
require.NoError(t, tempo1.WaitSumMetricsWithOptions(e2e.Equals(3), []string{`cortex_ring_members`}, e2e.WithLabelMatchers(matchers...), e2e.WaitMissingMetrics))
require.NoError(t, tempo2.WaitSumMetricsWithOptions(e2e.Equals(3), []string{`cortex_ring_members`}, e2e.WithLabelMatchers(matchers...), e2e.WaitMissingMetrics))
require.NoError(t, tempo3.WaitSumMetricsWithOptions(e2e.Equals(3), []string{`cortex_ring_members`}, e2e.WithLabelMatchers(matchers...), e2e.WaitMissingMetrics))
c1, err := util.NewJaegerGRPCClient(tempo1.Endpoint(14250))
require.NoError(t, err)
require.NotNil(t, c1)
c2, err := util.NewJaegerGRPCClient(tempo2.Endpoint(14250))
require.NoError(t, err)
require.NotNil(t, c2)
c3, err := util.NewJaegerGRPCClient(tempo3.Endpoint(14250))
require.NoError(t, err)
require.NotNil(t, c3)
info := tempoUtil.NewTraceInfo(time.Unix(1632169410, 0), "")
require.NoError(t, info.EmitBatches(c1))
expected, err := info.ConstructTraceFromEpoch()
require.NoError(t, err)
// test metrics
require.NoError(t, tempo1.WaitSumMetrics(e2e.Equals(spanCount(expected)), "tempo_distributor_spans_received_total"))
// wait trace_idle_time and ensure trace is created in ingester
time.Sleep(1 * time.Second)
require.NoError(t, tempo1.WaitSumMetricsWithOptions(e2e.Less(3), []string{"tempo_ingester_traces_created_total"}, e2e.WaitMissingMetrics))
for _, i := range []*e2e.HTTPService{tempo1, tempo2, tempo3} {
callFlush(t, i)
require.NoError(t, i.WaitSumMetrics(e2e.Equals(1), "tempo_ingester_blocks_flushed_total"))
callIngesterRing(t, i)
callCompactorRing(t, i)
callStatus(t, i)
}
apiClient1 := tempoUtil.NewClient("http://"+tempo1.Endpoint(3200), "")
queryAndAssertTrace(t, apiClient1, info)
err = tempo1.Kill()
require.NoError(t, err)
// Push to one of the instances that are still running.
require.NoError(t, info.EmitBatches(c2))
err = tempo2.Kill()
require.NoError(t, err)
err = tempo3.Kill()
require.NoError(t, err)
}
func makeThriftBatch() *thrift.Batch {
return makeThriftBatchWithSpanCount(1)
}
func makeThriftBatchWithSpanCount(n int) *thrift.Batch {
var spans []*thrift.Span
traceIDLow := rand.Int63()
traceIDHigh := rand.Int63()
tagValue := "y"
for i := 0; i < n; i++ {
spans = append(spans, &thrift.Span{
TraceIdLow: traceIDLow,
TraceIdHigh: traceIDHigh,
SpanId: rand.Int63(),
ParentSpanId: 0,
OperationName: "my operation",
References: nil,
Flags: 0,
StartTime: time.Now().Unix(),
Duration: 1,
Tags: []*thrift.Tag{
{
Key: "x",
VStr: &tagValue,
},
},
Logs: nil,
})
}
return &thrift.Batch{Spans: spans}
}
func callFlush(t *testing.T, ingester *e2e.HTTPService) {
fmt.Printf("Calling /flush on %s\n", ingester.Name())
res, err := e2e.DoGet("http://" + ingester.Endpoint(3200) + "/flush")
require.NoError(t, err)
require.Equal(t, 204, res.StatusCode)
}
func callIngesterRing(t *testing.T, svc *e2e.HTTPService) {
endpoint := "/ingester/ring"
fmt.Printf("Calling %s on %s\n", endpoint, svc.Name())
res, err := e2e.DoGet("http://" + svc.Endpoint(3200) + endpoint)
require.NoError(t, err)
require.Equal(t, 200, res.StatusCode)
}
func callCompactorRing(t *testing.T, svc *e2e.HTTPService) {
endpoint := "/compactor/ring"
fmt.Printf("Calling %s on %s\n", endpoint, svc.Name())
res, err := e2e.DoGet("http://" + svc.Endpoint(3200) + endpoint)
require.NoError(t, err)
require.Equal(t, 200, res.StatusCode)
}
func callStatus(t *testing.T, svc *e2e.HTTPService) {
endpoint := "/status/endpoints"
fmt.Printf("Calling %s on %s\n", endpoint, svc.Name())
res, err := e2e.DoGet("http://" + svc.Endpoint(3200) + endpoint)
require.NoError(t, err)
// body, err := ioutil.ReadAll(res.Body)
// require.NoError(t, err)
// t.Logf("body: %+v", string(body))
require.Equal(t, 200, res.StatusCode)
}
func assertEcho(t *testing.T, url string) {
res, err := e2e.DoGet(url)
require.NoError(t, err)
require.Equal(t, 200, res.StatusCode)
defer res.Body.Close()
}
func queryAndAssertTrace(t *testing.T, client *tempoUtil.Client, info *tempoUtil.TraceInfo) {
resp, err := client.QueryTrace(info.HexID())
require.NoError(t, err)
expected, err := info.ConstructTraceFromEpoch()
require.NoError(t, err)
require.True(t, equalTraces(resp, expected))
}
func equalTraces(a, b *tempopb.Trace) bool {
trace.SortTrace(a)
trace.SortTrace(b)
return reflect.DeepEqual(a, b)
}
func spanCount(a *tempopb.Trace) float64 {
count := 0
for _, batch := range a.Batches {
for _, spans := range batch.ScopeSpans {
count += len(spans.Spans)
}
}
return float64(count)
}