Repository navigation
feat(mcp): report each tile's wall time and read counts in query_tiles - #3822
Conversation
Agents could not tell an expensive dashboard tile from a cheap one: query_tiles kept only status, hasData and rowCount. Each tile that ran now reports elapsedMs (measured from when it starts, so time waiting in the queue doesn't count, and on failures too, since slow tiles are often the ones that fail), plus ClickHouse's rowsRead and bytesRead when the tile's result carries them. PromQL results get elapsedMs as well: the Prometheus API returns no read counts, so wall time is the only cost signal there. Part of ClickHouse#3649.
🦋 Changeset detectedLatest commit: 39345ab The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
@bcanvural is attempting to deploy a commit to the HyperDX Team on Vercel. A member of the Team first needs to authorize it. |
| // timeout, folding either into a status:'error' entry so one | ||
| // misbehaving tile never rejects the whole batch. | ||
| const started = Date.now(); | ||
| const elapsed = () => ({ elapsedMs: Date.now() - started }); |
There was a problem hiding this comment.
🟡 P2 — Tiles skipped by the batch deadline still report elapsedMs (≈0), as if they ran
started is captured before withDeadline, and withDeadline (packages/api/src/mcp/utils/timeout.ts:87-90) throws DeadlineExceededError without starting the query once the budget is spent. The catch block then spreads ...elapsed(), so a tile that never ran comes back with elapsedMs: 0. That contradicts the new description ('Each tile that ran reports elapsedMs'), and an agent ranking tiles by cost will read 0 as 'cheap'. Fix: omit elapsedMs when err instanceof DeadlineExceededError and the work never started, or record started inside the thunk passed to withDeadline. Then assert in the existing 'does not issue a query for tiles scheduled after the deadline' test that elapsedMs is absent.
Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond
There was a problem hiding this comment.
Fixed in ab3eea3: the clock starts inside the work withDeadline runs, so a skipped tile has no elapsedMs. The deadline test now checks that.
| signal: options?.abortSignal, | ||
| }), | ||
| ); | ||
| const started = Date.now(); |
There was a problem hiding this comment.
🟡 P2 — PromQL elapsedMs includes source lookup and backend probing, not just query wall time
started is taken before runPromqlTile, which first runs resolvePromqlTarget (Mongo lookups) and resolvePromqlQueryBackend, which probes the server for its Prometheus HTTP API (packages/api/src/mcp/tools/query/promqlTile.ts:127-150). Only then does it run the queries. The clickstack_promql description calls this "the query's wall time". Fix: either time only the runPromqlQuery calls inside runPromqlTile and return that value, or change the description to say it is the whole tile's wall time, including setup.
Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond
There was a problem hiding this comment.
Fixed in ab3eea3 by wording: the description now says the call's wall time. It includes the source lookup and backend probe on purpose, since that's part of what the call costs.
PR Review✅ No issues found. |
Deep Review
✅ No critical issues found. 🟡 P2 — recommended
🔵 P3 nitpicks (5)
Reviewers (10): correctness, testing, maintainability, project-standards, kieran-typescript, api-contract, adversarial, previous-comments, agent-native, learnings-researcher. Testing gaps:
No finding blocks merge automatically. A maintainer will expect P0/P1 findings in code this PR changes to be fixed; P2/P3 are your call -- fix or reply. Never fix findings about surrounding code here; reply instead. Do not widen the PR. How to respond |
Start a tile's clock inside the work withDeadline runs, so a tile that never started reports no time instead of 0, which reads as cheap. Time with performance.now(), and describe the PromQL elapsedMs as the call's wall time: it includes the source lookup and backend probe. Test statistics without bytes_read.
| // tile: the source lookup and backend probe as well as the queries. | ||
| return formatQueryResult({ | ||
| ...result, | ||
| elapsedMs: Math.round(performance.now() - started), |
There was a problem hiding this comment.
🟡 P2 — PromQL and SQL results now report wall time under different keys and units
clickstack_query_tile / clickstack_sql already put ClickHouse's time in result.statistics.elapsed, in seconds (windowedSearch.ts:68-80 builds the same shape). This change puts PromQL time in result.elapsedMs, in milliseconds. An agent comparing tiles from clickstack_query_tile now has to know two field names and two units. Fix: emit PromQL time as statistics: { elapsed: seconds } to match the SQL shape, or add elapsedMs to the builder and SQL branches of runConfigTile as well, so every result carries the same field.
Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond
There was a problem hiding this comment.
Keeping this as is. statistics is ClickHouse's own block and comes with read counts. A PromQL call has none, so a statistics block with only elapsed would look like ClickHouse output with the counts missing. elapsedMs names its unit.
|
Deep review P2 on Two review rounds are done with no P0/P1 left, so I'll stop on the automated findings here. @brandon-pereira, could you take a look when you have time? |
| const readStatisticsSchema = z.object({ | ||
| rows_read: z.number(), | ||
| bytes_read: z.number(), | ||
| }); | ||
|
|
There was a problem hiding this comment.
| const readStatisticsSchema = z.object({ | |
| rows_read: z.number(), | |
| bytes_read: z.number(), | |
| }); | |
| const readStatisticsSchema = z | |
| .object({ | |
| statistics: z.object({ | |
| rows_read: z.number(), | |
| bytes_read: z.number(), | |
| }), | |
| }) | |
| .transform(({ statistics }) => statistics); |
There was a problem hiding this comment.
Folded into the rewrite in 028e14e: the schema now parses the whole payload, with statistics read inside result.
| // ClickHouse's own read counts, present on builder and raw SQL tiles. | ||
| const stats = readStatisticsSchema.safeParse( | ||
| result != null && typeof result === 'object' && 'statistics' in result | ||
| ? result.statistics | ||
| : undefined, | ||
| ); |
There was a problem hiding this comment.
Along with my comment above,
| // ClickHouse's own read counts, present on builder and raw SQL tiles. | |
| const stats = readStatisticsSchema.safeParse( | |
| result != null && typeof result === 'object' && 'statistics' in result | |
| ? result.statistics | |
| : undefined, | |
| ); | |
| // ClickHouse's own read counts, present on builder and raw SQL tiles. | |
| const stats = readStatisticsSchema.safeParse(result); |
There was a problem hiding this comment.
Actually, this whole function could probably benefit from being rewritten to a Zod parse, rather than mixing hand-written type checks with Zod.
There was a problem hiding this comment.
Done in 028e14e: summarizeRows is now one Zod parse of the payload, and only the JSON.parse stays outside it. Incomplete statistics are dropped with .catch(undefined), so they still don't fail the summary.
karl-power
left a comment
There was a problem hiding this comment.
Looks good. Just two small docs updates please:
-
MCP.md: Add a short “Query cost metadata” section explaining thatelapsedMsmeasures wall time, including source lookup and network overhead but excluding batch queue wait. Started queries report timing even on failure; never-started tiles omit it. Clarify thatrowsRead/bytesReadmean data scanned, not rows returned or response size, and that successful PromQL responses expose timing atresult.elapsedMs. -
packages/api/src/mcp/tools/dashboards/queryTiles.ts: Qualify the tool description’s read-count promise with “when available.” Event-pattern tiles currently report onlyelapsedMs, like PromQL tiles, so “builder and SQL tiles also report rowsRead and bytesRead” is too broad.
Add a Query cost metadata section to MCP.md and qualify the query_tiles read counts with "when available": event-pattern tiles report only elapsedMs.
|
Thanks! Both done in e8a82bc:
|
Summary
Part of #3649 (not all of it; see "Not in this PR").
clickstack_query_tilesreturned onlystatus,hasDataandrowCountper tile, so an agent couldn't tell a tile that read 10 GiB from one that read 4 MB. Now:elapsedMs, its wall time. It's measured from when the tile's query starts, so time spent waiting in the queue (6 tiles at a time) doesn't count. Failed tiles report it too, since the slow tiles are often the ones that fail.rowsReadandbytesRead. These are ClickHouse's own numbers: they're already in each tile's JSON result (statistics), andsummarizeRowsnow keeps them.elapsedMs, inclickstack_promql,clickstack_query_tileand per tile inclickstack_query_tiles. The Prometheus API returns no read counts, so wall time is the only cost signal there. The tool descriptions say so.Not in this PR
X-ClickHouse-Summarywithread_rows: 0, also on ClickHouse master. It never wires query progress to the response, whichHTTPHandlerdoes.system.query_loghas the real numbers under our query id, but only after the log flush. Reported as Prometheus query API: X-ClickHouse-Summary reports read_rows 0 for the PromQL queries it runs ClickHouse#134380.describe_source,event_patterns,emerging_signals). This needs counting inside the ClickHouse client. I'd do it in a follow-up, after feat(mcp): emerging_signals compares both windows in one Drain tree and reports new variants #3753 (which changes the pattern tools) lands.How to reproduce
Starting point:
make dev-int.On
main:clickstack_query_tileson any dashboard returns tiles withoutelapsedMs,rowsReadorbytesRead, andclickstack_promqlreturns noelapsedMs.On this branch:
make dev-int FILE="mcp/__tests__/(dashboards/(queryTiles|promqlQueryTile)|promqlQueryTool)"passes, including the new assertions onelapsedMs,rowsReadandbytesRead.yarn jest src/mcp/__tests__/dashboards/queryTiles(unit) coverssummarizeRowswithstatistics, andelapsedMson a tile that throws.How to test on Vercel preview
N/A — non-UI change.
References
describe_sourcetools return no cost statistics #3649Note
Low Risk
Additive MCP response fields and documentation; behavior of queries is unchanged aside from timing measurement and statistics passthrough.
Overview
MCP query tools now expose query cost metadata so agents can compare expensive vs cheap dashboard tiles and PromQL calls.
clickstack_query_tilesadds per-tileelapsedMs(wall time from when the query starts, excluding queue wait; omitted if the tile never ran). Successful builder and SQL tiles also surface ClickHouserowsReadandbytesReadby parsingstatisticsfrom the tile payload via a new Zod-backedsummarizeRows. Failed tiles still reportelapsedMswhen the query started.PromQL paths (
clickstack_promql,clickstack_query_tile, and PromQL tiles in batch query) attachelapsedMson the result object inrunConfigTile; no read counts because the Prometheus API does not provide them. Tool descriptions and MCP.md document the new fields.Reviewed by Cursor Bugbot for commit 39345ab. Bugbot is set up for automated code reviews on this repo. Configure here.