Skip to content

Improve clickhouse query in metric alert evaluator#8131

Merged
jonathanawesome merged 10 commits into
mainfrom
improve-clickhouse-query-in-metric-alert-evaluator
Jun 10, 2026
Merged

Improve clickhouse query in metric alert evaluator#8131
jonathanawesome merged 10 commits into
mainfrom
improve-clickhouse-query-in-metric-alert-evaluator

Conversation

@jonathanawesome

@jonathanawesome jonathanawesome commented Jun 9, 2026

Copy link
Copy Markdown
Member

This PR speeds up the metric-alert evaluator's ClickHouse query for unfiltered rules by reading from new target-keyed rollups instead of the full per-hash/client tables.

The evaluator's window query sums across all operations for a target over a time range. The existing operations_minutely / operations_hourly tables are ordered (target, hash, client_name, client_version, timestamp) (timestamp is last) and operations_minutely is PARTITION BY tuple(), so that query can't prune by time and ends up scanning the target's entire slice (up to 24h of per-hash/client rows for a 5-minute alert).

This adds two tables fed by materialized views, operations_minutely_by_target and operations_hourly_by_target, that re-aggregate default.operations keyed on (target, timestamp) only, partitioned by hour (minutely) / by day (hourly), ordered (target, timestamp).

Routing is keyed on filterConditions.length === 0...rules with a saved filter keep using the existing tables, where the hash/client predicate already exploits the sort-key prefix and the rollups (which drop those dimensions) wouldn't be usable.

@jonathanawesome jonathanawesome requested review from jdolle and n1ru4l June 9, 2026 20:16

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request introduces target-keyed materialized views (operations_minutely_by_target and operations_hourly_by_target) to optimize unfiltered metric-alert queries by avoiding full table scans, and updates the evaluator to route queries to these rollups when no filter conditions are present. Feedback on these changes highlights a critical issue in the migration: using SummingMergeTree with AggregateFunction columns will corrupt the average and quantile metrics during merges; the engine should be changed to AggregatingMergeTree with SimpleAggregateFunction for the sum columns. Additionally, a defensive check is recommended in the evaluator to ensure the rollup table is only selected when there are indeed no filter conditions.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread packages/migrations/src/clickhouse-actions/018-metric-alert-target-rollups.ts Outdated
Comment thread packages/migrations/src/clickhouse-actions/018-metric-alert-target-rollups.ts Outdated
Comment thread packages/services/workflows/src/lib/metric-alert-evaluator.ts Outdated
@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
@graphql-hive/apollo 0.48.1-alpha-20260610132951-a65882318f57792c812f186e092d051b1fd46862 npm ↗︎ unpkg ↗︎
@graphql-hive/cli 0.60.1-alpha-20260610132951-a65882318f57792c812f186e092d051b1fd46862 npm ↗︎ unpkg ↗︎
@graphql-hive/core 0.21.1-alpha-20260610132951-a65882318f57792c812f186e092d051b1fd46862 npm ↗︎ unpkg ↗︎
@graphql-hive/envelop 0.40.6-alpha-20260610132951-a65882318f57792c812f186e092d051b1fd46862 npm ↗︎ unpkg ↗︎
@graphql-hive/yoga 0.48.1-alpha-20260610132951-a65882318f57792c812f186e092d051b1fd46862 npm ↗︎ unpkg ↗︎
hive 11.3.0-alpha-20260610132951-a65882318f57792c812f186e092d051b1fd46862 npm ↗︎ unpkg ↗︎

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

🐋 This PR was built and pushed to the following Docker images:

Targets: build

Platforms: linux/arm64

Image Tag: a65882318f57792c812f186e092d051b1fd46862

Comment thread packages/migrations/src/clickhouse-actions/018-metric-alert-target-rollups.ts Outdated
Comment thread packages/migrations/src/clickhouse-actions/018-metric-alert-target-rollups.ts Outdated
Comment thread packages/migrations/src/clickhouse-actions/018-metric-alert-target-rollups.ts Outdated
Comment thread packages/migrations/src/clickhouse-actions/018-metric-alert-target-rollups.ts Outdated
Comment thread packages/services/workflows/src/lib/metric-alert-evaluator.ts Outdated
Comment thread packages/migrations/src/clickhouse-actions/018-metric-alert-target-rollups.ts Outdated
Comment thread packages/migrations/src/clickhouse-actions/018-metric-alert-target-rollups.ts Outdated
Comment thread packages/services/workflows/src/lib/metric-alert-evaluator.ts

@jdolle jdolle left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's get another set of eyes on this but it's looking good to me.

Comment thread packages/migrations/src/clickhouse-actions/018-metric-alert-target-rollups.ts Outdated

@n1ru4l n1ru4l left a comment

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.

Looks good with me. Added two comments

@jonathanawesome jonathanawesome merged commit 2741a5a into main Jun 10, 2026
24 checks passed
@jonathanawesome jonathanawesome deleted the improve-clickhouse-query-in-metric-alert-evaluator branch June 10, 2026 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants