首页 > 解决方案 > AWS lambda powertools Metrics 仅记录循环中的最后一个指标

问题描述

我正在使用AWS Lambda Powertools Python,并且需要将多个数据点放在我的函数中的一个指标中。

使用Metrics Utility,我的代码如下所示

from aws_lambda_powertools import Metrics
from aws_lambda_powertools.metrics import MetricUnit

metrics = Metrics()
   
@metrics.log_metrics
def lambda_handler(event, context):
    # There are many things
    for thing in event["things"]:
        metrics.add_metric(
            name="MyMetric",
            unit=MetricUnit.Count,
            value=thing['value']
        )

使用此代码,只有最后一个 thing被放入 cloudwatch 指标。

如何确保所有数据都things发送到 cloudwatch 指标?

标签: python-3.xamazon-web-servicesaws-lambda

解决方案


推荐阅读