Skip to content

Commit

Permalink
fix: nil parent when multi root trace (#2599)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathnogueira authored May 26, 2023
1 parent 0bcfa3a commit c1af6b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
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

0 comments on commit c1af6b5

Please sign in to comment.