Bug: Metrics keeps dimensions and metadata from an invocation that added no metrics
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 79/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- aws, python
- 領域
- backend, observability
調査の方向性
AmazonCloudWatchEMFProvider.flush_metrics() と既存の clear_metrics() の動作から始めてください。issue にはファイルパスやテスト名が記載されていません。メトリクスが出力されない場合、検証で例外が発生する場合、またはメトリクスが無効になっている場合に、フラッシュがどのように動作するかを確認し、それらのケースのカバレッジを追加または更新してください。警告が出された場合や例外が発生した場合も含め、各フラッシュで呼び出しデータが消去され、デフォルトのディメンションが復元されれば完了です。
索引モデルが 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:
- The invocation added dimensions or metadata but no metrics, for example a handler that returns early.
log_metricswarnsNo application metrics to publishand keeps everything in memory. raise_on_empty_metrics=TrueraisesSchemaValidationErrorbefore the clear.POWERTOOLS_METRICS_DISABLEDis set. Metric values keep piling up across invocations, and every invocation that sets the same dimension again warnsDimension '...' 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
- Decorate a handler with
@metrics.log_metrics. - Invoke it once so it adds a dimension or metadata and returns without adding a metric.
- Invoke it again so it adds a metric without that dimension.
- 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時間
- マージ済み PR(30日)
- 50
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
aws-powertools/powertools-lambda-python のほかの issue
-
Bug: CaseInsensitiveDict (event.headers) returns True for both == and != against the same mappingオープンbug triage
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
aws-powertools/powertools-lambda-python#8948 ·
メンテナーはふだん 1 日以内に返信
-
Docs: child Logger without a parent uses the root log level since #8842対応中かも @rdxrahul12 が今日担当しました。 オープンdocumentation triage
難易度 1/5 1〜3時間 初心者へのやさしさ 85/100
aws-powertools/powertools-lambda-python#8935 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
Bug: Set-Cookie values passed as a header are comma-joined into one invalid header on HTTP API and Function URL responses対応中かも @saquibjawedbit が 4 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
aws-powertools/powertools-lambda-python#8921 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
Bug: validate() raises raw jmespath ParseError instead of InvalidEnvelopeExpressionError for most malformed envelopes対応中かも @0x5t4l1n が今日担当しました。 オープンbug triage
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
aws-powertools/powertools-lambda-python#8918 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
Bug: SQSRecord.decoded_nested_sns_event raises KeyError on SNS messages delivered to SQS対応中かも @hyeonsang010716 が 7 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
aws-powertools/powertools-lambda-python#8913 ·
メンテナーはふだん 1 日以内に返信
aws-powertools/powertools-lambda-python の issue をすべて見る
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
litestar-org/sqlspec#946 ·
メンテナーはふだん 1 日以内に返信
-
area:cli area:docs Platform vdr
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
NVIDIA-NeMo/Gym#7192 ·
メンテナーはふだん 2 日以内に返信
-
SofyaBotオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
ai-robots-txt/ai.robots.txt#324 ·
-
[Resource]: Dashbarオープンresource-submission validation-passed
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
hesreallyhim/awesome-claude-code#3821 · コメント 1 件 ·
-
bug good first issue help wanted level 1 wocs
難易度 2/5 1〜3時間 初心者へのやさしさ 92/100
GianlucaBave/murmury#169 ·
メンテナーはふだん 1 日以内に返信