Bug: Metrics keeps dimensions and metadata from an invocation that added no metrics
Maintainer antworten meist innerhalb von 1 Tag
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 79/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- aws, python
- Bereich
- backend, observability
Rechercherichtung
Beginne mit AmazonCloudWatchEMFProvider.flush_metrics() und dem bestehenden Verhalten von clear_metrics(); das Issue nennt keine Dateipfade oder Testnamen. Prüfe, wie sich das Flushen verhält, wenn keine Metriken ausgegeben werden, die Validierung eine Ausnahme auslöst oder Metriken deaktiviert sind, und ergänze oder aktualisiere die Abdeckung für diese Fälle. Fertig ist die Arbeit, wenn jeder Flush die Aufrufdaten löscht und die Standarddimensionen wiederherstellt, auch wenn dabei eine Warnung ausgegeben oder eine Ausnahme ausgelöst wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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
- Vorherrschende Sprache
- Python
- Sterne
- 3.3k
- Forks
- 511
- Ø Merge
- 2 T. 23 Std.
- Gemergte PRs (30 T.)
- 50
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus aws-powertools/powertools-lambda-python
-
Bug: CaseInsensitiveDict (event.headers) returns True for both == and != against the same mappingOffenbug triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
aws-powertools/powertools-lambda-python#8948 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Docs: child Logger without a parent uses the root log level since #8842Evtl. vergeben @rdxrahul12 hat das heute übernommen. Offendocumentation triage
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 85/100
aws-powertools/powertools-lambda-python#8935 · 3 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Bug: Set-Cookie values passed as a header are comma-joined into one invalid header on HTTP API and Function URL responsesEvtl. vergeben @saquibjawedbit hat das vor 4 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
aws-powertools/powertools-lambda-python#8921 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Bug: validate() raises raw jmespath ParseError instead of InvalidEnvelopeExpressionError for most malformed envelopesEvtl. vergeben @0x5t4l1n hat das heute übernommen. Offenbug triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
aws-powertools/powertools-lambda-python#8918 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Bug: SQSRecord.decoded_nested_sns_event raises KeyError on SNS messages delivered to SQSEvtl. vergeben @hyeonsang010716 hat das vor 7 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 79/100
aws-powertools/powertools-lambda-python#8913 ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in aws-powertools/powertools-lambda-python
Ähnliche Issues
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
litestar-org/sqlspec#946 ·
Maintainer antworten meist innerhalb von 1 Tag
-
area:cli area:docs Platform vdr
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
NVIDIA-NeMo/Gym#7192 ·
Maintainer antworten meist innerhalb von 2 Tagen
-
SofyaBotOffen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 66/100
ai-robots-txt/ai.robots.txt#324 ·
-
[Resource]: DashbarOffenresource-submission validation-passed
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 78/100
hesreallyhim/awesome-claude-code#3821 · 1 Kommentar ·
-
bug good first issue help wanted level 1 wocs
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 92/100
GianlucaBave/murmury#169 ·
Maintainer antworten meist innerhalb von 1 Tag