Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

feat(tracedetail): add waterfall api with memory optimisations (#11450)

Nikhil Soni1e326159b0e6
diff --git a/docs/api/openapi.yml b/docs/api/openapi.yml
index 69e1279818..f8a23906dc 100644
--- a/docs/api/openapi.yml
+++ b/docs/api/openapi.yml
@@ -18948,6 +18948,77 @@ paths:
summary: Get waterfall view for a trace
tags:
- tracedetail
+ /api/v4/traces/{traceID}/waterfall:
+ post:
+ deprecated: false
+ description: Returns the waterfall view of spans including all spans if total
+ spans are under a limit, a max count otherwise. Aggregations are dropped compared
+ to v3
+ operationId: GetWaterfallV4
+ parameters:
+ - in: path
+ name: traceID
+ required: true
+ schema:
+ type: string
+ requestBody:
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/SpantypesPostableWaterfall'
+ responses:
+ "200":
+ content:
+ application/json:
+ schema:
+ properties:
+ data:
+ $ref: '#/components/schemas/SpantypesGettableWaterfallTrace'
+ status:
+ type: string
+ required:
+ - status
+ - data
+ type: object
+ description: OK
+ "400":
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/RenderErrorResponse'
+ description: Bad Request
+ "401":
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/RenderErrorResponse'
+ description: Unauthorized
+ "403":
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/RenderErrorResponse'
+ description: Forbidden
+ "404":
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/RenderErrorResponse'
+ description: Not Found
+ "500":
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/RenderErrorResponse'
+ description: Internal Server Error
+ security:
+ - api_key:
+ - VIEWER
+ - tokenizer:
+ - VIEWER
+ summary: Get waterfall view for a trace
+ tags:
+ - tracedetail
/api/v5/query_range:
post:
deprecated: false
diff --git a/frontend/src/api/generated/services/sigNoz.schemas.ts b/frontend/src/api/generated/services/sigNoz.schemas.ts
index 98f5f21f02..7391ad79ad 100644
--- a/frontend/src/api/generated/services/sigNoz.schemas.ts
+++ b/frontend/src/api/generated/services/sigNoz.schemas.ts
@@ -9232,6 +9232,17 @@ export type GetWaterfall200 = {
status: string;
};
+export type GetWaterfallV4PathParameters = {
+ traceID: string;
+};
+export type GetWaterfallV4200 = {
+ data: SpantypesGettableWaterfallTraceDTO;
+ /**
+ * @type string
+ */
+ status: string;
+};
+
export type QueryRangeV5200 = {
data: Querybuildertypesv5QueryRangeResponseDTO;
/**
diff --git a/frontend/src/api/generated/services/tracedetail/index.ts b/frontend/src/api/generated/services/tracedetail/index.ts
index 39a9e54448..2bad04de15 100644
--- a/frontend/src/api/generated/services/tracedetail/index.ts
+++ b/frontend/src/api/generated/services/tracedetail/index.ts
@@ -14,6 +14,8 @@ import type {
import type {
GetWaterfall200,
GetWaterfallPathParameters,
+ GetWaterfallV4200,
+ GetWaterfallV4PathParameters,
RenderErrorResponseDTO,
SpantypesPostableWaterfallDTO,
} from '../sigNoz.schemas';
@@ -120,3 +122,102 @@ export const useGetWaterfall = <
> => {
return useMutation(getGetWaterfallMutationOptions(options));
};
+/**
+ * Returns the waterfall view of spans including all spans if total spans are under a limit, a max count otherwise. Aggregations are dropped compared to v3
+ * @summary Get waterfall view for a trace
+ */
+export const getWaterfallV4 = (
+ { traceID }: GetWaterfallV4PathParameters,
+ spantypesPostableWaterfallDTO?: BodyType<SpantypesPostableWaterfallDTO>,
+ signal?: AbortSignal,
+) => {
+ return GeneratedAPIInstance<GetWaterfallV4200>({
+ url: `/api/v4/traces/${traceID}/waterfall`,
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ data: spantypesPostableWaterfallDTO,
+ signal,
+ });
+};
+
+export const getGetWaterfallV4MutationOptions = <
+ TError = ErrorType<RenderErrorResponseDTO>,
+ TContext = unknown,
+>(options?: {
+ mutation?: UseMutationOptions<
+ Awaited<ReturnType<typeof getWaterfallV4>>,
+ TError,
+ {
+ pathParams: GetWaterfallV4PathParameters;
+ data?: BodyType<SpantypesPostableWaterfallDTO>;
+ },
+ TContext
+ >;
+}): UseMutationOptions<
+ Awaited<ReturnType<typeof getWaterfallV4>>,
+ TError,
+ {
+ pathParams: GetWaterfallV4PathParameters;
+ data?: BodyType<SpantypesPostableWaterfallDTO>;
+ },
+ TContext
+> => {
+ const mutationKey = ['getWaterfallV4'];
+ const { mutation: mutationOptions } = options
+ ? options.mutation &&
+ 'mutationKey' in options.mutation &&
+ options.mutation.mutationKey
+ ? options
+ : { ...options, mutation: { ...options.mutation, mutationKey } }
+ : { mutation: { mutationKey } };
+
+ const mutationFn: MutationFunction<
+ Awaited<ReturnType<typeof getWaterfallV4>>,
+ {
+ pathParams: GetWaterfallV4PathParameters;
+ data?: BodyType<SpantypesPostableWaterfallDTO>;
+ }
+ > = (props) => {
+ const { pathParams, data } = props ?? {};
+
+ return getWaterfallV4(pathParams, data);
+ };
+
+ return { mutationFn, ...mutationOptions };
+};
+
+export type GetWaterfallV4MutationResult = NonNullable<
+ Awaited<ReturnType<typeof getWaterfallV4>>
+>;
+export type GetWaterfallV4MutationBody =
+ | BodyType<SpantypesPostableWaterfallDTO>
+ | undefined;
+export type GetWaterfallV4MutationError = ErrorType<RenderErrorResponseDTO>;
+
+/**
+ * @summary Get waterfall view for a trace
+ */
+export const useGetWaterfallV4 = <
+ TError = ErrorType<RenderErrorResponseDTO>,
+ TContext = unknown,
+>(options?: {
+ mutation?: UseMutationOptions<
+ Awaited<ReturnType<typeof getWaterfallV4>>,
+ TError,
+ {
+ pathParams: GetWaterfallV4PathParameters;
+ data?: BodyType<SpantypesPostableWaterfallDTO>;
+ },
+ TContext
+ >;
+}): UseMutationResult<
+ Awaited<ReturnType<typeof getWaterfallV4>>,
+ TError,
+ {
+ pathParams: GetWaterfallV4PathParameters;
+ data?: BodyType<SpantypesPostableWaterfallDTO>;
+ },
+ TContext
+> => {
+ return useMutation(getGetWaterfallV4MutationOptions(options));
+};
diff --git a/pkg/apiserver/signozapiserver/tracedetail.go b/pkg/apiserver/signozapiserver/tracedetail.go
index b7c3fec10c..3f3dad7f99 100644
--- a/pkg/apiserver/signozapiserver/tracedetail.go
+++ b/pkg/apiserver/signozapiserver/tracedetail.go
@@ -29,5 +29,24 @@ func (provider *provider) addTraceDetailRoutes(router *mux.Router) error {
return err
}
+ if err := router.Handle("/api/v4/traces/{traceID}/waterfall", handler.New(
+ provider.authzMiddleware.ViewAccess(provider.traceDetailHandler.GetWaterfallV4),
+ handler.OpenAPIDef{
+ ID: "GetWaterfallV4",
+ Tags: []string{"tracedetail"},
+ Summary: "Get waterfall view for a trace",
+ Description: "Returns the waterfall view of spans including all spans if total spans are under a limit, a max count otherwise. Aggregations are dropped compared to v3",
+ Request: new(spantypes.PostableWaterfall),
+ RequestContentType: "application/json",
+ Response: new(spantypes.GettableWaterfallTrace),
+ ResponseContentType: "application/json",
+ SuccessStatusCode: http.StatusOK,
+ ErrorStatusCodes: []int{http.StatusBadRequest, http.StatusNotFound},
+ SecuritySchemes: newSecuritySchemes(types.RoleViewer),
+ },
+ )).Methods(http.MethodPost).GetError(); err != nil {
+ return err
+ }
+
return nil
}
diff --git a/pkg/modules/tracedetail/impltracedetail/handler.go b/pkg/modules/tracedetail/impltracedetail/handler.go
index ca650bc714..788de6e0a4 100644
--- a/pkg/modules/tracedetail/impltracedetail/handler.go
+++ b/pkg/modules/tracedetail/impltracedetail/handler.go
@@ -38,3 +38,24 @@ func (h *handler) GetWaterfall(rw http.ResponseWriter, r *http.Request) {
render.Success(rw, http.StatusOK, result)
}
+
+func (h *handler) GetWaterfallV4(rw http.ResponseWriter, r *http.Request) {
+ req := new(spantypes.PostableWaterfall)
+ if err := binding.JSON.BindBody(r.Body, req); err != nil {
+ render.Error(rw, err)
+ return
+ }
+
+ if err := req.Validate(); err != nil {
+ render.Error(rw, err)
+ return
+ }
+
+ result, err := h.module.GetWaterfallV4(r.Context(), mux.Vars(r)["traceID"], req.SelectedSpanID, req.UncollapsedSpans, req.Limit)
+ if err != nil {
+ render.Error(rw, err)
+ return
+ }
+
+ render.Success(rw, http.StatusOK, result)
+}
diff --git a/pkg/modules/tracedetail/impltracedetail/module.go b/pkg/modules/tracedetail/impltracedetail/module.go
index 3ff47d7492..87c57ceca3 100644
--- a/pkg/modules/tracedetail/impltracedetail/module.go
+++ b/pkg/modules/tracedetail/impltracedetail/module.go
@@ -2,6 +2,7 @@ package impltracedetail
import (
"context"
+ "time"
"github.com/SigNoz/signoz/pkg/factory"
"github.com/SigNoz/signoz/pkg/modules/tracedetail"
@@ -45,7 +46,7 @@ func (m *module) GetWaterfall(ctx context.Context, traceID string, req *spantype
return spantypes.NewGettableWaterfallTrace(waterfallTrace, selectedSpans, uncollapsedSpans, selectedAllSpans, aggregationResults), nil
}
-// getTraceData returns the waterfall cache for the given traceID with fallback on DB.
+// getTraceData fetches all spans for a trace and builds the WaterfallTrace.
func (m *module) getTraceData(ctx context.Context, traceID string) (*spantypes.WaterfallTrace, error) {
summary, err := m.store.GetTraceSummary(ctx, traceID)
if err != nil {
@@ -61,6 +62,86 @@ func (m *module) getTraceData(ctx context.Context, traceID string) (*spantypes.W
return nil, spantypes.ErrTraceNotFound
}
- traceData := spantypes.NewWaterfallTraceFromSpans(spanItems)
- return traceData, nil
+ nodes := make([]*spantypes.WaterfallSpan, len(spanItems))
+ for i := range spanItems {
+ nodes[i] = spanItems[i].ToWaterfallSpan(traceID)
+ }
+ return spantypes.NewWaterfallTraceFromSpans(nodes), nil
+}
+
+// GetWaterfallV4 is the OOM-safe V4 waterfall.
+// For large traces (NumSpans > effectiveLimit) it uses a two-step fetch:
+// minimal fields for all spans to build the tree, then full fields for the
+// visible window only. Aggregations are not returned.
+func (m *module) GetWaterfallV4(ctx context.Context, traceID string, selectedSpanID string, uncollapsedSpans []string, selectAllLimit uint) (*spantypes.GettableWaterfallTrace, error) {
+ summary, err := m.store.GetTraceSummary(ctx, traceID)
+ if err != nil {
+ return nil, err
+ }
+ effectiveLimit := min(selectAllLimit, m.config.Waterfall.MaxLimitToSelectAllSpans)
+ if summary.NumSpans > uint64(effectiveLimit) {
+ return m.getWindowedWaterfall(ctx, traceID, selectedSpanID, uncollapsedSpans, summary.Start, summary.End)
+ }
+ return m.getFullWaterfall(ctx, traceID, summary)
+}
+
+func (m *module) getFullWaterfall(ctx context.Context, traceID string, summary *spantypes.TraceSummary) (*spantypes.GettableWaterfallTrace, error) {
+ spanItems, err := m.store.GetTraceSpans(ctx, traceID, summary)
+ if err != nil {
+ return nil, err
+ }
+
+ if len(spanItems) == 0 {
+ return nil, spantypes.ErrTraceNotFound
+ }
+
+ nodes := make([]*spantypes.WaterfallSpan, len(spanItems))
+ for i := range spanItems {
+ nodes[i] = spanItems[i].ToWaterfallSpan(traceID)
+ }
+ waterfallTrace := spantypes.NewWaterfallTraceFromSpans(nodes)
+ selectedSpans := waterfallTrace.GetAllSpans()
+
+ return spantypes.NewGettableWaterfallTrace(waterfallTrace, selectedSpans, nil, true, nil), nil
+}
+
+// getWindowedWaterfall builds the waterfall tree with minimal data and then returns only a window of full spans.
+func (m *module) getWindowedWaterfall(ctx context.Context, traceID, selectedSpanID string, uncollapsedSpans []string, start, end time.Time) (*spantypes.GettableWaterfallTrace, error) {
+ // Step 1: minimal fetch → build full tree → select visible window
+ minimalSpans, err := m.store.GetMinimalSpans(ctx, traceID, start, end)
+ if err != nil {
+ return nil, err
+ }
+ if len(minimalSpans) == 0 {
+ return nil, spantypes.ErrTraceNotFound
+ }
+
+ nodes := make([]*spantypes.WaterfallSpan, len(minimalSpans))
+ for i := range minimalSpans {
+ nodes[i] = minimalSpans[i].ToWaterfallSpan(traceID)
+ }
+ waterfallTrace := spantypes.NewWaterfallTraceFromSpans(nodes)
+
+ selectedSpans, uncollapsedSpans := waterfallTrace.GetSelectedSpans(
+ uncollapsedSpans,
+ selectedSpanID,
+ m.config.Waterfall.SpanPageSize,
+ m.config.Waterfall.MaxDepthToAutoExpand,
+ )
+
+ // Step 2: full fetch for the selected window only
+ spanIDs := make([]string, len(selectedSpans))
+ for i, s := range selectedSpans {
+ spanIDs[i] = s.SpanID
+ }
+ fullSpans, err := m.store.GetTraceSpansByIDs(ctx, traceID, start, end, spanIDs)
+ if err != nil {
+ return nil, err
+ }
+
+ spantypes.EnrichSelectedSpans(selectedSpans, fullSpans)
+
+ return spantypes.NewGettableWaterfallTrace(
+ waterfallTrace, selectedSpans, uncollapsedSpans, false, nil,
+ ), nil
}
diff --git a/pkg/modules/tracedetail/impltracedetail/store.go b/pkg/modules/tracedetail/impltracedetail/store.go
index adbdb34761..de95644c2a 100644
--- a/pkg/modules/tracedetail/impltracedetail/store.go
+++ b/pkg/modules/tracedetail/impltracedetail/store.go
@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
+ "time"
sqlbuilder "github.com/huandu/go-sqlbuilder"
@@ -12,6 +13,8 @@ import (
"github.com/SigNoz/signoz/pkg/types/spantypes"
)
+const colServiceName = `resource_string_service$$$$name` // $ gets escaped so $$$$ converts to $$.
+
type traceStore struct {
telemetryStore telemetrystore.TelemetryStore
}
@@ -45,8 +48,8 @@ func (s *traceStore) GetTraceSpans(ctx context.Context, traceID string, summary
// DISTINCT ON (span_id) is ClickHouse-specific syntax not supported by sqlbuilder
query := fmt.Sprintf(`
SELECT DISTINCT ON (span_id)
- timestamp, duration_nano, span_id, trace_id, has_error, kind,
- resource_string_service$$name, name, links as references,
+ timestamp, duration_nano, span_id, has_error, kind,
+ resource_string_service$$name, name,
attributes_string, attributes_number, attributes_bool, resources_string,
events, status_message, status_code_string, kind_string, parent_span_id,
flags, is_remote, trace_state, status_code,
@@ -69,3 +72,64 @@ func (s *traceStore) GetTraceSpans(ctx context.Context, traceID string, summary
}
return spanItems, nil
}
+
+func (s *traceStore) GetMinimalSpans(ctx context.Context, traceID string, start, end time.Time) ([]spantypes.MinimalSpan, error) {
+ sb := sqlbuilder.NewSelectBuilder()
+ sb.Select(
+ "DISTINCT ON (span_id) span_id",
+ "parent_span_id", "timestamp", "duration_nano", "has_error",
+ colServiceName,
+ )
+ sb.From(fmt.Sprintf("%s.%s", spantypes.TraceDB, spantypes.TraceTable))
+ sb.Where(
+ sb.E("trace_id", traceID),
+ sb.GE("ts_bucket_start", start.Unix()-1800),
+ sb.LE("ts_bucket_start", end.Unix()),
+ )
+ sb.OrderByAsc("timestamp")
+ sb.OrderByAsc("name")
+ query, args := sb.BuildWithFlavor(sqlbuilder.ClickHouse)
+
+ var spans []spantypes.MinimalSpan
+ if err := s.telemetryStore.ClickhouseDB().Select(ctx, &spans, query, args...); err != nil {
+ return nil, errors.WrapInternalf(err, errors.CodeInternal, "error querying minimal spans")
+ }
+ return spans, nil
+}
+
+func (s *traceStore) GetTraceSpansByIDs(ctx context.Context, traceID string, start, end time.Time, spanIDs []string) ([]spantypes.StorableSpan, error) {
+ if len(spanIDs) == 0 {
+ return []spantypes.StorableSpan{}, nil
+ }
+ sb := sqlbuilder.NewSelectBuilder()
+ sb.Select(
+ "DISTINCT ON (span_id) timestamp",
+ "duration_nano", "span_id", "has_error", "kind",
+ colServiceName, "name",
+ "attributes_string", "attributes_number", "attributes_bool", "resources_string",
+ "events", "status_message", "status_code_string", "kind_string", "parent_span_id",
+ "flags", "is_remote", "trace_state", "status_code",
+ "db_name", "db_operation", "http_method", "http_url", "http_host",
+ "external_http_method", "external_http_url", "response_status_code",
+ )
+ sb.From(fmt.Sprintf("%s.%s", spantypes.TraceDB, spantypes.TraceTable))
+ ids := make([]any, len(spanIDs))
+ for i, id := range spanIDs {
+ ids[i] = id
+ }
+ sb.Where(
+ sb.E("trace_id", traceID),
+ sb.In("span_id", ids...),
+ sb.GE("ts_bucket_start", start.Unix()-1800),
+ sb.LE("ts_bucket_start", end.Unix()),
+ )
+ sb.OrderByAsc("timestamp")
+ sb.OrderByAsc("name")
+ query, args := sb.BuildWithFlavor(sqlbuilder.ClickHouse)
+
+ var spans []spantypes.StorableSpan
+ if err := s.telemetryStore.ClickhouseDB().Select(ctx, &spans, query, args...); err != nil {
+ return nil, errors.WrapInternalf(err, errors.CodeInternal, "error querying trace spans by IDs")
+ }
+ return spans, nil
+}
diff --git a/pkg/modules/tracedetail/tracedetail.go b/pkg/modules/tracedetail/tracedetail.go
index c2915d6cae..0e729274f8 100644
--- a/pkg/modules/tracedetail/tracedetail.go
+++ b/pkg/modules/tracedetail/tracedetail.go
@@ -10,9 +10,11 @@ import (
// Handler exposes HTTP handlers for trace detail APIs.
type Handler interface {
GetWaterfall(http.ResponseWriter, *http.Request)
+ GetWaterfallV4(http.ResponseWriter, *http.Request)
}
// Module defines the business logic for trace detail operations.
type Module interface {
GetWaterfall(ctx context.Context, traceID string, req *spantypes.PostableWaterfall) (*spantypes.GettableWaterfallTrace, error)
+ GetWaterfallV4(ctx context.Context, traceID string, selectedSpanID string, uncollapsedSpans []string, selectAllLimit uint) (*spantypes.GettableWaterfallTrace, error)
}
diff --git a/pkg/types/spantypes/store.go b/pkg/types/spantypes/store.go
index 520fd844ad..7b03d9ecef 100644
--- a/pkg/types/spantypes/store.go
+++ b/pkg/types/spantypes/store.go
@@ -2,6 +2,7 @@ package spantypes
import (
"context"
+ "time"
"github.com/SigNoz/signoz/pkg/valuer"
)
@@ -26,4 +27,6 @@ type SpanMapperStore interface {
type TraceStore interface {
GetTraceSummary(ctx context.Context, traceID string) (*TraceSummary, error)
GetTraceSpans(ctx context.Context, traceID string, summary *TraceSummary) ([]StorableSpan, error)
+ GetMinimalSpans(ctx context.Context, traceID string, start, end time.Time) ([]MinimalSpan, error)
+ GetTraceSpansByIDs(ctx context.Context, traceID string, start, end time.Time, spanIDs []string) ([]StorableSpan, error)
}
diff --git a/pkg/types/spantypes/waterfall_span.go b/pkg/types/spantypes/waterfall_span.go
index 37b0701f86..173675e101 100644
--- a/pkg/types/spantypes/waterfall_span.go
+++ b/pkg/types/spantypes/waterfall_span.go
@@ -103,12 +103,10 @@ type StorableSpan struct {
StartTime time.Time `ch:"timestamp"`
DurationNano uint64 `ch:"duration_nano"`
SpanID string `ch:"span_id"`
- TraceID string `ch:"trace_id"`
HasError bool `ch:"has_error"`
Kind int8 `ch:"kind"`
ServiceName string `ch:"resource_string_service$$name"`
Name string `ch:"name"`
- References string `ch:"references"`
AttributesString map[string]string `ch:"attributes_string"`
AttributesNumber map[string]float64 `ch:"attributes_number"`
AttributesBool map[string]bool `ch:"attributes_bool"`
@@ -132,6 +130,32 @@ type StorableSpan struct {
ResponseStatusCode string `ch:"response_status_code"`
}
+// MinimalSpan with only the fields needed to build the parent-child tree.
+type MinimalSpan struct {
+ SpanID string `ch:"span_id"`
+ ParentSpanID string `ch:"parent_span_id"`
+ StartTime time.Time `ch:"timestamp"`
+ DurationNano uint64 `ch:"duration_nano"`
+ HasError bool `ch:"has_error"`
+ ServiceName string `ch:"resource_string_service$$name"`
+}
+
+func (item *MinimalSpan) ToWaterfallSpan(traceID string) *WaterfallSpan {
+ return &WaterfallSpan{
+ SpanID: item.SpanID,
+ TraceID: traceID,
+ ParentSpanID: item.ParentSpanID,
+ TimeUnix: uint64(item.StartTime.UnixNano()),
+ DurationNano: item.DurationNano,
+ HasError: item.HasError,
+ ServiceName: item.ServiceName,
+ Resource: map[string]string{"service.name": item.ServiceName},
+ Children: make([]*WaterfallSpan, 0),
+ Attributes: make(map[string]any),
+ Events: make([]Event, 0),
+ }
+}
+
// NewMissingWaterfallSpan creates a synthetic placeholder span for a parent that has no recorded data.
func NewMissingWaterfallSpan(spanID, traceID string, timeUnixNano, durationNano uint64) *WaterfallSpan {
return &WaterfallSpan{
@@ -261,7 +285,7 @@ func (item *StorableSpan) UnmarshalledEvents() []Event {
return events
}
-func (item *StorableSpan) ToWaterfallSpan() *WaterfallSpan {
+func (item *StorableSpan) ToWaterfallSpan(traceID string) *WaterfallSpan {
resources := make(map[string]string)
maps.Copy(resources, item.ResourcesString)
@@ -289,7 +313,7 @@ func (item *StorableSpan) ToWaterfallSpan() *WaterfallSpan {
StatusCode: item.StatusCode,
StatusCodeString: item.StatusCodeString,
StatusMessage: item.StatusMessage,
- TraceID: item.TraceID,
+ TraceID: traceID,
TraceState: item.TraceState,
Children: make([]*WaterfallSpan, 0),
TimeUnix: uint64(item.StartTime.UnixNano()),
@@ -297,6 +321,24 @@ func (item *StorableSpan) ToWaterfallSpan() *WaterfallSpan {
}
}
+func EnrichSelectedSpans(window []*WaterfallSpan, fullSpans []StorableSpan) {
+ fullByID := make(map[string]*StorableSpan, len(fullSpans))
+ for i := range fullSpans {
+ fullByID[fullSpans[i].SpanID] = &fullSpans[i]
+ }
+ for i, ws := range window {
+ full, ok := fullByID[ws.SpanID]
+ if !ok {
+ continue // synthesized MissingSpan — keep empty shell
+ }
+ newWS := full.ToWaterfallSpan(ws.TraceID)
+ newWS.Level = ws.Level
+ newWS.HasChildren = ws.HasChildren
+ newWS.SubTreeNodeCount = ws.SubTreeNodeCount
+ window[i] = newWS
+ }
+}
+
// getSpanIndex returns the index of matched span and -1 for no match.
func getSpanIndex(spans []*WaterfallSpan, targetSpanID string) int {
for i, s := range spans {
diff --git a/pkg/types/spantypes/waterfall_trace.go b/pkg/types/spantypes/waterfall_trace.go
index e3832d7b59..e091b7d342 100644
--- a/pkg/types/spantypes/waterfall_trace.go
+++ b/pkg/types/spantypes/waterfall_trace.go
@@ -62,26 +62,24 @@ func NewWaterfallTrace(
}
}
-func NewWaterfallTraceFromSpans(spans []StorableSpan) *WaterfallTrace {
+// NewWaterfallTraceFromSpans requires WaterfallSpan nodes with only below fields:
+// SpanID, ParentSpanID, TimeUnix, DurationNano, HasError, and ServiceName.
+func NewWaterfallTraceFromSpans(nodes []*WaterfallSpan) *WaterfallTrace {
var (
startTime, endTime, totalErrorSpans uint64
- spanIDToSpanNodeMap = make(map[string]*WaterfallSpan, len(spans))
+ spanIDToSpanNodeMap = make(map[string]*WaterfallSpan, len(nodes))
traceRoots []*WaterfallSpan
hasMissingSpans bool
)
- for _, item := range spans {
- span := item.ToWaterfallSpan()
- startTimeUnixNano := uint64(item.StartTime.UnixNano())
- if startTime == 0 || startTimeUnixNano < startTime {
- startTime = startTimeUnixNano
+ for _, span := range nodes {
+ if startTime == 0 || span.TimeUnix < startTime {
+ startTime = span.TimeUnix
}
- endTime = max(endTime, startTimeUnixNano+span.DurationNano)
-
+ endTime = max(endTime, span.TimeUnix+span.DurationNano)
if span.HasError {
totalErrorSpans++
}
-
spanIDToSpanNodeMap[span.SpanID] = span
}
@@ -116,7 +114,7 @@ func NewWaterfallTraceFromSpans(spans []StorableSpan) *WaterfallTrace {
return NewWaterfallTrace(
startTime,
endTime,
- uint64(len(spans)),
+ uint64(len(nodes)),
totalErrorSpans,
spanIDToSpanNodeMap,
traceRoots,