Bug: POWERTOOLS_METRICS_DISABLED is ignored by single_metric, the cold start metric, add_metric auto-flush and Datadog's lambda_metric
メンテナーはふだん 1 日以内に返信
評価
この issue はまだ評価されていません。
説明
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 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 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
- 主要言語
- Python
- スター
- 3.3k
- フォーク
- 511
- 平均マージ
- 3日 4時間
- マージ済み PR(30日)
- 98
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
aws-powertools/powertools-lambda-python のほかの issue
-
Bug: POWERTOOLS_IDEMPOTENCY_DISABLED set to an empty or non-boolean value crashes every invocation対応中かも @lechakrawarthy が 2 日前に担当しました。 オープンbug good first issue help wanted idempotency
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
aws-powertools/powertools-lambda-python#8728 · コメント 4 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Bug: Redis layer fails the invocation instead of retrying when a record expires between SET NX and GET対応中かも @coughlmao が 2 日前に担当しました。 オープンbug good first issue help wanted idempotency
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
aws-powertools/powertools-lambda-python#8727 · コメント 4 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Bug: Redis orphan lock timeout ignores expires_after_seconds and is always 10 seconds対応中かも @CJstate が 2 日前に担当しました。 オープンbug good first issue help wanted idempotency
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
aws-powertools/powertools-lambda-python#8726 · コメント 3 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Bug: idempotency payloads with set, frozenset or bytes fields cannot be hashed対応中かも @Neeraj111010 が 2 日前に担当しました。 オープンbug good first issue help wanted idempotency
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
aws-powertools/powertools-lambda-python#8725 · コメント 6 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Bug: with a DynamoDB composite key, DataRecord.idempotency_key is the static partition key value対応中かも @sumedh-aerram が 2 日前に担当しました。 オープンbug good first issue help wanted idempotency
難易度 2/5 1〜3時間 初心者へのやさしさ 90/100
aws-powertools/powertools-lambda-python#8724 · コメント 3 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
aws-powertools/powertools-lambda-python の issue をすべて見る
似ている issue
-
bump patch
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
autowarefoundation/autoware_carla_scenario#1321 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1〜3時間 初心者へのやさしさ 78/100
QuantConnect/Documentation#2838 ·
メンテナーはふだん 1 日以内に返信
-
get_all_subclasses must deduplicate by class identity対応中かも @Gambit-Checkmate が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 15 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
awslabs/route53-dynamic-dns-with-lambda#67 · コメント 1 件 ·