Repository navigation
Bug: POWERTOOLS_METRICS_DISABLED is ignored by single_metric, the cold start metric, add_metric auto-flush and Datadog's lambda_metric #8667
Description
Expected Behaviour
With POWERTOOLS_METRICS_DISABLED=true (or POWERTOOLS_DEV=true without POWERTOOLS_METRICS_DISABLED), nothing is emitted, as documented ("Disables all metrics emitted by Powertools", for both the CloudWatch and Datadog providers, per #6046).
Current Behaviour
flush_metrics() checks is_metrics_disabled(), but three other emission paths don't:
single_metric(metrics/base.py) always prints in itsfinallyblock. This is also how the CloudWatch provider emits theColdStartmetric, so@metrics.log_metrics(capture_cold_start_metric=True)still prints aColdStartEMF line while the application metrics are correctly suppressed.- The CloudWatch
add_metricauto-flush: when the metric set reaches 100 metrics, or a metric reaches 100 values,AmazonCloudWatchEMFProvider.add_metricprints the EMF blob directly. - Datadog with the
datadog_lambdapackage installed:DatadogProvider.flush_metricscallslambda_metric(...)without checkingis_metrics_disabled(); only the flush-to-log branch checks it.
In Lambda the EMF lines in 1 and 2 are ingested as real CloudWatch metrics, so metrics are published even though they're disabled.
While there: if the body of a with single_metric(...) block raises, metric_set is still None in the finally block and a literal null line is printed to stdout.
Code snippet
import os
from types import SimpleNamespace
os.environ["POWERTOOLS_METRICS_DISABLED"] = "true"
from aws_lambda_powertools import Metrics
from aws_lambda_powertools.metrics import single_metric
metrics = Metrics(namespace="ns", service="svc")
@metrics.log_metrics(capture_cold_start_metric=True)
def handler(event, context):
metrics.add_metric(name="Booking", unit="Count", value=1) # correctly not emitted
context = SimpleNamespace(function_name="fn", memory_limit_in_mb=128, invoked_function_arn="arn", aws_request_id="id")
handler({}, context) # prints the ColdStart EMF line
with single_metric(name="Single", unit="Count", value=1, namespace="ns"):
pass # prints the EMF line
for i in range(100):
metrics.add_metric(name=f"metric_{i}", unit="Count", value=1) # auto-flush prints the EMF blobOutput (develop @ 5785377, Python 3.11):
{"_aws":{"Timestamp":...,"CloudWatchMetrics":[{"Namespace":"ns","Dimensions":[["function_name","service"]],"Metrics":[{"Name":"ColdStart","Unit":"Count"}]}]},"function_name":"fn","service":"svc","ColdStart":[1.0]}
{"_aws":{"Timestamp":...,"CloudWatchMetrics":[{"Namespace":"ns","Dimensions":[[]],"Metrics":[{"Name":"Single","Unit":"Count"}]}]},"Single":[1.0]}
{"_aws": {"Timestamp": ..., "CloudWatchMetrics": [{"Namespace": "ns", "Dimensions": [["service"]], "Metrics": [{"Name": "metric_0", "Unit": "Count"}, ...
Possible Solution
Check is_metrics_disabled() at each of those points, like flush_metrics() does:
single_metric: only print whenmetric_set is not None and not is_metrics_disabled()(which also stops thenullline).- CloudWatch
add_metric: skip the print on auto-flush when disabled, but still clearmetric_setas today. - Datadog: skip the
lambda_metricsubmission when disabled.
Happy to send a PR with tests for each path.
Steps to Reproduce
Run the snippet above.
Powertools for AWS Lambda (Python) version
latest (develop @ 5785377)
AWS Lambda function runtime
3.11
Packaging format used
PyPi
Debugging logs
No response
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsTriage
Activity
leandrodamascena commentedon Oct 25, 2026
Thanks @soujasK, confirmed on
develop: withPOWERTOOLS_METRICS_DISABLED=truetheColdStartline,single_metricand the 100-metric auto-flush all still print, and in Lambda those become real metrics. Checkingis_metrics_disabled()at each of those points, plus not printingnullwhen thesingle_metricbody raises, is the right fix. It's yours, one test per path please. This goes into 3.40.0.soujasK commentedon Oct 25, 2026
Thanks @leandrodamascena! PR #8668 is up with one test per path (
single_metric, cold start vialog_metrics, the 100-metric auto-flush, Datadoglambda_metric), plus thenullcase and aPOWERTOOLS_DEVcheck forsingle_metric.