Bug: Metrics keeps dimensions and metadata from an invocation that added no metrics
Les mainteneurs répondent en général sous 1 jour
Évaluation
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Accessibilité débutants
- 79/100
- Type d'issue
- Bug
- Clarté
- Clairement spécifiée
- Activité
- Active
- Stack technique
- aws, python
- Domaine
- backend, observability
Piste de recherche
Commencez par AmazonCloudWatchEMFProvider.flush_metrics() et le comportement existant de clear_metrics() ; l’issue ne donne ni chemins de fichiers ni noms de tests. Vérifiez le comportement du vidage lorsqu’aucune métrique n’est émise, lorsque la validation déclenche une exception ou lorsque les métriques sont désactivées, puis ajoutez ou mettez à jour la couverture pour ces cas. Le travail est terminé lorsque chaque vidage efface les données d’invocation et rétablit les dimensions par défaut, y compris lorsqu’un avertissement est émis ou qu’une exception est déclenchée.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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
- Langage dominant
- Python
- Étoiles
- 3.3k
- Forks
- 511
- Merge moyen
- 2 j 23 h
- PR mergées (30 j)
- 50
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de aws-powertools/powertools-lambda-python
-
Bug: CaseInsensitiveDict (event.headers) returns True for both == and != against the same mappingOuvertebug triage
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
aws-powertools/powertools-lambda-python#8948 ·
Les mainteneurs répondent en général sous 1 jour
-
Docs: child Logger without a parent uses the root log level since #8842Peut-être pris @rdxrahul12 l’a pris aujourd’hui. Ouvertedocumentation triage
Difficulté 1/5 1-3 heures Accessibilité débutants 85/100
aws-powertools/powertools-lambda-python#8935 · 3 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Bug: Set-Cookie values passed as a header are comma-joined into one invalid header on HTTP API and Function URL responsesPeut-être pris @saquibjawedbit l’a pris il y a 4 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 85/100
aws-powertools/powertools-lambda-python#8921 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Bug: validate() raises raw jmespath ParseError instead of InvalidEnvelopeExpressionError for most malformed envelopesPeut-être pris @0x5t4l1n l’a pris aujourd’hui. Ouvertebug triage
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
aws-powertools/powertools-lambda-python#8918 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Bug: SQSRecord.decoded_nested_sns_event raises KeyError on SNS messages delivered to SQSPeut-être pris @hyeonsang010716 l’a pris il y a 7 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 79/100
aws-powertools/powertools-lambda-python#8913 ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de aws-powertools/powertools-lambda-python
Issues similaires
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
litestar-org/sqlspec#946 ·
Les mainteneurs répondent en général sous 1 jour
-
area:cli area:docs Platform vdr
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
NVIDIA-NeMo/Gym#7192 ·
Les mainteneurs répondent en général sous 2 jours
-
SofyaBotOuverte
Difficulté 2/5 1-3 heures Accessibilité débutants 66/100
ai-robots-txt/ai.robots.txt#324 ·
-
[Resource]: DashbarOuverteresource-submission validation-passed
Difficulté 1/5 Moins d'une heure Accessibilité débutants 78/100
hesreallyhim/awesome-claude-code#3821 · 1 commentaire ·
-
bug good first issue help wanted level 1 wocs
Difficulté 2/5 1-3 heures Accessibilité débutants 92/100
GianlucaBave/murmury#169 ·
Les mainteneurs répondent en général sous 1 jour