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 沒有提供檔案路徑或測試名稱。檢查在沒有發出任何指標、驗證引發例外或指標遭停用時,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:
- 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 小時
- 30 天內合併 PR
- 50
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 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-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 天內回覆