fix(metrics): bound histogram sample storage to prevent unbounded memory growth#4119
Open
UGVicV wants to merge 16 commits into
Open
fix(metrics): bound histogram sample storage to prevent unbounded memory growth#4119UGVicV wants to merge 16 commits into
UGVicV wants to merge 16 commits into
Conversation
added 16 commits
May 22, 2026 11:55
This was referenced May 25, 2026
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.
Description
This PR resolves the issue where
MetricsCollector.observe()stored every raw observation value in an unbounded list, causing unbounded memory growth in long-running agents with high-volume metrics.Fix
liststorage with a new_HistogramBucketclass using__slots__for memory efficiency.count,total(sum),min_val,max_val, and boundedsamples(deque with maxlen).MAX_HISTOGRAM_SAMPLES = 1000constant — only the most recent 1000 samples are retained.count,sum,min,max) are computed incrementally O(1) per observation, not recomputed from raw data.minandmaxfields alongside existingcount,sum,avg.observe()in try-except withlogger.error()for robust error logging.print()withlogger.error()insnapshot().Listimport.Verification (Proof)
Added 4 regression tests to
tests/test_metrics.py:test_histogram_min_max: Verifies min/max tracking across observations.test_histogram_bounded_samples: Verifies memory is bounded after 1500 observations while count/sum remain accurate.test_histogram_aggregates_accurate: Verifies count/sum/avg/min/max for a known set.test_histogram_empty: Verifies no crash when querying unobserved metrics.Test Execution Output
Lint & Formatting Output
flake8 src/common/metrics.py tests/test_metrics.py-> Passed (0 errors/warnings)git diff --check-> Passed (0 errors)No secrets, tokens, or hidden context are included in the code.
Closes #982.