Repository navigation
fix(metrics): clear in-memory metrics when flush emits nothing - #8912
Open
hyeonsang010716 wants to merge 1 commit into
Open
hyeonsang010716 wants to merge 1 commit into
hyeonsang010716 wants to merge 1 commit into
Conversation
flush_metrics only called clear_metrics after printing an EMF blob. When an invocation added dimensions or metadata but no metrics, when raise_on_empty_metrics raised, or when POWERTOOLS_METRICS_DISABLED was set, nothing was cleared. The next invocation on the same warm container then emitted the previous dimensions, dimension sets and metadata, and with metrics disabled the metric values kept piling up. Clear in a finally block so every flush resets the in-memory data. Default dimensions are still restored by clear_metrics.
|
This branch has not been deployed
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.



Issue number: closes #8911
Summary
Changes
flush_metrics()only cleared the in-memory data after printing an EMF blob. When the flush emitted nothing, because no metrics were added,raise_on_empty_metricsraised, orPOWERTOOLS_METRICS_DISABLEDwas set, the dimensions, dimension sets and metadata stayed in memory and were emitted by the next invocation.clear_metrics()in afinallyblock inflush_metrics(), so every flush clears the in-memory data. Default dimensions are still restored byclear_metrics().raise_on_empty_metrics=True, and forPOWERTOOLS_METRICS_DISABLED.User experience
Before: a handler that adds a dimension or metadata and returns without a metric leaves them in memory, and the next invocation on the same warm container emits them. The repro in #8911 emits
"Dimensions":[["coupon","service"]]with"coupon_owner":"user-123"on its second invocation.After: each invocation only emits what it added, plus the default dimensions. The repro emits
"Dimensions":[["service"]]. With metrics disabled, values no longer pile up across invocations.Verification
The three new tests fail on
developand pass with this change.Tested locally on Python 3.14 with the full
pytest -m "not perf" --ignore tests/e2e,ruff format --check,ruff check,mypyandty. Two logger tests (test_append_context_keys_handles_empty_dict,test_log_dict_key_strip_nones) fail in the full run ondevelopas well and pass on their own, so they are unrelated to this change.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.