Skip to content

[DO NOT MERGE] Rule group select merging #7522

Draft
alanprot wants to merge 1 commit into
cortexproject:masterfrom
alanprot:rule-group-select-merging
Draft

[DO NOT MERGE] Rule group select merging #7522
alanprot wants to merge 1 commit into
cortexproject:masterfrom
alanprot:rule-group-select-merging

Conversation

@alanprot
Copy link
Copy Markdown
Member

@alanprot alanprot commented May 15, 2026

Rule Group Select Merging — Reduce redundant ingester queries

What

When a rule group has multiple rules querying the same metric with overlapping matchers (e.g., different label filter variants), this optimization pre-fetches the broadest query once and serves individual rules from the cached result with local filtering.

Why

Rule groups often contain multiple rules querying the same metric with slightly different label filters (e.g., per-environment, per-deployment variant). Each rule independently queries the ingester even when one rule's matchers are a superset of the others. This results in redundant postings resolution and series fetching that scales linearly with the number of variants.

How

Uses the existing GroupEvalIterationFunc hook — zero vendored Prometheus changes. The QueryFunc is wrapped at construction time to check context for a prefetch cache, and the iteration func injects the cache via context.

Flow per group evaluation:

  1. Plan — Parse all rules' PromQL expressions, group by metric name + expression structure, find superset matchers (e.g., =~".*" covers "blue" and "green")
  2. Pre-fetch — Evaluate the broadest rule's full expression once via the existing QueryFunc
  3. Evaluate — Subsequent rules are served from the cached Vector, filtered by their specific matchers

Configuration

ruler:
  select_merger_enabled: true
  select_merger_min_rules: 3  # minimum rules on same metric to trigger merging

Flags: --ruler.select-merger-enabled, --ruler.select-merger-min-rules

Safety

  • Disabled by default — opt-in via config flag
  • Graceful fallback — if pre-fetch errors or no superset exists, rules evaluate normally
  • Only merges same expression structurerate(m{...}[5m]) and sum(m{...}) are never merged
  • Only merges when superset rule existsjob="api" + job="web" (no superset) won't merge

Benchmark

BenchmarkSelectMerger/without_merging    114 queries/op    1.60ms/op    11971 allocs/op
BenchmarkSelectMerger/with_merging        38 queries/op    1.30ms/op     8557 allocs/op

66% query reduction, 19% faster, 28% fewer allocations.

Files

File Change
pkg/ruler/select_merger.go Core planner: parse rules, group by metric+structure, compute merged matchers
pkg/ruler/prefetch_queryable.go Cache, QueryFunc wrapper, vector filtering
pkg/ruler/manager.go Wire iteration func when config enabled
pkg/ruler/ruler.go Config fields
pkg/ruler/compat.go Wrap QueryFunc with context-aware select merger

@alanprot alanprot force-pushed the rule-group-select-merging branch from 88e3d22 to 5ae03f9 Compare May 15, 2026 23:56
…eries

When a rule group has multiple rules querying the same metric with overlapping
matchers, pre-fetch the broadest query once and serve individual rules from
the cached result with local filtering.

- Parse rules' PromQL expressions, group by metric + expression structure
- Detect superset matchers (e.g., =~".*" covers "blue" and "green")
- Pre-fetch broadest rule's expression once via QueryFunc
- Serve subsequent rules from cached Vector with local matcher filtering
- Zero vendored changes — uses context injection via GroupEvalIterationFunc

Config: --ruler.select-merger-enabled, --ruler.select-merger-min-rules

Benchmark: 114 → 38 queries per group evaluation (66% reduction)
@alanprot alanprot force-pushed the rule-group-select-merging branch from 5ae03f9 to 93ccc9a Compare May 16, 2026 00:08
@alanprot alanprot changed the title Rule group select merging [DO NOT MERGE] Rule group select merging May 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant