Skip to content

feat: Add span and trace presence queries - #6079

Open
simonswine wants to merge 1 commit into
grafana:mainfrom
simonswine:20261109_add-span-id-presence
Open

simonswine wants to merge 1 commit into
grafana:mainfrom
simonswine:20261109_add-span-id-presence

Conversation

@simonswine

Copy link
Copy Markdown
Contributor

Summary

Add an internal SelectSpanPresence API to identify spans with stored profiling samples within a time range and label selector, without resolving symbols.

Supports querying by span IDs, trace IDs, or both. IDs are ORed within each selector; when both selectors are supplied, they must match the same sample.

Changes

  • Add API definitions, generated bindings, frontend validation, backend execution, and report aggregation.
  • Return sample-value totals grouped by trace ID, span ID, and profile type, keeping different units separate.
  • Preserve presence even when matching samples sum to zero.
  • Use dictionary-based page filtering to skip nonmatching data before decoding sample values.
  • Require the full requested range to use the v2 read path; v1 and mixed-mode requests return Unimplemented.

Testing

Adds unit and integration coverage for selector validation, routing, filtering, aggregation, and span-only, trace-only, combined, and nonmatching queries.

Tests not run as part of preparing this PR.

Add API bindings, v2 frontend routing and validation, backend execution, and report aggregation. Filter dictionary-encoded span and trace IDs before profile filters and late sample-value materialization; enforce combined selectors on the same sample.
@simonswine
simonswine force-pushed the 20261109_add-span-id-presence branch from 89559dc to 244778a Compare November 10, 2026 17:27
@simonswine
simonswine marked this pull request as ready for review November 11, 2026 15:48
Comment on lines +478 to +482
// span_selector = ["0000000000000001", "0000000000000002"]
// trace_id_selector = ["0000000000000000000000000000000a",
// "0000000000000000000000000000000b"]
// Matches (span 1, trace A), (span 1, trace B), (span 2, trace A),
// or (span 2, trace B), if those pairs occur on stored samples.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is not super intuitive to me, what is the use case for this API behavior?


// SelectSpanPresence returns spans with stored profiling samples and their
// total sample values, grouped by trace ID, span ID, and profile type.
rpc SelectSpanPresence(SelectSpanPresenceRequest) returns (SelectSpanPresenceResponse) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, "presence" sounds like a yes/no signal but we return profile types and totals - what other names can we consider?

x.maxPageRowNum = rn + pageNumRows
x.rowsFetched += pageNumRows
x.vit.reset(x.page, x.readSize)
x.rejected = x.predicate != nil && !x.predicate.KeepPage(x.page)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KeepPage only looks at page.Dictionary(), which is the column chunk's dictionary, so every page in a row group gets the same verdict. The probe still reads and decompresses every page that holds a candidate row, and rescans the whole dictionary each time. Is it easily possible to evaluate the predicate once per row group (on the first page) and then accept or reject the remaining candidates in that row group without further page reads?

columns...,
),
rows: rows,
columns: NewMultiColumnIterator(ctx, WrapWithRowNumber(numbers), o.batchSize, rowGroups, columns...),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For rows that pass the gate, the SpanID/TraceID pages the probe just read are read again here, since the multi-column iterator opens its own readers for the same columns.

With a per-row-group verdict (see my comment on readPage) the probe would only touch one page per row group, which would make this less relevant.

decoded, _ := hex.DecodeString(id)
ids = append(ids, string(decoded))
}
options = append(options, pq.WithPagePredicate(traceIndex, pq.NewStringInPredicate(ids)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringInPredicate.KeepPage keeps pages whose dictionary is empty, which is what an all-null TraceID chunk looks like. So for trace-only queries, row groups without any trace IDs are not skipped: every candidate row gets decoded and then dropped by the sample predicate.

spanPresencePredicate rejects those early. It's also a nested loop over dictionary entries x IDs; a map-based predicate like the span one would fix both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants