Repository navigation
Description
Expected Behaviour
After metrics.clear_default_dimensions(), the next flush should only carry the dimensions added after the call (plus service). The docs say "If you'd like to remove them at some point, you can use clear_default_dimensions method."
Current Behaviour
The cleared dimension is still emitted on the next flush. It disappears only on the flush after that.
Metrics.clear_default_dimensions() in aws_lambda_powertools/metrics/metrics.py empties provider.default_dimensions, but set_default_dimensions() has already copied each default into provider.dimension_set, and clear_metrics() copies them back after every flush. Nothing removes them from dimension_set, so the pending EMF blob keeps them.
CloudWatch treats [environment, service] and [service] as two different metrics, so the datapoint lands in the wrong series.
The reset fixture in examples/metrics/src/clear_metrics_in_tests.py runs into the same thing: it calls clear_metrics() and then clear_default_dimensions(), so clear_metrics() re-stages the defaults and the next test's first flush still has them.
Code snippet
import contextlib
import io
import json
from aws_lambda_powertools import Metrics
from aws_lambda_powertools.metrics import MetricUnit
def flushed_dimensions(metrics: Metrics) -> list:
out = io.StringIO()
with contextlib.redirect_stdout(out):
metrics.flush_metrics()
return json.loads(out.getvalue())["_aws"]["CloudWatchMetrics"][0]["Dimensions"]
metrics = Metrics(namespace="ServerlessAirline", service="booking")
metrics.set_default_dimensions(environment="dev")
metrics.clear_default_dimensions()
metrics.add_metric(name="SuccessfulBooking", unit=MetricUnit.Count, value=1)
print(flushed_dimensions(metrics))
# [['environment', 'service']], expected [['service']]
# reset fixture from examples/metrics/src/clear_metrics_in_tests.py
metrics.set_default_dimensions(environment="dev")
metrics.add_metric(name="TestA", unit=MetricUnit.Count, value=1)
flushed_dimensions(metrics)
fixture = Metrics()
fixture.clear_metrics()
fixture.clear_default_dimensions()
other = Metrics(namespace="ServerlessAirline", service="booking")
other.add_metric(name="TestB", unit=MetricUnit.Count, value=1)
print(flushed_dimensions(other))
# [['environment', 'service']], expected [['service']]Possible Solution
When clearing, also drop the default keys from the provider's pending dimension_set, but only where the value is still the default one, so a dimension the caller added with add_dimension() under the same name survives. A small clear_default_dimensions() on AmazonCloudWatchEMFProvider that Metrics.clear_default_dimensions() calls would keep this in one place.
Steps to Reproduce
pip install aws-lambda-powertools(no AWS access needed).- Run the snippet above.
- Both prints show
[['environment', 'service']].
Did you run the reproduction yourself?
Yes, I ran it and saw the failure. I used Claude Code to help write this report; the outputs above are from my own runs.
Powertools for AWS Lambda (Python) version
3.41.0 (PyPI), also on develop be21802
AWS Lambda function runtime
3.13
Packaging format used
PyPi
Debugging logs
No response
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsBacklog
Activity
boring-cyborg commentedon Nov 24, 2026
Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link
powertools-for-aws-oss-automation commentedon Nov 24, 2026
Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out our GitHub Discussions: Join the conversation
cleissonom commentedon Nov 25, 2026
A local test on
develop(0bbc252e) confirmed that the first flush afterclear_default_dimensions()still emittedenvironment=devalongsideservice=booking. The second flush contained onlyservice. The existing test checks that the defaults dictionary is empty, so it passes even when that first record carries the old dimension.@aimanmalib, are you planning to send a fix? If this is available after triage, I’d be happy to take it and add a regression test against the emitted record.
ibrhmyi commentedon Nov 26, 2026
I'd like to take this. I have a fix with tests ready on a branch and will follow AGENTS.md. I'll note AI assistance (Claude Code, reviewed and tested by me) in the PR.
cleissonom commentedon Nov 26, 2026
I've opened a draft fix in #8916. It drops the cleared defaults from the pending dimensions where they still hold the default value, as suggested in the issue, and adds a test that checks the emitted EMF record. It stays a draft until a maintainer triages this issue. @aimanmalib, if you'd rather send the fix yourself, let me know and I'll close mine.
@ibrhmyi, thanks. I offered to pick this up earlier in the thread, so I've opened the draft above. The maintainers can decide how to proceed when they triage it.
ibrhmyi commentedon Nov 26, 2026
Thanks @cleissonom. You offered first, so I'm stepping back and won't open a competing PR. Good luck with #8916.