Skip to content

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

Open
soujasK wants to merge 1 commit into
aws-powertools:developfrom
soujasK:fix/metrics-disabled-all-paths
Open

soujasK wants to merge 1 commit into
aws-powertools:developfrom
soujasK:fix/metrics-disabled-all-paths

Conversation

@soujasK

@soujasK soujasK commented Oct 25, 2026

Copy link
Copy Markdown

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.

@soujasK
soujasK requested a review from a team as a code owner October 25, 2026 19:07
@soujasK
soujasK requested a review from hjgraca October 25, 2026 19:07
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 25, 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/L Denotes a PR that changes 100-499 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