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

fix: nil parent when multi root trace #2599

Merged
merged 1 commit into from
May 26, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions server/model/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func NewTrace(traceID string, spans []Span) Trace {
rootSpan = *rootSpans[0]
} else {
rootSpan = Span{ID: IDGen.SpanID(), Name: TemporaryRootSpanName, Attributes: make(Attributes), Children: rootSpans}
for _, child := range rootSpan.Children {
child.Parent = &rootSpan
}
}

id, _ := trace.TraceIDFromHex(traceID)
Expand Down Expand Up @@ -121,6 +124,9 @@ func replaceRoot(oldRoot, newRoot Span) Span {
if oldRoot.Name == TemporaryRootSpanName {
// Replace the temporary root with the actual root
newRoot.Children = oldRoot.Children
for _, span := range oldRoot.Children {
span.Parent = &newRoot
}
return newRoot
}

Expand Down
10 changes: 10 additions & 0 deletions server/model/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/traces"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "go.opentelemetry.io/proto/otlp/trace/v1"
)

Expand Down Expand Up @@ -110,6 +111,10 @@ func TestInjectingNewRootWhenMultipleRoots(t *testing.T) {
spans := []model.Span{root1, root1Child, root2, root2Child, root3, root3Child}
trace := model.NewTrace("trace", spans)

for _, oldRoot := range trace.RootSpan.Children {
require.NotNil(t, oldRoot.Parent)
}

newRoot := newSpan("new Root", nil)
newTrace := trace.InsertRootSpan(newRoot)

Expand All @@ -119,6 +124,11 @@ func TestInjectingNewRootWhenMultipleRoots(t *testing.T) {
assert.Equal(t, "Root 1", newTrace.RootSpan.Children[0].Name)
assert.Equal(t, "Root 2", newTrace.RootSpan.Children[1].Name)
assert.Equal(t, "Root 3", newTrace.RootSpan.Children[2].Name)

for _, oldRoot := range trace.RootSpan.Children {
require.NotNil(t, oldRoot.Parent)
assert.Equal(t, newRoot.ID.String(), oldRoot.Parent.ID.String())
}
}

func newSpan(name string, parent *model.Span) model.Span {
Expand Down