Skip to content

fix(metrics): respect POWERTOOLS_METRICS_DISABLED in every emission path - #8668

Merged
leandrodamascena merged 2 commits into
aws-powertools:developfrom
soujasK:fix/metrics-disabled-all-paths
Nov 15, 2026
Merged

leandrodamascena merged 2 commits into
aws-powertools:developfrom
soujasK:fix/metrics-disabled-all-paths

Conversation

@soujasK

@soujasK soujasK commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Issue number: closes #8667

Summary

Changes

POWERTOOLS_METRICS_DISABLED (added in #6046 for both providers) was only checked in flush_metrics(). This adds the same is_metrics_disabled() check to the other emission paths:

  • single_metric (metrics/base.py): only prints when metrics aren't disabled. This also covers the CloudWatch ColdStart metric, which is emitted through single_metric. It also no longer prints a null line when the with body raises (metric_set is still None at that point); the exception still propagates.
  • AmazonCloudWatchEMFProvider.add_metric: the automatic flush at 100 metrics / 100 values doesn't print when disabled. The metric set is still cleared, as before.
  • DatadogProvider.flush_metrics: doesn't submit through datadog_lambda's lambda_metric when disabled.

Since is_metrics_disabled() also returns true for POWERTOOLS_DEV=true without POWERTOOLS_METRICS_DISABLED, single_metric and the cold start metric are now silent in dev mode too, the same as flush_metrics() already was.

The legacy MetricManager in metrics/base.py isn't changed: it's only reached through SingleMetric, which can't hit its auto-flush.

Tests: one per path (single_metric, cold start via log_metrics, the 100-metric auto-flush, Datadog lambda_metric mocked), a POWERTOOLS_DEV case for single_metric, and the null line when the body raises. The five path tests fail without the change. tests/functional/metrics passes; on Linux (Python 3.12) ruff, mypy, ty, bandit and xenon pass. Running tests/unit, tests/functional/metrics and tests/functional/batch together on Linux, test_datadog_write_to_log_with_env_variable fails the same way on develop without this change (it passes on its own), so it looks like existing cross-test state.

User experience

Before: with metrics disabled, the ColdStart metric, single_metric, the 100-metric auto-flush and Datadog extension submissions were still emitted (and EMF lines are ingested by CloudWatch).

After: nothing is emitted when metrics are disabled.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@leandrodamascena

Copy link
Copy Markdown
Contributor

Hi @soujasK, I saw you closed this one. No problem, I just want to understand what you'd prefer. The bug is real and triaged for 3.40.0, and I already worked on it on my side. Do you want to reopen so I review your PR, or should I open a new one myself? Either way is fine, just let me know.

@soujasK soujasK reopened this Oct 31, 2026
@soujasK

soujasK commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Reopening this. I closed it by mistake. I need a bit more time to test and validate it properly, so I'll update here once that's done.

@leandrodamascena

Copy link
Copy Markdown
Contributor

Thanks @soujasK, same story as #8664, this missed 3.40.0 and goes in 3.41.0. The change is right and the six tests cover every path. It overlaps with #8797, which touches the same lines in single_metric, so once that one lands I'll resolve the conflict on your branch myself rather than asking you to. You'll be in the release notes.

@leandrodamascena leandrodamascena modified the milestones: 3.40.0, 3.41.0 Nov 15, 2026
# Conflicts:
#	aws_lambda_powertools/metrics/base.py
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 15, 2026

@leandrodamascena leandrodamascena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @soujasK! Reviewed on top of develop with #8797 in: every emission path now checks the flag, including the cold start metric and the Datadog submit, and the six tests cover them. I merged develop into your branch to clear the one-line overlap with #8797, nothing else changed. Goes out in 3.41.0, you'll be in the release notes.

@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.17%. Comparing base (ff823e7) to head (e390e86).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #8668   +/-   ##
========================================
  Coverage    97.17%   97.17%           
========================================
  Files          323      323           
  Lines        16629    16630    +1     
  Branches      1509     1510    +1     
========================================
+ Hits         16160    16161    +1     
  Misses         328      328           
  Partials       141      141           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@leandrodamascena
leandrodamascena merged commit 3f54976 into aws-powertools:develop Nov 15, 2026
16 checks passed
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: POWERTOOLS_METRICS_DISABLED is ignored by single_metric, the cold start metric, add_metric auto-flush and Datadog's lambda_metric

2 participants