Skip to content

feat(trace-details): Add API endpoint & module for trace aggregations - #11452

Merged
krsoninikhil merged 21 commits into
mainfrom
ns/waterfall-v3-memory-3
Jun 1, 2026
Merged

feat(trace-details): Add API endpoint & module for trace aggregations#11452
krsoninikhil merged 21 commits into
mainfrom
ns/waterfall-v3-memory-3

Conversation

@krsoninikhil

@krsoninikhil krsoninikhil commented May 25, 2026

Copy link
Copy Markdown
Member

Part of https://github.com/SigNoz/engineering-pod/issues/4787

Add API endpoint & module for trace aggregations. Will raise a follow up for implementation of store methods.

Query used for calculating the non-overlapping span execution time:

WITH

-- query minimum span fields
all_spans AS (
	SELECT DISTINCT ON (span_id)
		%s AS field_value,
		toUnixTimestamp64Nano(timestamp) AS start_ns,
		start_ns + duration_nano AS end_ns
	FROM %s.%s
	WHERE trace_id=? AND ts_bucket_start>=? AND ts_bucket_start<=? AND notEmpty(field_value)
	ORDER BY timestamp ASC, name ASC
),

-- calculate max end time of preceding spans
effective_start AS (
	SELECT
		field_value, end_ns,
		greatest(
			start_ns,
			ifNull(
				max(end_ns) OVER (
					PARTITION BY field_value
					ORDER BY start_ns
					ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
				),
				toUInt64(0)
			)
		) AS effective_start_ns
	FROM all_spans
)

-- each span contributes only the tail past its effective start.
SELECT field_value, sum(toUInt64(greatest(end_ns - effective_start_ns, 0))) AS total_ns
FROM effective_start
GROUP BY field_value

Another possible option using arrayFold and arraySort simpler but hard to understand (rejected):

WITH spans AS (
    SELECT DISTINCT ON (span_id)
        <field_expr> AS grp,
        toUInt64(timestamp)                AS s,
        toUInt64(timestamp) + duration_nano AS e
    FROM signoz_traces.distributed_signoz_index_v3
    WHERE trace_id = ? AND ts_bucket_start >= ? AND ts_bucket_start <= ?
      AND notEmpty(grp)
)
SELECT
    grp,
    arrayFold(
        (acc, x) -> (
            greatest(acc.1, x.2),
            acc.2 + if(x.2 > greatest(x.1, acc.1),
                       x.2 - greatest(x.1, acc.1),
                       toUInt64(0))
        ),
        arraySort(t -> t.1, groupArray((s, e))),
        (toUInt64(0), toUInt64(0))
    ).2 AS value
FROM spans
GROUP BY grp

@krsoninikhil
krsoninikhil changed the base branch from main to emdash/trace-oom-k62bc May 25, 2026 20:06
@krsoninikhil
krsoninikhil force-pushed the ns/waterfall-v3-memory-3 branch from 11f4389 to 84bdf9f Compare May 26, 2026 09:23
@krsoninikhil
krsoninikhil marked this pull request as ready for review May 26, 2026 09:49
@krsoninikhil
krsoninikhil requested review from a team and therealpandey as code owners May 26, 2026 09:49
Base automatically changed from emdash/trace-oom-k62bc to main May 26, 2026 10:19
@krsoninikhil
krsoninikhil force-pushed the ns/waterfall-v3-memory-3 branch from 978c0f8 to dd76af8 Compare May 26, 2026 11:27
@krsoninikhil
krsoninikhil marked this pull request as draft May 26, 2026 18:55
@krsoninikhil
krsoninikhil marked this pull request as ready for review May 26, 2026 20:29
Comment thread pkg/modules/tracedetail/impltracedetail/store.go Outdated
@krsoninikhil
krsoninikhil force-pushed the ns/waterfall-v3-memory-3 branch from b0af9b3 to 534e042 Compare May 27, 2026 09:23
@krsoninikhil

krsoninikhil commented May 27, 2026

Copy link
Copy Markdown
Member Author

Curl example:

POST 'http://localhost:8080/api/v1/traces/fb83154a65378c5cef2bf64229d73b12/aggregations' \
  --header 'Content-Type: application/json' \
  --header 'SIGNOZ-API-KEY: <>' \
  --body '{
   "aggregations": [
        {
	     "field": {"name": "service.name", "fieldContext": "resource", "fieldDataType": "string"},  
	     "aggregation": "execution_time_percentage"
	    }
    ]
}' 

Response:

{
    "status": "success",
    "data": {
        "aggregations": [
            {
                "field": {
                    "name": "service.name",
                    "signal": "",
                    "fieldContext": "resource",
                    "fieldDataType": "string"
                },
                "aggregation": "execution_time_percentage",
                "value": {
                    "go-bulk-40": 99
                }
            }
        ]
    }
}

@krsoninikhil krsoninikhil changed the title Add API endpoint & module for trace aggregations feat(trace-details) Add API endpoint & module for trace aggregations May 27, 2026
@krsoninikhil krsoninikhil changed the title feat(trace-details) Add API endpoint & module for trace aggregations feat(trace-details): Add API endpoint & module for trace aggregations May 27, 2026
Comment thread pkg/types/spantypes/store.go
Comment thread pkg/modules/tracedetail/impltracedetail/module.go
@github-actions github-actions Bot added the enhancement New feature or request label May 29, 2026
Comment thread pkg/types/spantypes/spantypestest/store.go Outdated
@krsoninikhil
krsoninikhil force-pushed the ns/waterfall-v3-memory-3 branch from 26078d0 to 1f93993 Compare June 1, 2026 09:35
therealpandey
therealpandey previously approved these changes Jun 1, 2026
@therealpandey

Copy link
Copy Markdown
Member

@krsoninikhil can we keep the aggregation response similar to QBV5?

@krsoninikhil
krsoninikhil enabled auto-merge June 1, 2026 12:30
therealpandey
therealpandey previously approved these changes Jun 1, 2026
@krsoninikhil
krsoninikhil added this pull request to the merge queue Jun 1, 2026
Merged via the queue into main with commit 5e94f7a Jun 1, 2026
19 checks passed
@krsoninikhil
krsoninikhil deleted the ns/waterfall-v3-memory-3 branch June 1, 2026 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants