Skip to content

perf(ingestor): optimize prom_rate to a single per-series pass#1175

Open
jessejlt wants to merge 4 commits into
mainfrom
jessejlt/didactic-waddle
Open

perf(ingestor): optimize prom_rate to a single per-series pass#1175
jessejlt wants to merge 4 commits into
mainfrom
jessejlt/didactic-waddle

Conversation

@jessejlt

@jessejlt jessejlt commented Jun 15, 2026

Copy link
Copy Markdown
Member

This is a follow up to a recent meeting regarding the performance of prom_rate. The git commit explains the issue with the current implementation and how the optimized version helps. The optimization reduces memory consumption by 30%.

prom_rate previously invoked prom_increase (which partitions by series and
runs a serialized prev/next scan) and then computed the rate with a SECOND
prev() over the combined output. Profiling against a production counter
table using Kusto's server-side QueryResourceConsumption showed the cost is
dominated by the per-series, time-ordered scan: prev()/next() require a
serialized (single-threaded) row set, so each serialized pass is expensive
and memory-heavy. The old shape paid for this twice and materialized
throwaway columns (prevVal, diff), and the second prev() ran outside the
partition over an unspecified row order -- both a performance cost and a
latent correctness risk at series boundaries.

Fuse the counter-increase and the rate division into one partition pass:
order each series by Timestamp once, derive the increase (with counter-reset
handling) and divide by the sample gap in the same scan, then project. This
removes the second serialized pass and the intermediate materialization.

Benchmark (identical queries, warm runs, server-side resource consumption):

  window  samples   metric        before   after    delta
  ~6h     ~5.6M     peak memory   228 MB   163 MB   -29%
                    total CPU     3.40 s   3.36 s   ~parity
  ~24h    ~23M      peak memory   641 MB   429 MB   -33%  (warm peak 755->386, -49%)
                    total CPU     15.4 s   14.9 s   ~parity

The per-series sort is irreducible for a prev/next approach (Kusto rejects
prev on an unordered set), so CPU/latency are at parity; the win is peak
memory (~30-50% lower). Memory is the binding resource at this scale -- the
old shape exhausts memory sooner -- so the reduction directly extends the
time range a single query can cover.

Correctness: results are identical to full printed precision. The optimized
function only omits each series' first sample, which the previous version
emitted as a spurious 0 with a cross-series denominator; this is a no-op for
sum-based aggregations and more correct for avg-based ones.

Note: the order-of-magnitude lever is to precompute per-series deltas off
the query path (a partitioning policy keyed by series with a Timestamp sort
key, or an ingestion-time update policy / materialized view). A plain scan
of the same data costs ~0.25 s CPU / ~4 MB versus ~15 s / hundreds of MB to
compute the rate over raw samples.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI 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.

Pull request overview

This PR optimizes the ADX Prometheus helper function prom_rate (created by the ingestor’s ADX syncer) by fusing counter-increase calculation and rate division into a single per-series ordered partition pass, reducing duplicated serialized scans and intermediate materialization.

Changes:

  • Rewrites prom_rate KQL to compute per-series prev()/next() deltas and rate division in one partitioned, timestamp-ordered scan.
  • Avoids the prior “second prev() outside partition on unspecified row order” shape, reducing correctness risk at series boundaries.
  • Adds in-code rationale documenting why the fused approach reduces peak memory.

Comment thread ingestor/adx/syncer.go Outdated
Comment thread ingestor/adx/syncer.go
jessejlt and others added 2 commits June 16, 2026 07:31
Avoid non-finite rates when duplicate or non-monotonic timestamps produce non-positive sample gaps. Add a focused test that records the emitted management statements and asserts the generated prom_rate function text.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jessejlt jessejlt requested a review from Copilot June 16, 2026 13:49

Copilot AI 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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot AI 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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread ingestor/adx/syncer.go
Avoid duplicating the full embedded KQL body in the test. Assert only the prom_rate contract points needed to prevent regression: fused partitioned implementation, zero-gap guard, finite-value filtering, and no fallback to prom_increase.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jessejlt jessejlt marked this pull request as ready for review June 16, 2026 14:07
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