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.

Add support for custom field selection for color by feature. (#11092)

Nikhil Soni9a3e79fb54b8
diff --git a/docs/api/openapi.yml b/docs/api/openapi.yml
index 8617c30370..0fbbd951fb 100644
--- a/docs/api/openapi.yml
+++ b/docs/api/openapi.yml
@@ -4898,6 +4898,11 @@ components:
type: object
TracedetailtypesGettableWaterfallTrace:
properties:
+ aggregations:
+ items:
+ $ref: '#/components/schemas/TracedetailtypesSpanAggregationResult'
+ nullable: true
+ type: array
endTimestampMillis:
minimum: 0
type: integer
@@ -4937,6 +4942,11 @@ components:
type: object
TracedetailtypesPostableWaterfall:
properties:
+ aggregations:
+ items:
+ $ref: '#/components/schemas/TracedetailtypesSpanAggregation'
+ nullable: true
+ type: array
limit:
minimum: 0
type: integer
@@ -4948,6 +4958,32 @@ components:
nullable: true
type: array
type: object
+ TracedetailtypesSpanAggregation:
+ properties:
+ aggregation:
+ $ref: '#/components/schemas/TracedetailtypesSpanAggregationType'
+ field:
+ $ref: '#/components/schemas/TelemetrytypesTelemetryFieldKey'
+ type: object
+ TracedetailtypesSpanAggregationResult:
+ properties:
+ aggregation:
+ $ref: '#/components/schemas/TracedetailtypesSpanAggregationType'
+ field:
+ $ref: '#/components/schemas/TelemetrytypesTelemetryFieldKey'
+ value:
+ additionalProperties:
+ minimum: 0
+ type: integer
+ nullable: true
+ type: object
+ type: object
+ TracedetailtypesSpanAggregationType:
+ enum:
+ - span_count
+ - execution_time_percentage
+ - duration
+ type: string
TracedetailtypesWaterfallSpan:
properties:
attributes:
diff --git a/frontend/src/api/generated/services/sigNoz.schemas.ts b/frontend/src/api/generated/services/sigNoz.schemas.ts
index 79b90c59a0..44467be28a 100644
--- a/frontend/src/api/generated/services/sigNoz.schemas.ts
+++ b/frontend/src/api/generated/services/sigNoz.schemas.ts
@@ -5901,6 +5901,11 @@ export type TracedetailtypesGettableWaterfallTraceDTOServiceNameToTotalDurationM
{ [key: string]: number } | null;
export interface TracedetailtypesGettableWaterfallTraceDTO {
+ /**
+ * @type array
+ * @nullable true
+ */
+ aggregations?: TracedetailtypesSpanAggregationResultDTO[] | null;
/**
* @type integer
* @minimum 0
@@ -5955,6 +5960,11 @@ export interface TracedetailtypesGettableWaterfallTraceDTO {
}
export interface TracedetailtypesPostableWaterfallDTO {
+ /**
+ * @type array
+ * @nullable true
+ */
+ aggregations?: TracedetailtypesSpanAggregationDTO[] | null;
/**
* @type integer
* @minimum 0
@@ -5971,6 +5981,33 @@ export interface TracedetailtypesPostableWaterfallDTO {
uncollapsedSpans?: string[] | null;
}
+export interface TracedetailtypesSpanAggregationDTO {
+ aggregation?: TracedetailtypesSpanAggregationTypeDTO;
+ field?: TelemetrytypesTelemetryFieldKeyDTO;
+}
+
+/**
+ * @nullable
+ */
+export type TracedetailtypesSpanAggregationResultDTOValue = {
+ [key: string]: number;
+} | null;
+
+export interface TracedetailtypesSpanAggregationResultDTO {
+ aggregation?: TracedetailtypesSpanAggregationTypeDTO;
+ field?: TelemetrytypesTelemetryFieldKeyDTO;
+ /**
+ * @type object
+ * @nullable true
+ */
+ value?: TracedetailtypesSpanAggregationResultDTOValue;
+}
+
+export enum TracedetailtypesSpanAggregationTypeDTO {
+ span_count = 'span_count',
+ execution_time_percentage = 'execution_time_percentage',
+ duration = 'duration',
+}
/**
* @nullable
*/
diff --git a/pkg/modules/tracedetail/impltracedetail/handler.go b/pkg/modules/tracedetail/impltracedetail/handler.go
index af328ad424..768ac09995 100644
--- a/pkg/modules/tracedetail/impltracedetail/handler.go
+++ b/pkg/modules/tracedetail/impltracedetail/handler.go
@@ -25,6 +25,11 @@ func (h *handler) GetWaterfall(rw http.ResponseWriter, r *http.Request) {
return
}
+ if err := req.Validate(); err != nil {
+ render.Error(rw, err)
+ return
+ }
+
result, err := h.module.GetWaterfall(r.Context(), mux.Vars(r)["traceID"], req)
if err != nil {
render.Error(rw, err)
diff --git a/pkg/modules/tracedetail/impltracedetail/module.go b/pkg/modules/tracedetail/impltracedetail/module.go
index f74bbe913e..1de06107f0 100644
--- a/pkg/modules/tracedetail/impltracedetail/module.go
+++ b/pkg/modules/tracedetail/impltracedetail/module.go
@@ -37,7 +37,12 @@ func (m *module) GetWaterfall(ctx context.Context, traceID string, req *tracedet
m.config.Waterfall.MaxDepthToAutoExpand,
)
- return tracedetailtypes.NewGettableWaterfallTrace(waterfallTrace, selectedSpans, uncollapsedSpans, selectedAllSpans), nil
+ aggregationResults := make([]tracedetailtypes.SpanAggregationResult, 0, len(req.Aggregations))
+ for _, a := range req.Aggregations {
+ aggregationResults = append(aggregationResults, waterfallTrace.GetSpanAggregation(a.Aggregation, a.Field))
+ }
+
+ return tracedetailtypes.NewGettableWaterfallTrace(waterfallTrace, selectedSpans, uncollapsedSpans, selectedAllSpans, aggregationResults), nil
}
// getTraceData returns the waterfall cache for the given traceID with fallback on DB.
diff --git a/pkg/modules/tracedetail/impltracedetail/waterfall_test.go b/pkg/modules/tracedetail/impltracedetail/waterfall_test.go
index 7ddd01171a..bee3193da6 100644
--- a/pkg/modules/tracedetail/impltracedetail/waterfall_test.go
+++ b/pkg/modules/tracedetail/impltracedetail/waterfall_test.go
@@ -260,7 +260,7 @@ func TestGetSelectedSpans_MultipleRoots(t *testing.T) {
trace := getWaterfallTrace([]*tracedetailtypes.WaterfallSpan{root1, root2}, spanMap)
spans, _ := trace.GetSelectedSpans([]string{"root1", "root2"}, "root1", 500, 5)
- traceRespnose := tracedetailtypes.NewGettableWaterfallTrace(trace, spans, nil, false)
+ traceRespnose := tracedetailtypes.NewGettableWaterfallTrace(trace, spans, nil, false, nil)
assert.Equal(t, []string{"root1", "child1", "root2", "child2"}, spanIDs(spans), "root1 subtree must precede root2 subtree")
assert.Equal(t, "svc-a", traceRespnose.RootServiceName, "metadata comes from first root")
@@ -567,7 +567,7 @@ func TestGetAllSpans(t *testing.T) {
)
trace := getWaterfallTrace([]*tracedetailtypes.WaterfallSpan{root}, nil)
spans := trace.GetAllSpans()
- traceResponse := tracedetailtypes.NewGettableWaterfallTrace(trace, spans, nil, true)
+ traceResponse := tracedetailtypes.NewGettableWaterfallTrace(trace, spans, nil, true, nil)
assert.ElementsMatch(t, spanIDs(spans), []string{"root", "childA", "grandchildA", "leafA", "childB", "grandchildB", "leafB"})
assert.Equal(t, "svc", traceResponse.RootServiceName)
assert.Equal(t, "root-op", traceResponse.RootServiceEntryPoint)
diff --git a/pkg/query-service/app/clickhouseReader/reader.go b/pkg/query-service/app/clickhouseReader/reader.go
index 2dacb30809..6765e3452d 100644
--- a/pkg/query-service/app/clickhouseReader/reader.go
+++ b/pkg/query-service/app/clickhouseReader/reader.go
@@ -1154,7 +1154,13 @@ func (r *ClickHouseReader) GetFlamegraphSpansForTrace(ctx context.Context, orgID
if err != nil {
r.logger.Info("cache miss for getFlamegraphSpansForTrace", "traceID", traceID)
- searchScanResponses, err := r.GetSpansForTrace(ctx, traceID, fmt.Sprintf("SELECT timestamp, duration_nano, span_id, trace_id, has_error,links as references, resource_string_service$$name, name, events FROM %s.%s WHERE trace_id=$1 and ts_bucket_start>=$2 and ts_bucket_start<=$3 ORDER BY timestamp ASC, name ASC", r.TraceDB, r.traceTableName))
+ selectCols := "timestamp, duration_nano, span_id, trace_id, has_error, links as references, resource_string_service$$name, name, events"
+ if len(req.SelectFields) > 0 {
+ selectCols += ", attributes_string, attributes_number, attributes_bool, resources_string"
+ }
+ flamegraphQuery := fmt.Sprintf("SELECT %s FROM %s.%s WHERE trace_id=$1 and ts_bucket_start>=$2 and ts_bucket_start<=$3 ORDER BY timestamp ASC, name ASC", selectCols, r.TraceDB, r.traceTableName)
+
+ searchScanResponses, err := r.GetSpansForTrace(ctx, traceID, flamegraphQuery)
if err != nil {
return nil, err
}
@@ -1193,6 +1199,10 @@ func (r *ClickHouseReader) GetFlamegraphSpansForTrace(ctx context.Context, orgID
Children: make([]*model.FlamegraphSpan, 0),
}
+ if len(req.SelectFields) > 0 {
+ jsonItem.SetRequestedFields(item, req.SelectFields)
+ }
+
// metadata calculation
startTimeUnixNano := uint64(item.TimeUnixNano.UnixNano())
if startTime == 0 || startTimeUnixNano < startTime {
diff --git a/pkg/query-service/model/queryParams.go b/pkg/query-service/model/queryParams.go
index 56faa8032f..570c21bdd7 100644
--- a/pkg/query-service/model/queryParams.go
+++ b/pkg/query-service/model/queryParams.go
@@ -2,6 +2,8 @@ package model
import (
"time"
+
+ "github.com/SigNoz/signoz/pkg/types/telemetrytypes"
)
type InstantQueryMetricsParams struct {
@@ -337,10 +339,11 @@ type GetWaterfallSpansForTraceWithMetadataParams struct {
}
type GetFlamegraphSpansForTraceParams struct {
- SelectedSpanID string `json:"selectedSpanId"`
- Limit uint `json:"limit"`
- BoundaryStartTS uint64 `json:"boundaryStartTsMilli"`
- BoundaryEndTS uint64 `json:"boundarEndTsMilli"`
+ SelectedSpanID string `json:"selectedSpanId"`
+ Limit uint `json:"limit"`
+ BoundaryStartTS uint64 `json:"boundaryStartTsMilli"`
+ BoundaryEndTS uint64 `json:"boundarEndTsMilli"`
+ SelectFields []telemetrytypes.TelemetryFieldKey `json:"selectFields"`
}
type SpanFilterParams struct {
diff --git a/pkg/query-service/model/response.go b/pkg/query-service/model/response.go
index 9e83bca7a1..c247fa46e4 100644
--- a/pkg/query-service/model/response.go
+++ b/pkg/query-service/model/response.go
@@ -7,6 +7,7 @@ import (
"strconv"
"time"
+ "github.com/SigNoz/signoz/pkg/types/telemetrytypes"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/util/stats"
@@ -314,6 +315,31 @@ type FlamegraphSpan struct {
Events []Event `json:"event"`
References []OtelSpanRef `json:"references,omitempty"`
Children []*FlamegraphSpan `json:"children"`
+ Attributes map[string]any `json:"attributes,omitempty"`
+ Resource map[string]string `json:"resource,omitempty"`
+}
+
+// SetRequestedFields extracts the requested attribute/resource fields from item into s.
+// This can eventually support missing fieldContext by checking both
+func (s *FlamegraphSpan) SetRequestedFields(item SpanItemV2, fields []telemetrytypes.TelemetryFieldKey) {
+ for _, field := range fields {
+ switch field.FieldContext {
+ case telemetrytypes.FieldContextResource:
+ if v, ok := item.Resources_string[field.Name]; ok && v != "" {
+ if s.Resource == nil {
+ s.Resource = make(map[string]string)
+ }
+ s.Resource[field.Name] = v
+ }
+ case telemetrytypes.FieldContextAttribute:
+ if v := item.AttributeValue(field.Name); v != nil {
+ if s.Attributes == nil {
+ s.Attributes = make(map[string]any)
+ }
+ s.Attributes[field.Name] = v
+ }
+ }
+ }
}
type GetWaterfallSpansForTraceWithMetadataResponse struct {
diff --git a/pkg/query-service/model/trace.go b/pkg/query-service/model/trace.go
index ccee5a83d2..48d127ec87 100644
--- a/pkg/query-service/model/trace.go
+++ b/pkg/query-service/model/trace.go
@@ -29,3 +29,17 @@ type TraceSummary struct {
End time.Time `ch:"end"`
NumSpans uint64 `ch:"num_spans"`
}
+
+// AttributeValue looks up an attribute across string, number, and bool maps in priority order.
+func (s SpanItemV2) AttributeValue(name string) any {
+ if v, ok := s.Attributes_string[name]; ok {
+ return v
+ }
+ if v, ok := s.Attributes_number[name]; ok {
+ return v
+ }
+ if v, ok := s.Attributes_bool[name]; ok {
+ return v
+ }
+ return nil
+}
diff --git a/pkg/types/preferencetypes/name.go b/pkg/types/preferencetypes/name.go
index 38c07bb95b..5b0643ae25 100644
--- a/pkg/types/preferencetypes/name.go
+++ b/pkg/types/preferencetypes/name.go
@@ -20,6 +20,8 @@ var (
NameNavShortcuts = Name{valuer.NewString("nav_shortcuts")}
NameLastSeenChangelogVersion = Name{valuer.NewString("last_seen_changelog_version")}
NameSpanDetailsPinnedAttributes = Name{valuer.NewString("span_details_pinned_attributes")}
+ NameSpanDetailsPreviewAttributes = Name{valuer.NewString("span_details_preview_attributes")}
+ NameSpanDetailsColorByAttribute = Name{valuer.NewString("span_details_color_by_attribute")}
NameSpanPercentileResourceAttributes = Name{valuer.NewString("span_percentile_resource_attributes")}
)
@@ -40,6 +42,8 @@ func NewName(name string) (Name, error) {
NameNavShortcuts.StringValue(),
NameLastSeenChangelogVersion.StringValue(),
NameSpanDetailsPinnedAttributes.StringValue(),
+ NameSpanDetailsPreviewAttributes.StringValue(),
+ NameSpanDetailsColorByAttribute.StringValue(),
NameSpanPercentileResourceAttributes.StringValue(),
},
name,
diff --git a/pkg/types/preferencetypes/preference.go b/pkg/types/preferencetypes/preference.go
index c8ab6bafef..df91acb777 100644
--- a/pkg/types/preferencetypes/preference.go
+++ b/pkg/types/preferencetypes/preference.go
@@ -163,6 +163,24 @@ func NewAvailablePreference() map[Name]Preference {
AllowedValues: []string{},
Value: MustNewValue([]any{}, ValueTypeArray),
},
+ NameSpanDetailsPreviewAttributes: {
+ Name: NameSpanDetailsPreviewAttributes,
+ Description: "List of preview attributes in span tooltip.",
+ ValueType: ValueTypeArray,
+ DefaultValue: MustNewValue([]any{}, ValueTypeArray),
+ AllowedScopes: []Scope{ScopeUser},
+ AllowedValues: []string{},
+ Value: MustNewValue([]any{}, ValueTypeArray),
+ },
+ NameSpanDetailsColorByAttribute: {
+ Name: NameSpanDetailsColorByAttribute,
+ Description: "Attribute used to color spans in span details.",
+ ValueType: ValueTypeString,
+ DefaultValue: MustNewValue("", ValueTypeString),
+ AllowedScopes: []Scope{ScopeUser},
+ AllowedValues: []string{},
+ Value: MustNewValue("", ValueTypeString),
+ },
NameSpanPercentileResourceAttributes: {
Name: NameSpanPercentileResourceAttributes,
Description: "Additional resource attributes for span percentile filtering (beyond mandatory name and service.name).",
diff --git a/pkg/types/tracedetailtypes/aggregation.go b/pkg/types/tracedetailtypes/aggregation.go
new file mode 100644
index 0000000000..c06bb16982
--- /dev/null
+++ b/pkg/types/tracedetailtypes/aggregation.go
@@ -0,0 +1,50 @@
+package tracedetailtypes
+
+import (
+ "slices"
+
+ "github.com/SigNoz/signoz/pkg/errors"
+ "github.com/SigNoz/signoz/pkg/types/telemetrytypes"
+ "github.com/SigNoz/signoz/pkg/valuer"
+)
+
+const maxAggregationItems = 10
+
+var ErrTooManyAggregationItems = errors.NewInvalidInputf(errors.CodeInvalidInput, "aggregations request exceeds maximum of %d items", maxAggregationItems)
+
+// SpanAggregationType defines the aggregation to compute over spans grouped by a field.
+type SpanAggregationType struct {
+ valuer.String
+}
+
+var (
+ SpanAggregationSpanCount = SpanAggregationType{valuer.NewString("span_count")}
+ SpanAggregationExecutionTimePercentage = SpanAggregationType{valuer.NewString("execution_time_percentage")}
+ SpanAggregationDuration = SpanAggregationType{valuer.NewString("duration")}
+)
+
+// SpanAggregation is a single aggregation request item: which field to group by and how.
+type SpanAggregation struct {
+ Field telemetrytypes.TelemetryFieldKey `json:"field"`
+ Aggregation SpanAggregationType `json:"aggregation"`
+}
+
+// SpanAggregationResult is the computed result for one aggregation request item.
+// Duration values are in milliseconds.
+type SpanAggregationResult struct {
+ Field telemetrytypes.TelemetryFieldKey `json:"field"`
+ Aggregation SpanAggregationType `json:"aggregation"`
+ Value map[string]uint64 `json:"value" nullable:"true"`
+}
+
+func (SpanAggregationType) Enum() []any {
+ return []any{
+ SpanAggregationSpanCount,
+ SpanAggregationExecutionTimePercentage,
+ SpanAggregationDuration,
+ }
+}
+
+func (s SpanAggregationType) isValid() bool {
+ return slices.ContainsFunc(s.Enum(), func(v any) bool { return v == s })
+}
diff --git a/pkg/types/tracedetailtypes/aggregation_test.go b/pkg/types/tracedetailtypes/aggregation_test.go
new file mode 100644
index 0000000000..f1000aa72f
--- /dev/null
+++ b/pkg/types/tracedetailtypes/aggregation_test.go
@@ -0,0 +1,251 @@
+package tracedetailtypes
+
+import (
+ "testing"
+
+ "github.com/SigNoz/signoz/pkg/types/telemetrytypes"
+ "github.com/stretchr/testify/assert"
+)
+
+// mkASpan builds a WaterfallSpan with timing and field data for analytics tests.
+func mkASpan(id string, resource map[string]string, attributes map[string]any, startNs, durationNs uint64) *WaterfallSpan {
+ return &WaterfallSpan{
+ SpanID: id,
+ Resource: resource,
+ Attributes: attributes,
+ TimeUnixNano: startNs,
+ DurationNano: durationNs,
+ Children: make([]*WaterfallSpan, 0),
+ }
+}
+
+func buildTraceFromSpans(spans ...*WaterfallSpan) *WaterfallTrace {
+ spanMap := make(map[string]*WaterfallSpan, len(spans))
+ var startTime, endTime uint64
+ initialized := false
+ for _, s := range spans {
+ spanMap[s.SpanID] = s
+ if !initialized || s.TimeUnixNano < startTime {
+ startTime = s.TimeUnixNano
+ initialized = true
+ }
+ if end := s.TimeUnixNano + s.DurationNano; end > endTime {
+ endTime = end
+ }
+ }
+ return NewWaterfallTrace(startTime, endTime, uint64(len(spanMap)), 0, spanMap, nil, nil, false)
+}
+
+var (
+ fieldServiceName = telemetrytypes.TelemetryFieldKey{
+ Name: "service.name",
+ FieldContext: telemetrytypes.FieldContextResource,
+ }
+ fieldHTTPMethod = telemetrytypes.TelemetryFieldKey{
+ Name: "http.method",
+ FieldContext: telemetrytypes.FieldContextAttribute,
+ }
+ fieldCached = telemetrytypes.TelemetryFieldKey{
+ Name: "db.cached",
+ FieldContext: telemetrytypes.FieldContextAttribute,
+ }
+)
+
+func TestGetSpanAggregation_SpanCount(t *testing.T) {
+ tests := []struct {
+ name string
+ trace *WaterfallTrace
+ field telemetrytypes.TelemetryFieldKey
+ want map[string]uint64
+ }{
+ {
+ name: "counts by resource field",
+ trace: buildTraceFromSpans(
+ mkASpan("s1", map[string]string{"service.name": "frontend"}, nil, 0, 10),
+ mkASpan("s2", map[string]string{"service.name": "frontend"}, nil, 10, 5),
+ mkASpan("s3", map[string]string{"service.name": "backend"}, nil, 20, 8),
+ ),
+ field: fieldServiceName,
+ want: map[string]uint64{"frontend": 2, "backend": 1},
+ },
+ {
+ name: "counts by string attribute field",
+ trace: buildTraceFromSpans(
+ mkASpan("s1", nil, map[string]any{"http.method": "GET"}, 0, 10),
+ mkASpan("s2", nil, map[string]any{"http.method": "POST"}, 10, 5),
+ mkASpan("s3", nil, map[string]any{"http.method": "GET"}, 20, 8),
+ ),
+ field: fieldHTTPMethod,
+ want: map[string]uint64{"GET": 2, "POST": 1},
+ },
+ {
+ name: "counts by boolean attribute field",
+ trace: buildTraceFromSpans(
+ mkASpan("s1", nil, map[string]any{"db.cached": true}, 0, 10),
+ mkASpan("s2", nil, map[string]any{"db.cached": false}, 10, 5),
+ mkASpan("s3", nil, map[string]any{"db.cached": true}, 20, 8),
+ ),
+ field: fieldCached,
+ want: map[string]uint64{"true": 2, "false": 1},
+ },
+ {
+ name: "spans missing the field are excluded",
+ trace: buildTraceFromSpans(
+ mkASpan("s1", map[string]string{"service.name": "frontend"}, nil, 0, 10),
+ mkASpan("s2", map[string]string{}, nil, 10, 5), // no service.name
+ mkASpan("s3", map[string]string{"service.name": "backend"}, nil, 20, 8),
+ ),
+ field: fieldServiceName,
+ want: map[string]uint64{"frontend": 1, "backend": 1},
+ },
+ {
+ // empty string is a valid field value — counted under the "" key, unlike a missing field
+ name: "span with empty service.name is counted under empty string key",
+ trace: buildTraceFromSpans(
+ mkASpan("s1", map[string]string{"service.name": "frontend"}, nil, 0, 10),
+ mkASpan("s2", map[string]string{"service.name": ""}, nil, 10, 5),
+ mkASpan("s3", map[string]string{"service.name": "backend"}, nil, 20, 8),
+ ),
+ field: fieldServiceName,
+ want: map[string]uint64{"frontend": 1, "backend": 1, "": 1},
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ result := tc.trace.GetSpanAggregation(SpanAggregationSpanCount, tc.field)
+ assert.Equal(t, tc.field, result.Field)
+ assert.Equal(t, SpanAggregationSpanCount, result.Aggregation)
+ assert.Equal(t, tc.want, result.Value)
+ })
+ }
+}
+
+func TestGetSpanAggregation_Duration(t *testing.T) {
+ tests := []struct {
+ name string
+ trace *WaterfallTrace
+ field telemetrytypes.TelemetryFieldKey
+ want map[string]uint64
+ }{
+ {
+ name: "non-overlapping spans — merged equals sum",
+ trace: buildTraceFromSpans(
+ mkASpan("s1", map[string]string{"service.name": "frontend"}, nil, 0, 100),
+ mkASpan("s2", map[string]string{"service.name": "frontend"}, nil, 100, 50),
+ mkASpan("s3", map[string]string{"service.name": "backend"}, nil, 0, 80),
+ ),
+ field: fieldServiceName,
+ want: map[string]uint64{"frontend": 150, "backend": 80},
+ },
+ {
+ name: "non-overlapping attribute groups — merged equals sum",
+ trace: buildTraceFromSpans(
+ mkASpan("s1", nil, map[string]any{"http.method": "GET"}, 0, 30),
+ mkASpan("s2", nil, map[string]any{"http.method": "GET"}, 50, 20),
+ mkASpan("s3", nil, map[string]any{"http.method": "POST"}, 0, 70),
+ ),
+ field: fieldHTTPMethod,
+ want: map[string]uint64{"GET": 50, "POST": 70},
+ },
+ {
+ name: "overlapping spans — non-overlapping interval merge",
+ trace: buildTraceFromSpans(
+ mkASpan("s1", map[string]string{"service.name": "svc"}, nil, 0, 10),
+ mkASpan("s2", map[string]string{"service.name": "svc"}, nil, 5, 10),
+ ),
+ field: fieldServiceName,
+ want: map[string]uint64{"svc": 15}, // [0,10] ∪ [5,15] = [0,15]
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ result := tc.trace.GetSpanAggregation(SpanAggregationDuration, tc.field)
+ assert.Equal(t, tc.field, result.Field)
+ assert.Equal(t, SpanAggregationDuration, result.Aggregation)
+ assert.Equal(t, tc.want, result.Value)
+ })
+ }
+}
+
+func TestGetSpanAggregation_ExecutionTimePercentage(t *testing.T) {
+ tests := []struct {
+ name string
+ trace *WaterfallTrace
+ field telemetrytypes.TelemetryFieldKey
+ want map[string]uint64
+ }{
+ {
+ // trace [0,30]: svc occupies [0,10]+[20,30]=20 → 20*100/30 = 66%
+ name: "non-overlapping spans",
+ trace: buildTraceFromSpans(
+ mkASpan("s1", map[string]string{"service.name": "svc"}, nil, 0, 10),
+ mkASpan("s2", map[string]string{"service.name": "svc"}, nil, 20, 10),
+ ),
+ field: fieldServiceName,
+ want: map[string]uint64{"svc": 66},
+ },
+ {
+ // trace [0,15]: svc [0,15]=15 → 100%
+ name: "partially overlapping spans",
+ trace: buildTraceFromSpans(
+ mkASpan("s1", map[string]string{"service.name": "svc"}, nil, 0, 10),
+ mkASpan("s2", map[string]string{"service.name": "svc"}, nil, 5, 10),
+ ),
+ field: fieldServiceName,
+ want: map[string]uint64{"svc": 100},
+ },
+ {
+ // trace [0,20]: outer absorbs inner → 100%
+ name: "fully contained span",
+ trace: buildTraceFromSpans(
+ mkASpan("outer", map[string]string{"service.name": "svc"}, nil, 0, 20),
+ mkASpan("inner", map[string]string{"service.name": "svc"}, nil, 5, 5),
+ ),
+ field: fieldServiceName,
+ want: map[string]uint64{"svc": 100},
+ },
+ {
+ // trace [0,30]: svc [0,15]+[20,30]=25 → 25*100/30 = 83%
+ name: "three spans with two merges",
+ trace: buildTraceFromSpans(
+ mkASpan("s1", map[string]string{"service.name": "svc"}, nil, 0, 10),
+ mkASpan("s2", map[string]string{"service.name": "svc"}, nil, 5, 10),
+ mkASpan("s3", map[string]string{"service.name": "svc"}, nil, 20, 10),
+ ),
+ field: fieldServiceName,
+ want: map[string]uint64{"svc": 83},
+ },
+ {
+ // trace [0,28]: frontend [0,15]=15 → 53%, backend [0,5]+[20,28]=13 → 46%
+ name: "independent groups are computed separately",
+ trace: buildTraceFromSpans(
+ mkASpan("a1", map[string]string{"service.name": "frontend"}, nil, 0, 10),
+ mkASpan("a2", map[string]string{"service.name": "frontend"}, nil, 5, 10),
+ mkASpan("b1", map[string]string{"service.name": "backend"}, nil, 0, 5),
+ mkASpan("b2", map[string]string{"service.name": "backend"}, nil, 20, 8),
+ ),
+ field: fieldServiceName,
+ want: map[string]uint64{"frontend": 53, "backend": 46},
+ },
+ {
+ // trace [100,150]: svc [100,150]=50 → 100%
+ name: "single span",
+ trace: buildTraceFromSpans(
+ mkASpan("s1", map[string]string{"service.name": "svc"}, nil, 100, 50),
+ ),
+ field: fieldServiceName,
+ want: map[string]uint64{"svc": 100},
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ result := tc.trace.GetSpanAggregation(SpanAggregationExecutionTimePercentage, tc.field)
+ assert.Equal(t, tc.field, result.Field)
+ assert.Equal(t, SpanAggregationExecutionTimePercentage, result.Aggregation)
+ assert.Equal(t, tc.want, result.Value)
+ })
+ }
+}
diff --git a/pkg/types/tracedetailtypes/waterfall_span.go b/pkg/types/tracedetailtypes/waterfall_span.go
index 28ca5fa4a6..90397e720b 100644
--- a/pkg/types/tracedetailtypes/waterfall_span.go
+++ b/pkg/types/tracedetailtypes/waterfall_span.go
@@ -2,11 +2,13 @@ package tracedetailtypes
import (
"encoding/json"
+ "fmt"
"maps"
"sort"
"time"
"github.com/SigNoz/signoz/pkg/errors"
+ "github.com/SigNoz/signoz/pkg/types/telemetrytypes"
)
const (
@@ -21,9 +23,27 @@ var ErrTraceNotFound = errors.NewNotFoundf(errors.CodeNotFound, "trace not found
// PostableWaterfall is the request body for the v3 waterfall API.
type PostableWaterfall struct {
- SelectedSpanID string `json:"selectedSpanId"`
- UncollapsedSpans []string `json:"uncollapsedSpans"`
- Limit uint `json:"limit"`
+ SelectedSpanID string `json:"selectedSpanId"`
+ UncollapsedSpans []string `json:"uncollapsedSpans"`
+ Limit uint `json:"limit"`
+ Aggregations []SpanAggregation `json:"aggregations"`
+}
+
+func (p *PostableWaterfall) Validate() error {
+ if len(p.Aggregations) > maxAggregationItems {
+ return ErrTooManyAggregationItems
+ }
+ for _, a := range p.Aggregations {
+ if !a.Aggregation.isValid() {
+ return errors.NewInvalidInputf(errors.CodeInvalidInput, "unknown aggregation type: %q", a.Aggregation)
+ }
+ fc := a.Field.FieldContext
+ if fc != telemetrytypes.FieldContextResource && fc != telemetrytypes.FieldContextAttribute {
+ return errors.NewInvalidInputf(errors.CodeInvalidInput, "aggregation field context must be %q or %q, got %q",
+ telemetrytypes.FieldContextResource, telemetrytypes.FieldContextAttribute, fc)
+ }
+ }
+ return nil
}
// Event represents a span event.
@@ -160,7 +180,23 @@ func (ws *WaterfallSpan) GetSubtreeNodeCount() uint64 {
return count
}
-// getPreOrderedSpans returns spans in pre-order, uncollapsedSpanIDs must be pre-computed.
+// FieldValue returns the string representation of field's value on this span for grouping.
+// The bool reports whether the field was present with a non-empty value.
+func (ws *WaterfallSpan) FieldValue(field telemetrytypes.TelemetryFieldKey) (string, bool) {
+ switch field.FieldContext {
+ case telemetrytypes.FieldContextResource:
+ v, ok := ws.Resource[field.Name]
+ return v, ok
+ case telemetrytypes.FieldContextAttribute:
+ v, ok := ws.Attributes[field.Name]
+ if !ok {
+ return "", false
+ }
+ return fmt.Sprintf("%v", v), true
+ }
+ return "", false
+}
+
func (ws *WaterfallSpan) getPreOrderedSpans(uncollapsedSpanIDs map[string]struct{}, selectAll bool, level uint64) []*WaterfallSpan {
result := []*WaterfallSpan{ws.GetWithoutChildren(level)}
_, isUncollapsed := uncollapsedSpanIDs[ws.SpanID]
diff --git a/pkg/types/tracedetailtypes/waterfall_trace.go b/pkg/types/tracedetailtypes/waterfall_trace.go
index 7e808a0441..4eb28ff613 100644
--- a/pkg/types/tracedetailtypes/waterfall_trace.go
+++ b/pkg/types/tracedetailtypes/waterfall_trace.go
@@ -8,6 +8,7 @@ import (
"time"
"github.com/SigNoz/signoz/pkg/types/cachetypes"
+ "github.com/SigNoz/signoz/pkg/types/telemetrytypes"
)
type TraceSummary struct {
@@ -31,17 +32,19 @@ type WaterfallTrace struct {
// GettableWaterfallTrace is the response for the v3 waterfall API.
type GettableWaterfallTrace struct {
- StartTimestampMillis uint64 `json:"startTimestampMillis"`
- EndTimestampMillis uint64 `json:"endTimestampMillis"`
- RootServiceName string `json:"rootServiceName"`
- RootServiceEntryPoint string `json:"rootServiceEntryPoint"`
- TotalSpansCount uint64 `json:"totalSpansCount"`
- TotalErrorSpansCount uint64 `json:"totalErrorSpansCount"`
- ServiceNameToTotalDurationMap map[string]uint64 `json:"serviceNameToTotalDurationMap"`
- Spans []*WaterfallSpan `json:"spans"`
- HasMissingSpans bool `json:"hasMissingSpans"`
- UncollapsedSpans []string `json:"uncollapsedSpans"`
- HasMore bool `json:"hasMore"`
+ StartTimestampMillis uint64 `json:"startTimestampMillis"`
+ EndTimestampMillis uint64 `json:"endTimestampMillis"`
+ RootServiceName string `json:"rootServiceName"`
+ RootServiceEntryPoint string `json:"rootServiceEntryPoint"`
+ TotalSpansCount uint64 `json:"totalSpansCount"`
+ TotalErrorSpansCount uint64 `json:"totalErrorSpansCount"`
+ // Deprecated: use Aggregations with SpanAggregationExecutionTimePercentage on the service.name field instead.
+ ServiceNameToTotalDurationMap map[string]uint64 `json:"serviceNameToTotalDurationMap"`
+ Spans []*WaterfallSpan `json:"spans"`
+ HasMissingSpans bool `json:"hasMissingSpans"`
+ UncollapsedSpans []string `json:"uncollapsedSpans"`
+ HasMore bool `json:"hasMore"`
+ Aggregations []SpanAggregationResult `json:"aggregations"`
}
// NewWaterfallTrace constructs a WaterfallTrace from processed span data.
@@ -240,12 +243,13 @@ func (wt *WaterfallTrace) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, wt)
}
-// NewGettableWaterfallTrace constructs a WaterfallResponse from processed trace data and selected spans.
+// NewGettableWaterfallTrace constructs a GettableWaterfallTrace from processed trace data and selected spans.
func NewGettableWaterfallTrace(
traceData *WaterfallTrace,
selectedSpans []*WaterfallSpan,
uncollapsedSpans []string,
selectAllSpans bool,
+ aggregations []SpanAggregationResult,
) *GettableWaterfallTrace {
var rootServiceName, rootServiceEntryPoint string
if len(traceData.TraceRoots) > 0 {
@@ -263,6 +267,15 @@ func NewGettableWaterfallTrace(
span.TimeUnixNano = span.TimeUnixNano / 1_000_000
}
+ // duration values are in nanoseconds; convert in-place to milliseconds.
+ for i := range aggregations {
+ if aggregations[i].Aggregation == SpanAggregationDuration {
+ for k, v := range aggregations[i].Value {
+ aggregations[i].Value[k] = v / 1_000_000
+ }
+ }
+ }
+
return &GettableWaterfallTrace{
Spans: selectedSpans,
UncollapsedSpans: uncollapsedSpans,
@@ -275,6 +288,7 @@ func NewGettableWaterfallTrace(
ServiceNameToTotalDurationMap: serviceDurationsMillis,
HasMissingSpans: traceData.HasMissingSpans,
HasMore: !selectAllSpans,
+ Aggregations: aggregations,
}
}
@@ -307,29 +321,82 @@ func calculateServiceTime(spanIDToSpanNodeMap map[string]*WaterfallSpan) map[str
totalTimes := make(map[string]uint64)
for service, spans := range serviceSpans {
- sort.Slice(spans, func(i, j int) bool {
- return spans[i].TimeUnixNano < spans[j].TimeUnixNano
- })
-
- currentStart := spans[0].TimeUnixNano
- currentEnd := currentStart + spans[0].DurationNano
- total := uint64(0)
-
- for _, span := range spans[1:] {
- startNano := span.TimeUnixNano
- endNano := startNano + span.DurationNano
- if currentEnd >= startNano {
- if endNano > currentEnd {
- currentEnd = endNano
- }
- } else {
- total += currentEnd - currentStart
- currentStart = startNano
+ totalTimes[service] = mergeSpanIntervals(spans)
+ }
+ return totalTimes
+}
+
+// mergeSpanIntervals computes non-overlapping execution time for a set of spans.
+func mergeSpanIntervals(spans []*WaterfallSpan) uint64 {
+ if len(spans) == 0 {
+ return 0
+ }
+ sort.Slice(spans, func(i, j int) bool {
+ return spans[i].TimeUnixNano < spans[j].TimeUnixNano
+ })
+
+ currentStart := spans[0].TimeUnixNano
+ currentEnd := currentStart + spans[0].DurationNano
+ total := uint64(0)
+
+ for _, span := range spans[1:] {
+ startNano := span.TimeUnixNano
+ endNano := startNano + span.DurationNano
+ if currentEnd >= startNano {
+ if endNano > currentEnd {
currentEnd = endNano
}
+ } else {
+ total += currentEnd - currentStart
+ currentStart = startNano
+ currentEnd = endNano
}
- total += currentEnd - currentStart
- totalTimes[service] = total
}
- return totalTimes
+ return total + (currentEnd - currentStart)
+}
+
+// GetSpanAggregation computes one aggregation result over all spans in the trace.
+// Duration values are returned in nanoseconds; callers convert to milliseconds as needed.
+func (wt *WaterfallTrace) GetSpanAggregation(aggregation SpanAggregationType, field telemetrytypes.TelemetryFieldKey) SpanAggregationResult {
+ result := SpanAggregationResult{
+ Field: field,
+ Aggregation: aggregation,
+ Value: make(map[string]uint64),
+ }
+
+ switch aggregation {
+ case SpanAggregationSpanCount:
+ for _, span := range wt.SpanIDToSpanNodeMap {
+ if key, ok := span.FieldValue(field); ok {
+ result.Value[key]++
+ }
+ }
+
+ case SpanAggregationDuration:
+ spansByField := make(map[string][]*WaterfallSpan)
+ for _, span := range wt.SpanIDToSpanNodeMap {
+ if key, ok := span.FieldValue(field); ok {
+ spansByField[key] = append(spansByField[key], span)
+ }
+ }
+ for key, spans := range spansByField {
+ result.Value[key] = mergeSpanIntervals(spans)
+ }
+
+ case SpanAggregationExecutionTimePercentage:
+ traceDuration := wt.EndTime - wt.StartTime
+ spansByField := make(map[string][]*WaterfallSpan)
+ for _, span := range wt.SpanIDToSpanNodeMap {
+ if key, ok := span.FieldValue(field); ok {
+ spansByField[key] = append(spansByField[key], span)
+ }
+ }
+ if traceDuration > 0 {
+ for key, spans := range spansByField {
+ result.Value[key] = mergeSpanIntervals(spans) * 100 / traceDuration
+ }
+ }
+ }
+
+ return result
}