Skip to content

fix(metrics): reject non-finite metric values with warning - #8815

Closed
dhairyakhandelwal880-dotcom wants to merge 2 commits into
aws-powertools:developfrom
dhairyakhandelwal880-dotcom:fix/metrics-reject-nan-infinity
Closed

dhairyakhandelwal880-dotcom wants to merge 2 commits into
aws-powertools:developfrom
dhairyakhandelwal880-dotcom:fix/metrics-reject-nan-infinity

Conversation

@dhairyakhandelwal880-dotcom

@dhairyakhandelwal880-dotcom dhairyakhandelwal880-dotcom commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Issue number: closes #8796

Summary

Changes

  • Add math.isfinite(float(value)) checks in MetricManager.add_metric (aws_lambda_powertools/metrics/base.py), AmazonCloudWatchEMFProvider.add_metric (aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py), and DatadogProvider.add_metric (aws_lambda_powertools/metrics/provider/datadog/datadog.py).
  • Warn with PowertoolsUserWarning and skip non-finite values (NaN, inf, -inf) per ADR 0006, preserving all other valid metrics in the flush.
  • Add functional tests verifying warning emission and skipping of non-finite values across Metrics, AmazonCloudWatchEMFProvider, SingleMetric, and DatadogMetrics.

User experience

Before this change, passing a NaN or infinite value to add_metric (e.g. from division by zero in an average calculation) passed the isinstance(value, numbers.Number) check. At flush time, json.dumps emitted bare NaN / Infinity, corrupting the entire EMF line for all metrics in that flush into invalid JSON.

After this change, non-finite values are caught with a clear PowertoolsUserWarning, skipped, and all other valid metrics in the flush are serialized and published successfully without breaking the EMF line.

Verification

  • Ran pytest on functional test suites:
    • tests/functional/metrics/required_dependencies/test_metrics_cloudwatch_emf.py: 102 passed
    • tests/functional/metrics/datadog/test_metrics_datadog.py: 35 passed
  • Validated formatting and linting:
    • ruff check --cache-dir /tmp/ruff_cache aws_lambda_powertools/metrics/ tests/functional/metrics/: 0 errors
    • ruff format --check --cache-dir /tmp/ruff_cache aws_lambda_powertools/metrics/ tests/functional/metrics/: 0 modifications
  • Verified with AI assistant and personally validated all test runs.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

…tools#8796)

Signed-off-by: dhairyakhandelwal880-dotcom <dhairyakhandelwal880@gmail.com>
… assertions

Signed-off-by: dhairyakhandelwal880-dotcom <dhairyakhandelwal880@gmail.com>
@sonarqubecloud

Copy link
Copy Markdown

@leandrodamascena

Copy link
Copy Markdown
Contributor

Thanks for the PR @dhairyakhandelwal880-dotcom. This one is already fixed in #8797, which was linked on the issue and approved before yours came in, so I'm closing this as a duplicate. Your #8814 is reviewed and good, see there. Next time, a quick look at the issue for an existing PR saves you the work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

metrics size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants