Bug: Metrics keeps dimensions and metadata from an invocation that added no metrics

未关闭 适合新手
#8,911 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@hyeonsang010716 已经在做这个了。

开始于 2026年11月26日。

  • #8912 来自 @hyeonsang010716 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
79/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
aws, python

调研方向

从 AmazonCloudWatchEMFProvider.flush_metrics() 和现有的 clear_metrics() 行为入手;issue 没有提供文件路径或测试名称。检查在没有发出任何指标、验证引发异常或指标被禁用时,flush 的行为,然后为这些情况添加或更新测试覆盖。完成标准是每次 flush 都会清除调用数据并恢复默认维度,包括发出警告或引发异常时。

由索引模型根据 Issue 内容生成。

描述

Expected Behaviour

Each invocation only emits the dimensions, dimension sets and metadata it added, plus the default dimensions. The docs for flush_metrics() say it will "serialize, print metrics available to standard output, and clear in-memory metrics data".

Current Behaviour

AmazonCloudWatchEMFProvider.flush_metrics() only calls clear_metrics() after it prints an EMF blob. Nothing is cleared when the flush emits nothing:

  1. The invocation added dimensions or metadata but no metrics, for example a handler that returns early. log_metrics warns No application metrics to publish and keeps everything in memory.
  2. raise_on_empty_metrics=True raises SchemaValidationError before the clear.
  3. POWERTOOLS_METRICS_DISABLED is set. Metric values keep piling up across invocations, and every invocation that sets the same dimension again warns Dimension '...' has already been added.

In cases 1 and 2 the next invocation on the same warm container emits the previous invocation's dimensions, dimension sets and metadata, so its datapoints land in the wrong CloudWatch series.

This is separate from #8902, which is about the custom timestamp.

Code snippet
import warnings

from aws_lambda_powertools import Metrics
from aws_lambda_powertools.metrics import MetricUnit

warnings.simplefilter("ignore")
metrics = Metrics(namespace="Orders", service="orders")


@metrics.log_metrics
def lambda_handler(event, context):
    if event.get("coupon"):
        metrics.add_dimension(name="coupon", value=event["coupon"])
        metrics.add_metadata(key="coupon_owner", value="user-123")
    if not event.get("items"):
        return  # nothing to record
    metrics.add_metric(name="OrdersPlaced", unit=MetricUnit.Count, value=1)


lambda_handler({"coupon": "BLACKFRIDAY"}, None)  # adds no metric
lambda_handler({"items": [1]}, None)  # no coupon

Output of the second invocation:

{"_aws":{"Timestamp":1795652054040,"CloudWatchMetrics":[{"Namespace":"Orders","Dimensions":[["coupon","service"]],"Metrics":[{"Name":"OrdersPlaced","Unit":"Count"}]}]},"coupon":"BLACKFRIDAY","service":"orders","coupon_owner":"user-123","OrdersPlaced":[1.0]}

Expected "Dimensions":[["service"]], with no coupon and no coupon_owner.

Possible Solution

Call clear_metrics() in a finally block in flush_metrics(), so every flush clears the in-memory data whether it printed, warned or raised. clear_metrics() already restores the default dimensions.

Steps to Reproduce
  1. Decorate a handler with @metrics.log_metrics.
  2. Invoke it once so it adds a dimension or metadata and returns without adding a metric.
  3. Invoke it again so it adds a metric without that dimension.
  4. The second EMF blob contains the dimension and metadata from step 2.
Did you run the reproduction yourself?

Yes, I ran it and saw the failure

Powertools for AWS Lambda (Python) version

3.41.0

AWS Lambda function runtime

3.14

Packaging format used

PyPi

Debugging logs

主要语言
Python
星标
3.3k
派生
511
平均合并
2 天 23 小时
30 天内合并 PR
50

环境准备

从这里开始

  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 摘要。