diff --git a/server/model/traces.go b/server/model/traces.go index f27d34a736..bd2912fe1a 100644 --- a/server/model/traces.go +++ b/server/model/traces.go @@ -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) @@ -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 } diff --git a/server/model/traces_test.go b/server/model/traces_test.go index cf29ba8ba7..e95e754aec 100644 --- a/server/model/traces_test.go +++ b/server/model/traces_test.go @@ -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" ) @@ -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) @@ -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 {