-
Notifications
You must be signed in to change notification settings - Fork 2.6k
[feat] Deduplicate spans based upon their hashcode #6009
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
Merged
Merged
Changes from 13 commits
Commits
Show all changes
19 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 d0dcc19
SortTagsAndLogFields
cdanis 824f56c
Update model/adjuster/span_hash_deduper_test.go
cdanis d525b2b
Update model/adjuster/span_hash_deduper.go
cdanis 6a31385
when hash deduping keep only first span
cdanis b11e5d3
Merge branch 'main' into dupe-dedupe
yurishkuro 128480a
Update model/adjuster/sort_tags_and_log_fields.go
cdanis 3dcc05c
add additional test
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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. | ||
cdanis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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) | ||
cdanis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for _, span := range d.trace.Spans { | ||
hash, _ := model.HashCode(span) | ||
yurishkuro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 hidden or 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. | ||
cdanis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.