You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: a NaN or infinite metric value makes the whole EMF line invalid JSON #8796
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.
Add a metric with value float("nan") or float("inf") next to a normal metric.
Flush.
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.
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.
Activity
leandrodamascena commentedon Nov 10, 2026
Thanks @anishmehta24, you're right and I had never thought about this one. I reproduced it:
nangets past thenumbers.Numbercheck,json.dumpswrites it bare, and the whole EMF line goes out as invalid JSON. So onenanfrom 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.isfinitenext to the type check in bothadd_metricpaths, warn withPowertoolsUserWarningand 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.