Skip to content

Commit 7a54af5

Browse files
authored
fix(metrics): skip non-finite metric values with a warning (#8797)
* fix(metrics): skip non-finite metric values with a warning * fix(metrics): don't print null when the single_metric body raises
1 parent c887a23 commit 7a54af5

5 files changed

Lines changed: 76 additions & 2 deletions

File tree

‎.agents/skills/building-lambda-functions-with-powertools-python/references/metrics.md‎

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎aws_lambda_powertools/metrics/base.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import functools
1111
import json
1212
import logging
13+
import math
1314
import numbers
1415
import os
1516
import warnings
@@ -38,6 +39,7 @@
3839
)
3940
from aws_lambda_powertools.shared import constants
4041
from aws_lambda_powertools.shared.functions import is_durable_context, resolve_env_var_choice
42+
from aws_lambda_powertools.warnings import PowertoolsUserWarning
4143

4244
if TYPE_CHECKING:
4345
from collections.abc import Callable, Generator
@@ -152,6 +154,14 @@ def add_metric(
152154

153155
unit = self._extract_metric_unit_value(unit=unit)
154156
resolution = self._extract_metric_resolution_value(resolution=resolution)
157+
if not math.isfinite(value):
158+
warnings.warn(
159+
f"The metric {name} has a non-finite value ({value}) and won't be added. "
160+
"CloudWatch EMF only accepts finite numbers.",
161+
category=PowertoolsUserWarning,
162+
stacklevel=2,
163+
)
164+
return
155165
metric: dict = self.metric_set.get(name, defaultdict(list))
156166
metric["Unit"] = unit
157167
metric["StorageResolution"] = resolution
@@ -657,6 +667,9 @@ def single_metric(
657667
metric.add_dimension(name=dim_name, value=dim_value)
658668

659669
yield metric
660-
metric_set = metric.serialize_metric_set()
670+
# add_metric drops a non-finite value with a warning, leaving nothing to publish
671+
if metric.metric_set:
672+
metric_set = metric.serialize_metric_set()
661673
finally:
662-
print(json.dumps(metric_set, separators=(",", ":")))
674+
if metric_set is not None:
675+
print(json.dumps(metric_set, separators=(",", ":")))

‎aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import datetime as dt
44
import json
55
import logging
6+
import math
67
import numbers
78
import os
89
import warnings
@@ -165,6 +166,14 @@ def add_metric(
165166
metric_resolutions=self._metric_resolutions,
166167
resolution=resolution,
167168
)
169+
if not math.isfinite(value):
170+
warnings.warn(
171+
f"The metric {name} has a non-finite value ({value}) and won't be added. "
172+
"CloudWatch EMF only accepts finite numbers.",
173+
category=PowertoolsUserWarning,
174+
stacklevel=2,
175+
)
176+
return
168177
metric: dict = self.metric_set.get(name, defaultdict(list))
169178
metric["Unit"] = unit
170179
metric["StorageResolution"] = resolution

‎docs/core/metrics/index.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ You can create metrics using `add_metric`, and you can create dimensions for all
8181
???+ note "Note: Metrics overflow"
8282
CloudWatch EMF supports a max of 100 metrics per batch. Metrics utility will flush all metrics when adding the 100th metric. Subsequent metrics (101th+) will be aggregated into a new EMF object, for your convenience.
8383

84+
???+ note "Note: Non-finite values"
85+
CloudWatch EMF only accepts finite numbers. `add_metric` skips a `NaN` or infinite value and emits a `PowertoolsUserWarning`, so the other metrics in the batch are still published.
86+
8487
???+ warning "Warning: Do not create metrics or dimensions outside the handler"
8588
Metrics or dimensions added in the global scope will only be added during cold start. Disregard if that's the intended behavior.
8689

‎tests/functional/metrics/required_dependencies/test_metrics_cloudwatch_emf.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
MIN_METRIC_NAME_LENGTH,
3030
)
3131
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.exceptions import MetricNameError
32+
from aws_lambda_powertools.warnings import PowertoolsUserWarning
3233

3334
if TYPE_CHECKING:
3435
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.types import (
@@ -1251,6 +1252,50 @@ def test_add_dimensions_with_empty_value(namespace, capsys, metric):
12511252
assert my_dimension not in output
12521253

12531254

1255+
def _reject_non_finite(constant: str):
1256+
raise ValueError(f"EMF output contains {constant}, which is not valid JSON")
1257+
1258+
1259+
@pytest.mark.parametrize("value", [float("nan"), float("inf"), float("-inf")])
1260+
def test_add_metric_with_non_finite_value(namespace, capsys, metric, dimension, value):
1261+
# GIVEN Metrics is initialized
1262+
my_metrics = Metrics(namespace=namespace)
1263+
my_metrics.add_dimension(**dimension)
1264+
1265+
# WHEN we add a non-finite metric value next to a valid metric
1266+
with pytest.warns(PowertoolsUserWarning, match="The metric bad_metric has a non-finite value *"):
1267+
my_metrics.add_metric(name="bad_metric", unit=MetricUnit.Count, value=value)
1268+
my_metrics.add_metric(**metric)
1269+
my_metrics.flush_metrics()
1270+
1271+
# THEN the EMF output is valid JSON and only contains the valid metric
1272+
output = json.loads(capsys.readouterr().out.strip(), parse_constant=_reject_non_finite)
1273+
assert "bad_metric" not in output
1274+
assert output[metric["name"]] == [1.0]
1275+
1276+
1277+
def test_single_metric_with_non_finite_value(namespace, capsys):
1278+
# GIVEN a single metric with a NaN value
1279+
# WHEN the context manager exits
1280+
with pytest.warns(PowertoolsUserWarning, match="The metric bad_metric has a non-finite value *"):
1281+
with single_metric(name="bad_metric", unit=MetricUnit.Count, value=float("nan"), namespace=namespace):
1282+
pass
1283+
1284+
# THEN nothing is published and no exception is raised
1285+
assert capsys.readouterr().out == ""
1286+
1287+
1288+
def test_single_metric_body_raises(namespace, capsys):
1289+
# GIVEN a single metric whose context manager body raises
1290+
# WHEN the exception leaves the context manager
1291+
with pytest.raises(RuntimeError, match="boom"):
1292+
with single_metric(name="my_metric", unit=MetricUnit.Count, value=1, namespace=namespace):
1293+
raise RuntimeError("boom")
1294+
1295+
# THEN no metric or `null` line is printed
1296+
assert capsys.readouterr().out == ""
1297+
1298+
12541299
def test_get_and_set_namespace_and_service_properties(namespace, service, metrics, capsys):
12551300
# GIVEN Metrics instance is initialized without namespace and service
12561301
my_metrics = Metrics()

0 commit comments

Comments
 (0)