Bug: POWERTOOLS_METRICS_DISABLED is ignored by single_metric, the cold start metric, add_metric auto-flush and Datadog's lambda_metric

未关闭
#8,667 2 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 1 天内回复

@soujasK 已经在做这个了。

开始于 2026年10月25日。

  • #8668 来自 @soujasK —— 未关闭

评估

这个 Issue 还没有评估数据。

描述

bug metrics
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:

  1. single_metric (metrics/base.py) always prints in its finally block. This is also how the CloudWatch provider emits the ColdStart metric, so @metrics.log_metrics(capture_cold_start_metric=True) still prints a ColdStart EMF line while the application metrics are correctly suppressed.
  2. The CloudWatch add_metric auto-flush: when the metric set reaches 100 metrics, or a metric reaches 100 values, AmazonCloudWatchEMFProvider.add_metric prints the EMF blob directly.
  3. Datadog with the datadog_lambda package installed: DatadogProvider.flush_metrics calls lambda_metric(...) without checking is_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 blob

Output (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 when metric_set is not None and not is_metrics_disabled() (which also stops the null line).
  • CloudWatch add_metric: skip the print on auto-flush when disabled, but still clear metric_set as today.
  • Datadog: skip the lambda_metric submission 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

主要语言
Python
星标
3.3k
派生
511
平均合并
3 天 4 小时
30 天内合并 PR
98

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

aws-powertools/powertools-lambda-python 的其他 Issue

查看 aws-powertools/powertools-lambda-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。