perf(ingestor): optimize prom_rate to a single per-series pass#1175
Open
jessejlt wants to merge 4 commits into
Open
perf(ingestor): optimize prom_rate to a single per-series pass#1175jessejlt wants to merge 4 commits into
jessejlt wants to merge 4 commits into
Conversation
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>
Contributor
There was a problem hiding this comment.
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_rateKQL to compute per-seriesprev()/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.
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>
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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%.