Skip to content

Commit

Permalink
dedupe-spans
Browse files Browse the repository at this point in the history
Signed-off-by: Yuri Shkuro <[email protected]>
  • Loading branch information
yurishkuro committed May 19, 2024
1 parent b3a750b commit 4adedea
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion plugin/storage/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (s *StorageIntegration) testGetLargeSpan(t *testing.T) {
found := s.waitForCondition(t, func(t *testing.T) bool {
var err error
actual, err = s.SpanReader.GetTrace(context.Background(), expectedTraceID)
return err == nil && len(actual.Spans) == len(expected.Spans)
return err == nil && len(actual.Spans) >= len(expected.Spans)
})
if !assert.True(t, found) {
CompareTraces(t, expected, actual)
Expand Down
22 changes: 22 additions & 0 deletions plugin/storage/integration/trace_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ func CompareSliceOfTraces(t *testing.T, expected []*model.Trace, actual []*model
}
}

// trace.Spans may contain spans with the same SpanID. Remove duplicates
// and keep the first one. Use a map to keep track of the spans we've seen.
func dedupeSpans(trace *model.Trace) {
seen := make(map[model.SpanID]bool)
var newSpans []*model.Span
for _, span := range trace.Spans {
if !seen[span.SpanID] {
seen[span.SpanID] = true
newSpans = append(newSpans, span)
}
}
trace.Spans = newSpans
}

// CompareTraces compares two traces
func CompareTraces(t *testing.T, expected *model.Trace, actual *model.Trace) {
if expected.Spans == nil {
Expand All @@ -55,6 +69,14 @@ func CompareTraces(t *testing.T, expected *model.Trace, actual *model.Trace) {
}
require.NotNil(t, actual)
require.NotNil(t, actual.Spans)

// some storage implementation may retry writing of spans and end up with duplicates.
countBefore := len(actual.Spans)
dedupeSpans(actual)
if countAfter := len(actual.Spans); countAfter != countBefore {
t.Logf("Removed spans with duplicate span IDs; before=%d, after=%d", countBefore, countAfter)
}

model.SortTrace(expected)
model.SortTrace(actual)
checkSize(t, expected, actual)
Expand Down
30 changes: 30 additions & 0 deletions plugin/storage/integration/trace_compare_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package integration

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/jaegertracing/jaeger/model"
)

func TestDedupeSpans(t *testing.T) {
trace := &model.Trace{
Spans: []*model.Span{
{
SpanID: 1,
},
{
SpanID: 1,
},
{
SpanID: 2,
},
},
}
dedupeSpans(trace)
assert.Len(t, trace.Spans, 2)
}

0 comments on commit 4adedea

Please sign in to comment.