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
DatadogProvider.add_metric should reject or skip non-finite values (NaN, Infinity, -Infinity) with a warning, matching the behavior implemented for CloudWatch EMF in #8796 and #8797, so that non-finite values do not corrupt metric serialization or flush invalid JSON.
Current Behaviour
In aws_lambda_powertools/metrics/provider/datadog/datadog.py, add_metric only checks isinstance(value, numbers.Real). Since float("nan"), float("inf"), and float("-inf") are instances of numbers.Real, they pass validation and are appended to self.metric_set.
When flush_to_log=True, print(json.dumps(metric)) outputs bare NaN / Infinity, which is invalid JSON per RFC 8259 and causes downstream log processors and JSON parsers to fail. When flushed through datadog_lambda, Datadog expects 32-bit finite floats.
Add a metric with value=float("nan") or value=float("inf").
Call flush_metrics().
Observe invalid JSON emitted with bare NaN / Infinity.
Possible Solution
Add a check for math.isfinite(value) in DatadogProvider.add_metric:
ifnotmath.isfinite(value):
warnings.warn(
f"The metric {name} has a non-finite value ({value}) and won't be added. ""Datadog only accepts finite numbers.",
category=PowertoolsUserWarning,
stacklevel=2,
)
return
Activity