Skip to content

Bug: a NaN or infinite metric value makes the whole EMF line invalid JSON #8796

Open

Description

@anishmehta24

Expected Behaviour

add_metric should not accept a value CloudWatch can't store (nan, inf, -inf), the same way it rejects a value that isn't a number, so the other metrics in the same flush still get published. AWS's own aws-embedded-metrics-python raises Metric value must be finite for these.

Current Behaviour

The value check is isinstance(value, numbers.Number), which lets nan and ±inf through. At flush json.dumps writes them as bare NaN / Infinity, which isn't JSON, so the EMF line for the whole flush (every metric, the dimensions and the metadata) is no longer valid JSON. A nan from a division like total / count is enough to hit it.

I haven't sent one to CloudWatch; locally a strict JSON parser rejects the line. The same check is in metrics/base.py, used by single_metric.

Code snippet

import io, json, contextlib
from aws_lambda_powertools import Metrics
from aws_lambda_powertools.metrics import MetricUnit

metrics = Metrics(namespace="Shop", service="checkout")
metrics.add_metric(name="Orders", unit=MetricUnit.Count, value=1)
metrics.add_metric(name="AvgBasket", unit=MetricUnit.Count, value=float("nan"))

out = io.StringIO()
with contextlib.redirect_stdout(out):
    metrics.flush_metrics()

print(out.getvalue())
# {..."service":"checkout","Orders":[1.0],"AvgBasket":[NaN]}

def reject(constant):
    raise ValueError(constant)

json.loads(out.getvalue(), parse_constant=reject)  # ValueError: NaN

Steps to Reproduce

  1. Add a metric with value float("nan") or float("inf") next to a normal metric.
  2. Flush.
  3. The printed EMF line contains NaN / Infinity and is not valid JSON.

Possible Solution

Check math.isfinite(value) next to the existing type check in both add_metric methods. Since ADR 0006 asks for a warning before raising on input that is accepted today, it could warn with PowertoolsUserWarning and skip the value for now, then raise MetricValueError in the next major. I can open a PR once you've had a look.

Did you run the reproduction yourself?

Yes, I ran it and saw the failure

Powertools for AWS Lambda (Python) version

latest (develop, 3.40.0)

AWS Lambda function runtime

3.12

Packaging format used

PyPi

Debugging logs

No response

Activity

  1. leandrodamascena commented on Nov 10, 2026

    @leandrodamascena
    Contributor

    Thanks @anishmehta24, you're right and I had never thought about this one. I reproduced it: nan gets past the numbers.Number check, json.dumps writes it bare, and the whole EMF line goes out as invalid JSON. So one nan from a mean over an empty set takes the good metrics next to it down with it, and nobody sees an error. That's the part that bothers me.

    Let's do it the way you suggested: math.isfinite next to the type check in both add_metric paths, warn with PowertoolsUserWarning and drop the value so the rest of the flush still lands, and we raise in the next major. It's yours, go for it. I'm still catching up on the 3.41.0 queue, so the review may take me a few days.

  2. added this to the 3.41.0 milestone on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingmetrics

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions