Skip to content
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

[v2][adjuster] Implement adjuster for sorting attributes and events #6389

Merged
merged 5 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions cmd/query/app/querysvc/adjuster/sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package adjuster

import (
"sort"

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
)

var _ Adjuster = (*SortAttributesAndEventsAdjuster)(nil)

// SortAttributesAndEvents creates an adjuster that standardizes trace data by sorting elements:
// - Resource attributes are sorted lexicographically by their keys.
// - Span attributes are sorted lexicographically by their keys.
// - Span events are sorted lexicographically by their names.
// - Attributes within each span event are sorted lexicographically by their keys.
func SortAttributesAndEvents() SortAttributesAndEventsAdjuster {
return SortAttributesAndEventsAdjuster{}
}

type SortAttributesAndEventsAdjuster struct{}

func (s SortAttributesAndEventsAdjuster) Adjust(traces ptrace.Traces) {
resourceSpans := traces.ResourceSpans()
for i := 0; i < resourceSpans.Len(); i++ {
rs := resourceSpans.At(i)
resource := rs.Resource()
s.sortAttributes(resource.Attributes())
scopeSpans := rs.ScopeSpans()
for j := 0; j < scopeSpans.Len(); j++ {
ss := scopeSpans.At(j)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not sort scope attributes?

Suggested change
ss := scopeSpans.At(j)
ss := scopeSpans.At(j)
s.sortAttributes(ss.Attributes())

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yurishkuro Done. And did the same for link attributes as well. We should have all collections accounted for now which should provide us with a way to deterministic hash the traces.

spans := ss.Spans()
for k := 0; k < spans.Len(); k++ {
span := spans.At(k)
s.sortAttributes(span.Attributes())
s.sortEvents(span.Events())
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yurishkuro Wanted a sanity check here. In v1, the "event" field was moved to the first position (ref: https://github.com/jaegertracing/jaeger/blob/main/model/adjuster/sort_tags_and_log_fields.go#L31). However, in v2, logs are stored as event attributes and the "event" attribute is stored as the event name (https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/pkg/translator/jaeger/jaegerproto_to_traces.go#L406).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm. Good to have first class fields.

}
}
}
}

func (SortAttributesAndEventsAdjuster) sortAttributes(attributes pcommon.Map) {
entries := make([]struct {
key string
value pcommon.Value
}, 0, attributes.Len())
attributes.Range(func(k string, v pcommon.Value) bool {
entries = append(entries, struct {
key string
value pcommon.Value
}{key: k, value: v})
return true
})
sort.Slice(entries, func(i, j int) bool {
return entries[i].key < entries[j].key
})
newAttributes := pcommon.NewMap()
for _, entry := range entries {
entry.value.CopyTo(newAttributes.PutEmpty(entry.key))
}
newAttributes.CopyTo(attributes)
}

func (s SortAttributesAndEventsAdjuster) sortEvents(events ptrace.SpanEventSlice) {
events.Sort(func(a, b ptrace.SpanEvent) bool {
return a.Name() < b.Name()
})
for i := 0; i < events.Len(); i++ {
event := events.At(i)
s.sortAttributes(event.Attributes())
}
}
75 changes: 75 additions & 0 deletions cmd/query/app/querysvc/adjuster/sort_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package adjuster

import (
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/ptrace"
)

func TestSortAttributesAndEventsAdjuster(t *testing.T) {
adjuster := SortAttributesAndEvents()
input := func() ptrace.Traces {
traces := ptrace.NewTraces()
rs := traces.ResourceSpans().AppendEmpty()

resource := rs.Resource()
resource.Attributes().PutStr("attributeZ", "keyA")
resource.Attributes().PutStr("attributeA", "keyB")
resource.Attributes().PutInt("attributeY", 1)
resource.Attributes().PutStr("attributeX", "keyC")

ss := rs.ScopeSpans().AppendEmpty()
span := ss.Spans().AppendEmpty()
span.Attributes().PutStr("attributeW", "keyD")
span.Attributes().PutStr("attributeB", "keyZ")
span.Attributes().PutInt("attributeV", 2)

event2 := span.Events().AppendEmpty()
event2.SetName("event2")
event2.Attributes().PutStr("attributeU", "keyE")
event2.Attributes().PutStr("attributeT", "keyF")

event1 := span.Events().AppendEmpty()
event1.SetName("event1")
event1.Attributes().PutStr("attributeR", "keyE")
event1.Attributes().PutStr("attributeS", "keyF")

return traces
}
expected := func() ptrace.Traces {
traces := ptrace.NewTraces()
rs := traces.ResourceSpans().AppendEmpty()

resource := rs.Resource()
resource.Attributes().PutStr("attributeA", "keyB")
resource.Attributes().PutStr("attributeX", "keyC")
resource.Attributes().PutInt("attributeY", 1)
resource.Attributes().PutStr("attributeZ", "keyA")

ss := rs.ScopeSpans().AppendEmpty()
span := ss.Spans().AppendEmpty()
span.Attributes().PutStr("attributeB", "keyZ")
span.Attributes().PutInt("attributeV", 2)
span.Attributes().PutStr("attributeW", "keyD")

event1 := span.Events().AppendEmpty()
event1.SetName("event1")
event1.Attributes().PutStr("attributeR", "keyE")
event1.Attributes().PutStr("attributeS", "keyF")

event2 := span.Events().AppendEmpty()
event2.SetName("event2")
event2.Attributes().PutStr("attributeT", "keyF")
event2.Attributes().PutStr("attributeU", "keyE")

return traces
}

i := input()
adjuster.Adjust(i)
assert.Equal(t, expected(), i)
}
Loading