Skip to content

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

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

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. It ran the checks below; I'll repeat them myself before marking this ready for review.

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.

On macOS arm64 with CPython 3.14.10 and locked dependencies, uv run --locked pytest --disable-socket -q tests/functional/metrics/required_dependencies/test_metrics_cloudwatch_emf.py -k "removes_them_from_next_flush or keeps_dimension_added_with_another_value" gave 1 failed, 1 passed on develop (0bbc252e) with only the tests added ([['environment', 'service']] == [['service']]), and 2 passed with the change.

  • Both snippets from Bug: clear_default_dimensions() leaves the cleared dimensions on the next metrics flush #8899 print [['service']] instead of [['environment', 'service']].
  • The metrics tests pass on Python 3.10.22, 3.11.17, 3.12.15, 3.13.15 and 3.14.10 (182 passed, 1 skipped on each).
  • make pr without lint-docs, which needs Docker (no Markdown changed): ruff, mypy, bandit, xenon and pre-commit pass, with 4,359 passed and 73 skipped. Two timing-based tests, test_data_masking_encrypt_with_json_blob and test_tracer_init, failed on import-time limits in every run on this branch, and on develop too, while the machine was under heavy load.

The tests read the EMF output from stdout; nothing was sent to CloudWatch. Until a maintainer triages #8899, this stays a draft.


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

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