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

feat(server): allow trace to be incremented in UpdateTestRun endpoint #3128

Merged
merged 10 commits into from
Sep 15, 2023
Merged
3 changes: 3 additions & 0 deletions server/http/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/kubeshop/tracetest/server/test"
"github.com/kubeshop/tracetest/server/testsuite"
"github.com/kubeshop/tracetest/server/tracedb"
"github.com/kubeshop/tracetest/server/traces"
"github.com/kubeshop/tracetest/server/variableset"
"github.com/labstack/gommon/log"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -718,6 +719,8 @@ func (c *controller) UpdateTestRun(ctx context.Context, testID string, runID int

// Prevents bad data in other fields to override correct data
existingRun.TriggerResult = run.TriggerResult
newTrace := traces.MergeTraces(existingRun.Trace, run.Trace)
existingRun.Trace = &newTrace

err = c.testRunRepository.UpdateRun(ctx, existingRun)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions server/traces/span_entitiess.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ func decodeChildren(parent *Span, children []encodedSpan, cache spanCache) ([]*S
}

func (span Span) setMetadataAttributes() Span {
if span.Attributes == nil {
span.Attributes = Attributes{}
}

span.Attributes[TracetestMetadataFieldName] = span.Name
span.Attributes[TracetestMetadataFieldType] = spanType(span.Attributes)
span.Attributes[TracetestMetadataFieldDuration] = spanDuration(span)
Expand Down
18 changes: 18 additions & 0 deletions server/traces/trace_entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ type Trace struct {
Flat map[trace.SpanID]*Span `json:"-"`
}

func MergeTraces(traces ...*Trace) Trace {
if len(traces) == 0 {
return NewTrace(id.NewRandGenerator().TraceID().String(), []Span{})
}

traceID := traces[0].ID
spans := make([]Span, 0)
for _, trace := range traces {
if trace == nil {
continue
}

spans = append(spans, trace.Spans()...)
}

return NewTrace(traceID.String(), spans)
}

func NewTrace(traceID string, spans []Span) Trace {
spanMap := make(map[string]*Span, 0)
for _, span := range spans {
Expand Down
19 changes: 19 additions & 0 deletions server/traces/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,25 @@ func TestEventsAreInjectedIntoAttributes(t *testing.T) {
assert.Equal(t, "value", events[1].Attributes["attribute2"])
}

func TestMergingZeroTraces(t *testing.T) {
trace := traces.MergeTraces()
assert.NotEmpty(t, trace.ID)
}

func TestMergingFragmentsFromSameTrace(t *testing.T) {
traceID := id.NewRandGenerator().TraceID().String()
rootSpan := newSpan("Root")
childSpan1 := newSpan("child 1", withParent(&rootSpan))
childSpan2 := newSpan("child 2", withParent(&rootSpan))

firstFragment := traces.NewTrace(traceID, []traces.Span{childSpan2})
secondFragment := traces.NewTrace(traceID, []traces.Span{rootSpan, childSpan1})

trace := traces.MergeTraces(&firstFragment, &secondFragment)
assert.NotEmpty(t, trace.ID)
assert.Len(t, trace.Flat, 3)
}

type option func(*traces.Span)

func withParent(parent *traces.Span) option {
Expand Down
Loading