Skip to content

fix(metrics): prevent unbounded histogram memory growth - #850

Merged
moonming merged 2 commits into
mainfrom
fix/prometheus-upkeep-1199
Jul 30, 2026
Merged

fix(metrics): prevent unbounded histogram memory growth#850
moonming merged 2 commits into
mainfrom
fix/prometheus-upkeep-1199

Conversation

@moonming

@moonming moonming commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • periodically drain pending Prometheus histogram samples when AISIX owns a low-level recorder
  • stop the upkeep task through the existing shared shutdown channel
  • verify periodic upkeep scheduling and shutdown cancellation with paused Tokio time

Root cause

AISIX builds metrics-exporter-prometheus with PrometheusBuilder::build_recorder(). That low-level API does not spawn the exporter's upkeep task. Without Prometheus scrapes, each request leaves pending histogram samples in the recorder, so RSS grows approximately linearly with request count.

This is the memory issue tracked from api7/AISIX-Cloud#1199. It is independent of the allocator: local glibc and jemalloc runs both showed roughly constant retained bytes per request, and an unchanged binary stopped growing when /metrics was scraped periodically.

Benchmark

GetBusbar/benchmarking, OpenAI-to-OpenAI cell, ARM64 Colima VM, 8 vCPU / 6.8 GiB, gateway pinned to 4 cores, 300-second memory load:

Metric Before After
Idle RSS 67.20 MiB 67.26 MiB
Load-window RSS 553.04 MiB 90.81 MiB
Peak RSS 701.76 MiB 91.64 MiB
Recovered RSS 701.76 MiB 89.77 MiB
Growth +118.42 MiB/min -1.45 MiB/min
Plateau false true
Added latency p99 225 us 197 us
RPS under 10 ms p99 33,527 35,922

A second post-fix run measured 89.67 MiB peak, 87.34 MiB recovered, and -1.66 MiB/min growth.

Independent audit

  • MEDIUM: the original histogram test could pass because render() also drains pending samples, so it did not prove the scheduler lifecycle. Resolved by factoring the upkeep loop and testing periodic execution plus cancellation with paused Tokio time.

Validation

  • cargo test -p aisix-obs — 147 passed, 5 ignored real-cloud smoke tests
  • cargo test -p aisix-server — 87 passed
  • cargo clippy -p aisix-obs -p aisix-server --all-targets -- -D warnings
  • cargo fmt --all -- --check
  • GetBusbar OpenAI-to-OpenAI full memory protocol, two post-fix runs

@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@moonming, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 38 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 570cd7ce-22b7-4de0-9b0d-9169096e30d6

📥 Commits

Reviewing files that changed from the base of the PR and between 22b8353 and 1f3cfe6.

📒 Files selected for processing (3)
  • crates/aisix-obs/src/metrics.rs
  • crates/aisix-server/Cargo.toml
  • crates/aisix-server/src/main.rs

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@moonming
moonming merged commit 6202977 into main Jul 30, 2026
12 checks passed
@moonming
moonming deleted the fix/prometheus-upkeep-1199 branch July 30, 2026 08:29
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.

1 participant