-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[bugfix] Deduplicate spans based upon their hashcode #6009
Open
cdanis
wants to merge
12
commits into
jaegertracing:main
Choose a base branch
from
cdanis:dupe-dedupe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−19
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
88ecb83
clarify name of SpanIDDeduper
cdanis d5bdf14
rename zipkin-specific method as well
cdanis fdc22ca
fix comment
cdanis e5dc418
add DedupeSpansByID
cdanis ae4c914
add comment and test DedupeBySpanID
cdanis 18a9bab
add DedupeBySpanID to default adjusters
cdanis e847055
tweak zipkin adjuster name & move to new file
cdanis 5ee481b
fix up span id dedupe testing
cdanis ec81a10
More testing
cdanis 22cccca
fix flaky tests
cdanis 97eb7b5
rename
cdanis 3aeb4e5
group and dedupe by hashcode
cdanis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2019 The Jaeger Authors. | ||
// Copyright (c) 2017 Uber Technologies, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package adjuster | ||
|
||
import ( | ||
"github.com/jaegertracing/jaeger/model" | ||
) | ||
|
||
// DedupeBySpanHash returns an adjuster that removes all but one span with the same hashcode. | ||
// This is useful for when spans are duplicated in archival storage, as happens with | ||
// ElasticSearch archival. | ||
func DedupeBySpanHash() Adjuster { | ||
return Func(func(trace *model.Trace) (*model.Trace, error) { | ||
deduper := &spanHashDeduper{trace: trace} | ||
deduper.groupSpansByHash() | ||
deduper.dedupeSpansByHash() | ||
return deduper.trace, nil | ||
}) | ||
} | ||
|
||
type spanHashDeduper struct { | ||
trace *model.Trace | ||
spansByHash map[uint64][]*model.Span | ||
} | ||
|
||
func (d *spanHashDeduper) groupSpansByHash() { | ||
spansByHash := make(map[uint64][]*model.Span) | ||
for _, span := range d.trace.Spans { | ||
hash, _ := model.HashCode(span) | ||
if spans, ok := spansByHash[hash]; ok { | ||
spansByHash[hash] = append(spans, span) | ||
} else { | ||
spansByHash[hash] = []*model.Span{span} | ||
} | ||
} | ||
d.spansByHash = spansByHash | ||
} | ||
|
||
func (d *spanHashDeduper) dedupeSpansByHash() { | ||
d.trace.Spans = nil | ||
for _, spans := range d.spansByHash { | ||
d.trace.Spans = append(d.trace.Spans, spans[0]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright (c) 2019 The Jaeger Authors. | ||
// Copyright (c) 2017 Uber Technologies, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package adjuster | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/otel/trace" | ||
|
||
"github.com/jaegertracing/jaeger/model" | ||
) | ||
|
||
func newDuplicatedSpansTrace() *model.Trace { | ||
traceID := model.NewTraceID(0, 42) | ||
return &model.Trace{ | ||
Spans: []*model.Span{ | ||
{ | ||
TraceID: traceID, | ||
SpanID: clientSpanID, | ||
Tags: model.KeyValues{ | ||
// span.kind = server | ||
model.String(keySpanKind, trace.SpanKindServer.String()), | ||
}, | ||
}, | ||
{ | ||
TraceID: traceID, | ||
SpanID: clientSpanID, // shared span ID | ||
Tags: model.KeyValues{ | ||
// span.kind = server | ||
model.String(keySpanKind, trace.SpanKindServer.String()), | ||
}, | ||
}, | ||
{ | ||
// some other span, child of server span | ||
TraceID: traceID, | ||
SpanID: anotherSpanID, | ||
References: []model.SpanRef{model.NewChildOfRef(traceID, clientSpanID)}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func newUniqueSpansTrace() *model.Trace { | ||
traceID := model.NewTraceID(0, 42) | ||
return &model.Trace{ | ||
Spans: []*model.Span{ | ||
{ | ||
TraceID: traceID, | ||
SpanID: anotherSpanID, | ||
Tags: model.KeyValues{ | ||
// span.kind = server | ||
model.String(keySpanKind, trace.SpanKindServer.String()), | ||
}, | ||
}, | ||
{ | ||
TraceID: traceID, | ||
SpanID: anotherSpanID, // same ID as before, but different metadata | ||
References: []model.SpanRef{model.NewChildOfRef(traceID, clientSpanID)}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func getSpanIDs(spans []*model.Span) []int { | ||
ids := make([]int, len(spans)) | ||
for i, span := range spans { | ||
ids[i] = int(span.SpanID) | ||
} | ||
return ids | ||
} | ||
|
||
func TestDedupeBySpanHashTriggers(t *testing.T) { | ||
trace := newDuplicatedSpansTrace() | ||
deduper := DedupeBySpanHash() | ||
trace, err := deduper.Adjust(trace) | ||
require.NoError(t, err) | ||
|
||
assert.Len(t, trace.Spans, 2, "should dedupe spans") | ||
|
||
ids := getSpanIDs(trace.Spans) | ||
assert.ElementsMatch(t, []int{int(clientSpanID), int(anotherSpanID)}, ids, "should keep unique span IDs") | ||
} | ||
|
||
func TestDedupeBySpanHashNotTriggered(t *testing.T) { | ||
trace := newUniqueSpansTrace() | ||
deduper := DedupeBySpanHash() | ||
trace, err := deduper.Adjust(trace) | ||
require.NoError(t, err) | ||
|
||
assert.Len(t, trace.Spans, 2, "should not dedupe spans") | ||
|
||
ids := getSpanIDs(trace.Spans) | ||
assert.ElementsMatch(t, []int{int(anotherSpanID), int(anotherSpanID)}, ids, "should keep unique span IDs") | ||
assert.NotEqual(t, trace.Spans[0], trace.Spans[1], "should keep unique hashcodes") | ||
} | ||
|
||
func TestDedupeBySpanHashEmpty(t *testing.T) { | ||
trace := &model.Trace{} | ||
deduper := DedupeBySpanHash() | ||
trace, err := deduper.Adjust(trace) | ||
require.NoError(t, err) | ||
|
||
assert.Empty(t, trace.Spans, "should be empty") | ||
} | ||
|
||
func TestDedupeBySpanHashManyManySpans(t *testing.T) { | ||
traceID := model.NewTraceID(0, 42) | ||
spans := make([]*model.Span, 0, 100) | ||
const distinctSpanIDs = 10 | ||
for i := 0; i < 100; i++ { | ||
spans = append(spans, &model.Span{ | ||
TraceID: traceID, | ||
SpanID: model.SpanID(i % distinctSpanIDs), | ||
}) | ||
} | ||
trace := &model.Trace{Spans: spans} | ||
deduper := DedupeBySpanHash() | ||
trace, err := deduper.Adjust(trace) | ||
require.NoError(t, err) | ||
|
||
assert.Len(t, trace.Spans, distinctSpanIDs, "should dedupe spans") | ||
|
||
ids := getSpanIDs(trace.Spans) | ||
assert.ElementsMatch(t, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, ids, "should keep unique span IDs") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recall there was some issue in the past with hash function requiring proper ordering of tags or something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is still the case:
You can only compare hashes if the tags are properly sorted. Surprisingly, we don't have an explicit adjuster for that, but some of that sorting happens in ip_tag.go and sort_log_fields.go adjusters. Perhaps it would make sense to pull the sorting explicitly into SortTagsAndLogs adjuster and makes sure it is called before
DedupeBySpanHash
adjuster.