-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathintegration.go
588 lines (515 loc) · 17.8 KB
/
integration.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2017 Uber Technologies, Inc.
// SPDX-License-Identifier: Apache-2.0
package integration
import (
"bytes"
"context"
"embed"
"encoding/json"
"fmt"
"os"
"regexp"
"sort"
"strings"
"testing"
"time"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
samplemodel "github.com/jaegertracing/jaeger/cmd/collector/app/sampling/model"
"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/storage/dependencystore"
"github.com/jaegertracing/jaeger/storage/samplingstore"
"github.com/jaegertracing/jaeger/storage/spanstore"
)
//go:embed fixtures
var fixtures embed.FS
// StorageIntegration holds components for storage integration test.
// The intended usage is as follows:
// - a specific storage implementation declares its own test functions
// - in those functions it instantiates and populates this struct
// - it then calls RunAll.
//
// Some implementations may declare multiple tests, with different settings,
// and RunAll() under different conditions.
type StorageIntegration struct {
SpanWriter spanstore.Writer
SpanReader spanstore.Reader
ArchiveSpanReader spanstore.Reader
ArchiveSpanWriter spanstore.Writer
DependencyWriter dependencystore.Writer
DependencyReader dependencystore.Reader
SamplingStore samplingstore.Store
Fixtures []*QueryFixtures
// TODO: remove this after all storage backends return spanKind from GetOperations
GetOperationsMissingSpanKind bool
// TODO: remove this after all storage backends return Source column from GetDependencies
GetDependenciesReturnsSource bool
// Skip Archive Test if not supported by the storage backend
SkipArchiveTest bool
// List of tests which has to be skipped, it can be regex too.
SkipList []string
// CleanUp() should ensure that the storage backend is clean before another test.
// called either before or after each test, and should be idempotent
CleanUp func(t *testing.T)
}
// === SpanStore Integration Tests ===
// QueryFixtures and TraceFixtures are under ./fixtures/queries.json and ./fixtures/traces/*.json respectively.
// Each query fixture includes:
// - Caption: describes the query we are testing
// - Query: the query we are testing
// - ExpectedFixture: the trace fixture that we want back from these queries.
// Queries are not necessarily numbered, but since each query requires a service name,
// the service name is formatted "query##-service".
type QueryFixtures struct {
Caption string
Query *spanstore.TraceQueryParameters
ExpectedFixtures []string
}
func (s *StorageIntegration) cleanUp(t *testing.T) {
require.NotNil(t, s.CleanUp, "CleanUp function must be provided")
s.CleanUp(t)
}
func SkipUnlessEnv(t *testing.T, storage ...string) {
env := os.Getenv("STORAGE")
for _, s := range storage {
if env == s {
return
}
}
t.Skipf("This test requires environment variable STORAGE=%s", strings.Join(storage, "|"))
}
var CassandraSkippedTests = []string{
"Tags_+_Operation_name_+_Duration_range",
"Tags_+_Duration_range",
"Tags_+_Operation_name_+_max_Duration",
"Tags_+_max_Duration",
"Operation_name_+_Duration_range",
"Duration_range",
"max_Duration",
"Multiple_Traces",
}
func (s *StorageIntegration) skipIfNeeded(t *testing.T) {
for _, pat := range s.SkipList {
escapedPat := regexp.QuoteMeta(pat)
ok, err := regexp.MatchString(escapedPat, t.Name())
require.NoError(t, err)
if ok {
t.Skip()
return
}
}
}
func (*StorageIntegration) waitForCondition(t *testing.T, predicate func(t *testing.T) bool) bool {
const iterations = 100 // Will wait at most 100 seconds.
for i := 0; i < iterations; i++ {
if predicate(t) {
return true
}
t.Logf("Waiting for storage backend to update documents, iteration %d out of %d", i+1, iterations)
time.Sleep(time.Second)
}
return predicate(t)
}
func (s *StorageIntegration) testGetServices(t *testing.T) {
s.skipIfNeeded(t)
defer s.cleanUp(t)
expected := []string{"example-service-1", "example-service-2", "example-service-3"}
s.loadParseAndWriteExampleTrace(t)
var actual []string
found := s.waitForCondition(t, func(t *testing.T) bool {
var err error
actual, err = s.SpanReader.GetServices(context.Background())
if err != nil {
t.Log(err)
return false
}
sort.Strings(actual)
t.Logf("Retrieved services: %v", actual)
if len(actual) > len(expected) {
// If the storage backend returns more services than expected, let's log traces for those
t.Log("🛑 Found unexpected services!")
for _, service := range actual {
traces, err := s.SpanReader.FindTraces(context.Background(), &spanstore.TraceQueryParameters{
ServiceName: service,
})
if err != nil {
t.Log(err)
continue
}
for _, trace := range traces {
for _, span := range trace.Spans {
t.Logf("span: Service: %s, TraceID: %s, Operation: %s", service, span.TraceID, span.OperationName)
}
}
}
}
return assert.ObjectsAreEqualValues(expected, actual)
})
if !assert.True(t, found) {
t.Log("\t Expected:", expected)
t.Log("\t Actual :", actual)
}
}
func (s *StorageIntegration) testArchiveTrace(t *testing.T) {
s.skipIfNeeded(t)
if s.SkipArchiveTest {
t.Skip("Skipping ArchiveTrace test because archive reader or writer is nil")
}
defer s.cleanUp(t)
tID := model.NewTraceID(uint64(11), uint64(22))
expected := &model.Span{
OperationName: "archive_span",
StartTime: time.Now().Add(-time.Hour * 72 * 5).Truncate(time.Microsecond),
TraceID: tID,
SpanID: model.NewSpanID(55),
References: []model.SpanRef{},
Process: model.NewProcess("archived_service", model.KeyValues{}),
}
require.NoError(t, s.ArchiveSpanWriter.WriteSpan(context.Background(), expected))
var actual *model.Trace
found := s.waitForCondition(t, func(_ *testing.T) bool {
var err error
actual, err = s.ArchiveSpanReader.GetTrace(context.Background(), spanstore.GetTraceParameters{TraceID: tID})
return err == nil && len(actual.Spans) == 1
})
require.True(t, found)
CompareTraces(t, &model.Trace{Spans: []*model.Span{expected}}, actual)
}
func (s *StorageIntegration) testGetLargeSpan(t *testing.T) {
s.skipIfNeeded(t)
defer s.cleanUp(t)
t.Log("Testing Large Trace over 10K with duplicate IDs...")
expected := s.writeLargeTraceWithDuplicateSpanIds(t)
expectedTraceID := expected.Spans[0].TraceID
var actual *model.Trace
found := s.waitForCondition(t, func(_ *testing.T) bool {
var err error
actual, err = s.SpanReader.GetTrace(context.Background(), spanstore.GetTraceParameters{TraceID: expectedTraceID})
return err == nil && len(actual.Spans) >= len(expected.Spans)
})
if !assert.True(t, found) {
CompareTraces(t, expected, actual)
return
}
duplicateCount := 0
seenIDs := make(map[model.SpanID]int)
for _, span := range actual.Spans {
seenIDs[span.SpanID]++
if seenIDs[span.SpanID] > 1 {
duplicateCount++
}
}
assert.Positive(t, duplicateCount, "Duplicate SpanIDs should be present in the trace")
}
func (s *StorageIntegration) testGetOperations(t *testing.T) {
s.skipIfNeeded(t)
defer s.cleanUp(t)
var expected []spanstore.Operation
if s.GetOperationsMissingSpanKind {
expected = []spanstore.Operation{
{Name: "example-operation-1"},
{Name: "example-operation-3"},
{Name: "example-operation-4"},
}
} else {
expected = []spanstore.Operation{
{Name: "example-operation-1", SpanKind: "unspecified"},
{Name: "example-operation-3", SpanKind: "server"},
{Name: "example-operation-4", SpanKind: "client"},
}
}
s.loadParseAndWriteExampleTrace(t)
var actual []spanstore.Operation
found := s.waitForCondition(t, func(t *testing.T) bool {
var err error
actual, err = s.SpanReader.GetOperations(context.Background(),
spanstore.OperationQueryParameters{ServiceName: "example-service-1"})
if err != nil {
t.Log(err)
return false
}
sort.Slice(actual, func(i, j int) bool {
return actual[i].Name < actual[j].Name
})
t.Logf("Retrieved operations: %v", actual)
return assert.ObjectsAreEqualValues(expected, actual)
})
if !assert.True(t, found) {
t.Log("\t Expected:", expected)
t.Log("\t Actual :", actual)
}
}
func (s *StorageIntegration) testGetTrace(t *testing.T) {
s.skipIfNeeded(t)
defer s.cleanUp(t)
expected := s.loadParseAndWriteExampleTrace(t)
expectedTraceID := expected.Spans[0].TraceID
var actual *model.Trace
found := s.waitForCondition(t, func(t *testing.T) bool {
var err error
actual, err = s.SpanReader.GetTrace(context.Background(), spanstore.GetTraceParameters{TraceID: expectedTraceID})
if err != nil {
t.Log(err)
}
return err == nil && len(actual.Spans) == len(expected.Spans)
})
if !assert.True(t, found) {
CompareTraces(t, expected, actual)
}
t.Run("NotFound error", func(t *testing.T) {
fakeTraceID := model.TraceID{High: 0, Low: 1}
trace, err := s.SpanReader.GetTrace(context.Background(), spanstore.GetTraceParameters{TraceID: fakeTraceID})
assert.Equal(t, spanstore.ErrTraceNotFound, err)
assert.Nil(t, trace)
})
}
func (s *StorageIntegration) testFindTraces(t *testing.T) {
s.skipIfNeeded(t)
defer s.cleanUp(t)
// Note: all cases include ServiceName + StartTime range
s.Fixtures = append(s.Fixtures, LoadAndParseQueryTestCases(t, "fixtures/queries.json")...)
// Each query test case only specifies matching traces, but does not provide counterexamples.
// To improve coverage we get all possible traces and store all of them before running queries.
allTraceFixtures := make(map[string]*model.Trace)
expectedTracesPerTestCase := make([][]*model.Trace, 0, len(s.Fixtures))
for _, queryTestCase := range s.Fixtures {
var expected []*model.Trace
for _, traceFixture := range queryTestCase.ExpectedFixtures {
trace, ok := allTraceFixtures[traceFixture]
if !ok {
trace = s.getTraceFixture(t, traceFixture)
s.writeTrace(t, trace)
allTraceFixtures[traceFixture] = trace
}
expected = append(expected, trace)
}
expectedTracesPerTestCase = append(expectedTracesPerTestCase, expected)
}
for i, queryTestCase := range s.Fixtures {
t.Run(queryTestCase.Caption, func(t *testing.T) {
s.skipIfNeeded(t)
expected := expectedTracesPerTestCase[i]
actual := s.findTracesByQuery(t, queryTestCase.Query, expected)
CompareSliceOfTraces(t, expected, actual)
})
}
}
func (s *StorageIntegration) findTracesByQuery(t *testing.T, query *spanstore.TraceQueryParameters, expected []*model.Trace) []*model.Trace {
var traces []*model.Trace
found := s.waitForCondition(t, func(t *testing.T) bool {
var err error
traces, err = s.SpanReader.FindTraces(context.Background(), query)
if err != nil {
t.Log(err)
return false
}
if len(expected) != len(traces) {
t.Logf("Expecting certain number of traces: expected: %d, actual: %d", len(expected), len(traces))
return false
}
if spanCount(expected) != spanCount(traces) {
t.Logf("Excepting certain number of spans: expected: %d, actual: %d", spanCount(expected), spanCount(traces))
return false
}
return true
})
require.True(t, found)
return traces
}
func (s *StorageIntegration) writeTrace(t *testing.T, trace *model.Trace) {
t.Logf("%-23s Writing trace with %d spans", time.Now().Format("2006-01-02 15:04:05.999"), len(trace.Spans))
for _, span := range trace.Spans {
err := s.SpanWriter.WriteSpan(context.Background(), span)
require.NoError(t, err, "Not expecting error when writing trace to storage")
}
}
func (s *StorageIntegration) loadParseAndWriteExampleTrace(t *testing.T) *model.Trace {
trace := s.getTraceFixture(t, "example_trace")
s.writeTrace(t, trace)
return trace
}
func (s *StorageIntegration) writeLargeTraceWithDuplicateSpanIds(t *testing.T) *model.Trace {
trace := s.getTraceFixture(t, "example_trace")
repeatedSpan := trace.Spans[0]
trace.Spans = make([]*model.Span, 0, 10008)
for i := 0; i < 10008; i++ {
newSpan := new(model.Span)
*newSpan = *repeatedSpan
switch {
case i%100 == 0:
newSpan.SpanID = repeatedSpan.SpanID
default:
newSpan.SpanID = model.SpanID(i)
}
newSpan.StartTime = newSpan.StartTime.Add(time.Second * time.Duration(i+1))
trace.Spans = append(trace.Spans, newSpan)
}
s.writeTrace(t, trace)
return trace
}
func (*StorageIntegration) getTraceFixture(t *testing.T, fixture string) *model.Trace {
fileName := fmt.Sprintf("fixtures/traces/%s.json", fixture)
return getTraceFixtureExact(t, fileName)
}
func getTraceFixtureExact(t *testing.T, fileName string) *model.Trace {
var trace model.Trace
loadAndParseJSONPB(t, fileName, &trace)
return &trace
}
func loadAndParseJSONPB(t *testing.T, path string, object proto.Message) {
// #nosec
inStr, err := fixtures.ReadFile(path)
require.NoError(t, err, "Not expecting error when loading fixture %s", path)
err = jsonpb.Unmarshal(bytes.NewReader(correctTime(inStr)), object)
require.NoError(t, err, "Not expecting error when unmarshaling fixture %s", path)
}
// LoadAndParseQueryTestCases loads and parses query test cases
func LoadAndParseQueryTestCases(t *testing.T, queriesFile string) []*QueryFixtures {
var queries []*QueryFixtures
loadAndParseJSON(t, queriesFile, &queries)
return queries
}
func loadAndParseJSON(t *testing.T, path string, object any) {
// #nosec
inStr, err := fixtures.ReadFile(path)
require.NoError(t, err, "Not expecting error when loading fixture %s", path)
err = json.Unmarshal(correctTime(inStr), object)
require.NoError(t, err, "Not expecting error when unmarshaling fixture %s", path)
}
// required, because we want to only query on recent traces, so we replace all the dates with recent dates.
func correctTime(jsonData []byte) []byte {
jsonString := string(jsonData)
now := time.Now().UTC()
yesterday := now.AddDate(0, 0, -1).Format("2006-01-02")
twoDaysAgo := now.AddDate(0, 0, -2).Format("2006-01-02")
retString := strings.ReplaceAll(jsonString, "2017-01-26", yesterday)
retString = strings.ReplaceAll(retString, "2017-01-25", twoDaysAgo)
return []byte(retString)
}
func spanCount(traces []*model.Trace) int {
var count int
for _, trace := range traces {
count += len(trace.Spans)
}
return count
}
// === DependencyStore Integration Tests ===
func (s *StorageIntegration) testGetDependencies(t *testing.T) {
if s.DependencyReader == nil || s.DependencyWriter == nil {
t.Skipf("Skipping GetDependencies test because dependency reader or writer is nil")
return
}
s.skipIfNeeded(t)
defer s.cleanUp(t)
source := model.JaegerDependencyLinkSource
if !s.GetDependenciesReturnsSource {
source = ""
}
expected := []model.DependencyLink{
{
Parent: "hello",
Child: "world",
CallCount: uint64(1),
Source: source,
},
{
Parent: "world",
Child: "hello",
CallCount: uint64(3),
Source: source,
},
}
require.NoError(t, s.DependencyWriter.WriteDependencies(time.Now(), expected))
var actual []model.DependencyLink
found := s.waitForCondition(t, func(t *testing.T) bool {
var err error
actual, err = s.DependencyReader.GetDependencies(context.Background(), time.Now(), 5*time.Minute)
if err != nil {
t.Log(err)
return false
}
sort.Slice(actual, func(i, j int) bool {
return actual[i].Parent < actual[j].Parent
})
return assert.ObjectsAreEqualValues(expected, actual)
})
if !assert.True(t, found) {
t.Log("\t Expected:", expected)
t.Log("\t Actual :", actual)
}
}
// === Sampling Store Integration Tests ===
func (s *StorageIntegration) testGetThroughput(t *testing.T) {
s.skipIfNeeded(t)
if s.SamplingStore == nil {
t.Skip("Skipping GetThroughput test because sampling store is nil")
return
}
defer s.cleanUp(t)
start := time.Now()
s.insertThroughput(t)
expected := 2
var actual []*samplemodel.Throughput
_ = s.waitForCondition(t, func(t *testing.T) bool {
var err error
actual, err = s.SamplingStore.GetThroughput(start, start.Add(time.Second*time.Duration(10)))
if err != nil {
t.Log(err)
return false
}
return assert.ObjectsAreEqualValues(expected, len(actual))
})
assert.Len(t, actual, expected)
}
func (s *StorageIntegration) testGetLatestProbability(t *testing.T) {
s.skipIfNeeded(t)
if s.SamplingStore == nil {
t.Skip("Skipping GetLatestProbability test because sampling store is nil")
return
}
defer s.cleanUp(t)
s.SamplingStore.InsertProbabilitiesAndQPS("newhostname1", samplemodel.ServiceOperationProbabilities{"new-srv3": {"op": 0.123}}, samplemodel.ServiceOperationQPS{"new-srv2": {"op": 11}})
s.SamplingStore.InsertProbabilitiesAndQPS("dell11eg843d", samplemodel.ServiceOperationProbabilities{"new-srv": {"op": 0.1}}, samplemodel.ServiceOperationQPS{"new-srv": {"op": 4}})
expected := samplemodel.ServiceOperationProbabilities{"new-srv": {"op": 0.1}}
var actual samplemodel.ServiceOperationProbabilities
found := s.waitForCondition(t, func(t *testing.T) bool {
var err error
actual, err = s.SamplingStore.GetLatestProbabilities()
if err != nil {
t.Log(err)
return false
}
return assert.ObjectsAreEqualValues(expected, actual)
})
if !assert.True(t, found) {
t.Log("\t Expected:", expected)
t.Log("\t Actual :", actual)
}
}
func (s *StorageIntegration) insertThroughput(t *testing.T) {
throughputs := []*samplemodel.Throughput{
{Service: "my-svc", Operation: "op"},
{Service: "our-svc", Operation: "op2"},
}
err := s.SamplingStore.InsertThroughput(throughputs)
require.NoError(t, err)
}
// RunAll runs all integration tests
func (s *StorageIntegration) RunAll(t *testing.T) {
s.RunSpanStoreTests(t)
t.Run("ArchiveTrace", s.testArchiveTrace)
t.Run("GetDependencies", s.testGetDependencies)
t.Run("GetThroughput", s.testGetThroughput)
t.Run("GetLatestProbability", s.testGetLatestProbability)
}
// RunTestSpanstore runs only span related integration tests
func (s *StorageIntegration) RunSpanStoreTests(t *testing.T) {
t.Run("GetServices", s.testGetServices)
t.Run("GetOperations", s.testGetOperations)
t.Run("GetTrace", s.testGetTrace)
t.Run("GetLargeSpans", s.testGetLargeSpan)
t.Run("FindTraces", s.testFindTraces)
}