Skip to content

[jaeger][v2] implement span links adjuster to operate on otlp data model #6354

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 4 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
50 changes: 50 additions & 0 deletions cmd/query/app/querysvc/adjuster/spanlinks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package adjuster

import (
"go.opentelemetry.io/collector/pdata/ptrace"
)

// SpanLinks creates an adjuster that removes span links with empty trace IDs.
func SpanLinks() Adjuster {
return Func(func(traces ptrace.Traces) (ptrace.Traces, error) {
adjuster := linksAdjuster{}
resourceSpans := traces.ResourceSpans()
for i := 0; i < resourceSpans.Len(); i++ {
rs := resourceSpans.At(i)
scopeSpans := rs.ScopeSpans()
for j := 0; j < scopeSpans.Len(); j++ {
ss := scopeSpans.At(j)
spans := ss.Spans()
for k := 0; k < spans.Len(); k++ {
span := spans.At(k)
adjuster.adjust(span)
}
}
}
return traces, nil
})
}

type linksAdjuster struct{}

// adjust removes invalid links from a span.
func (l linksAdjuster) adjust(span ptrace.Span) {
links := span.Links()
validLinks := ptrace.NewSpanLinkSlice()
for i := 0; i < links.Len(); i++ {
link := links.At(i)
if l.valid(link) {
newLink := validLinks.AppendEmpty()
link.CopyTo(newLink)
}
}
validLinks.CopyTo(span.Links())
}

// valid checks if a span link's TraceID is not empty.
func (linksAdjuster) valid(link ptrace.SpanLink) bool {
return !link.TraceID().IsEmpty()
}
39 changes: 39 additions & 0 deletions cmd/query/app/querysvc/adjuster/spanlinks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package adjuster

import (
"testing"

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

func TestLinksAdjuster(t *testing.T) {
trace := ptrace.NewTraces()
resourceSpans := trace.ResourceSpans().AppendEmpty()
scopeSpans := resourceSpans.ScopeSpans().AppendEmpty()

// span with no links
scopeSpans.Spans().AppendEmpty()

// span with empty traceID link
spanA := scopeSpans.Spans().AppendEmpty()
spanA.Links().AppendEmpty().SetTraceID(pcommon.NewTraceIDEmpty())

// span with 2 non-empty traceID links and 1 empty (or zero) traceID link
spanB := scopeSpans.Spans().AppendEmpty()
spanB.Links().AppendEmpty().SetTraceID(pcommon.TraceID([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}))
spanB.Links().AppendEmpty().SetTraceID(pcommon.TraceID([]byte{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}))
spanB.Links().AppendEmpty().SetTraceID(pcommon.TraceID([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}))

trace, err := SpanLinks().Adjust(trace)
spans := trace.ResourceSpans().At(0).ScopeSpans().At(0).Spans()
require.NoError(t, err)
assert.Equal(t, 0, spans.At(0).Links().Len())
assert.Equal(t, 0, spans.At(1).Links().Len())
assert.Equal(t, 2, spans.At(2).Links().Len())
}
Loading