fix(metrics): skip non-finite metric values with a warning #8797
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
blacksoxx
<!-- markdownlint-disable MD041 MD043 -->
Issue number: closes #8796
Summary
Changes
add_metricin bothAmazonCloudWatchEMFProviderandMetricManager(used bysingle_metric) now checksmath.isfinite(value)after the unit and resolution checks, so a bad unit still raises as before. ANaNor infinite value is skipped with aPowertoolsUserWarning, per the discussion in the issue.single_metricdoesn't flush when its only value was skipped. Otherwise the empty metric set would raiseSchemaValidationError: Must contain at least one metric., where today it raises nothing.It also no longer prints a
nullline when thewithbody 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 bareNaNinto 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 raisesSchemaValidationError, 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 withparse_constantso a bare constant fails) andtest_single_metric_with_non_finite_valueintests/functional/metrics/required_dependencies/test_metrics_cloudwatch_emf.py. Also addedtest_single_metric_body_raises. All 5 fail ondevelopand pass with the fix.pytest tests/unit/metrics tests/functional/metricson Python 3.12: 170 passed, 1 failed (test_datadog_write_to_log_with_env_variable, which fails the same way ondevelopwhen run with the rest of the folder and passes alone).ruff format,ruff checkandmypyare clean apart fromtime.tzsetin 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.