← Back to Pull requests

fix(metrics): skip non-finite metric values with a warning #8797

Closed
unknown closed this pull request without merging on Nov 15, 2026 13:41 UTC
View on GitHub
unknown unknown commented on Nov 10, 2026 22:56 UTC
Author

<!-- markdownlint-disable MD041 MD043 -->
Issue number: closes #8796

Summary

Changes

add_metric in both AmazonCloudWatchEMFProvider and MetricManager (used by single_metric) now checks math.isfinite(value) after the unit and resolution checks, so a bad unit still raises as before. A NaN or infinite value is skipped with a PowertoolsUserWarning, per the discussion in the issue.

single_metric doesn't flush when its only value was skipped. Otherwise the empty metric set would raise SchemaValidationError: Must contain at least one metric., where today it raises nothing.

It also no longer prints a null line when the with body raises. 3.40.0 does that too, but this block is rewritten here anyway (suggested in review).

Added a docs note under "Creating metrics" and the regenerated agent skill reference for it (I left out the unrelated layer version bump the script also picked up).

User experience

Before: metrics.add_metric(name="Latency", unit=MetricUnit.Milliseconds, value=float("nan")) writes a bare NaN into the EMF line, so CloudWatch drops every metric in that flush without an error. After: a warning names the metric, it's left out, and the rest of the flush is published.

One thing to check: with raise_on_empty_metrics=True, a flush where every value was skipped now raises SchemaValidationError, like any empty flush. Happy to change that if you'd rather it didn't.

Verification

Added test_add_metric_with_non_finite_value (nan, inf, -inf, parses the output with parse_constant so a bare constant fails) and test_single_metric_with_non_finite_value in tests/functional/metrics/required_dependencies/test_metrics_cloudwatch_emf.py. Also added test_single_metric_body_raises. All 5 fail on develop and pass with the fix. pytest tests/unit/metrics tests/functional/metrics on Python 3.12: 170 passed, 1 failed (test_datadog_write_to_log_with_env_variable, which fails the same way on develop when run with the rest of the folder and passes alone). ruff format, ruff check and mypy are clean apart from time.tzset in a logger example, which mypy flags on Windows. I didn't send the output to CloudWatch.

I used an AI assistant (Claude Code) for this change.

---

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

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

Diff: 317 lines (+237 / -80) across 3 files Reviewer: blacksoxx Review duration: 6645m
All Vouch review confidence checks passed
Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 317 lines diff across 3 files
0.40
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.345
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #8797"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 40.4% (MEDIUM) · Higher = larger blast radius
40.4%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 16.0% · Safe to merge
16.0%