Skip to content

fix(metrics): remove cleared default dimensions from the next flush - #8916

Open
cleissonom wants to merge 2 commits into
aws-powertools:developfrom
cleissonom:fix/metrics-clear-default-dimensions
Open

cleissonom wants to merge 2 commits into
aws-powertools:developfrom
cleissonom:fix/metrics-clear-default-dimensions

Conversation

@cleissonom

@cleissonom cleissonom commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Issue number:
closes #8899

Summary

Changes

set_default_dimensions() also adds each default to the provider's dimension_set, which holds the dimensions for the next flush. clear_default_dimensions() only emptied default_dimensions, so the next flush still carried the cleared dimensions. clear_metrics() stages the defaults again after each flush, so the testing example, which calls it before clear_default_dimensions(), leaked them too.

Metrics.clear_default_dimensions() now removes each default from dimension_set while it still holds the default value, as suggested in the issue. A dimension that add_dimension() set to another value after the default is kept; one set to the same value is removed. The change stays in Metrics because AmazonCloudWatchEMFProvider, exported as EphemeralMetrics, has no clear_default_dimensions(), and adding one would be new public API. Dimension sets created earlier with add_dimensions() keep the defaults they copied. I can extend the change to them if you prefer.

User experience

Calls Before After
set_default_dimensions(environment="dev"), clear_default_dimensions(), add_metric(...), flush "Dimensions": [["environment", "service"]] and "environment": "dev" "Dimensions": [["service"]]
set_default_dimensions(tenant="a"), add_metric(...), clear_default_dimensions(), flush "Dimensions": [["tenant", "service"]] "Dimensions": [["service"]]

Both rows change the output. ADR 0006 allows that for a bug fix the PR calls out: the docs say clear_default_dimensions() removes the defaults, with no exception for metrics that haven't been flushed yet. Dimensions apply to the whole flush, which is why the second row changes too. For a dimension that belongs to a single flush, add_dimension() is the method to use. Flushes after the first one were already correct.

Verification

I used an AI coding assistant to write the change, the tests and this description. I reviewed the change and ran both commands below myself. The results shown come from the assistant's runs of the same commands on macOS arm64 with CPython 3.14.10 and locked dependencies.

test_clear_default_dimensions_removes_them_from_next_flush sets a default, clears it, flushes a metric and checks the emitted dimensions. The existing test_clear_default_dimensions only checks the dictionary, so it passed with the bug; it is unchanged. test_clear_default_dimensions_keeps_dimension_added_with_another_value covers the value check.

  • uv run --locked pytest -q tests/functional/metrics/required_dependencies/test_metrics_cloudwatch_emf.py -k "removes_them_from_next_flush or keeps_dimension_added_with_another_value" gives 1 failed, 1 passed on 3bb47f8, which adds only the tests to develop (0bbc252e). The failure is assert [['environment', 'service']] == [['service']]. On 5845d87, with the change, it gives 2 passed.
  • make pr on 5845d87 passes, with 4,359 passed and 73 skipped in the main test run and all 10 performance tests passing. ruff, markdownlint, mypy, bandit and xenon report no issues.

The reporter also ran both snippets from #8899 against 5845d87: each now prints [['service']], while develop still prints [['environment', 'service']].

The tests read the EMF output from stdout; nothing was sent to CloudWatch.


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.

@sonarqubecloud

Copy link
Copy Markdown

@cleissonom
cleissonom marked this pull request as ready for review November 29, 2026 21:02
@cleissonom
cleissonom requested a review from a team as a code owner November 29, 2026 21:02

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: clear_default_dimensions() leaves the cleared dimensions on the next metrics flush

1 participant