Skip to content

fix(metrics): clear in-memory metrics when flush emits nothing - #8912

Open
hyeonsang010716 wants to merge 1 commit into
aws-powertools:developfrom
hyeonsang010716:fix/metrics-clear-state-on-empty-flush
Open

hyeonsang010716 wants to merge 1 commit into
aws-powertools:developfrom
hyeonsang010716:fix/metrics-clear-state-on-empty-flush

Conversation

@hyeonsang010716

Copy link
Copy Markdown
Contributor

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_metrics raised, or POWERTOOLS_METRICS_DISABLED was set, the dimensions, dimension sets and metadata stayed in memory and were emitted by the next invocation.

  • Call clear_metrics() in a finally block in flush_metrics(), so every flush clears the in-memory data. Default dimensions are still restored by clear_metrics().
  • Added tests for an invocation without metrics, for raise_on_empty_metrics=True, and for POWERTOOLS_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 develop and 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, mypy and ty. Two logger tests (test_append_context_keys_handles_empty_dict, test_log_dict_key_strip_nones) fail in the full run on develop as 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.

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.
@hyeonsang010716
hyeonsang010716 requested a review from a team as a code owner November 26, 2026 00:17
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Nov 26, 2026
@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

metrics size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Metrics keeps dimensions and metadata from an invocation that added no metrics

1 participant