|
29 | 29 | MIN_METRIC_NAME_LENGTH, |
30 | 30 | ) |
31 | 31 | from aws_lambda_powertools.metrics.provider.cloudwatch_emf.exceptions import MetricNameError |
| 32 | +from aws_lambda_powertools.warnings import PowertoolsUserWarning |
32 | 33 |
|
33 | 34 | if TYPE_CHECKING: |
34 | 35 | from aws_lambda_powertools.metrics.provider.cloudwatch_emf.types import ( |
@@ -1251,6 +1252,50 @@ def test_add_dimensions_with_empty_value(namespace, capsys, metric): |
1251 | 1252 | assert my_dimension not in output |
1252 | 1253 |
|
1253 | 1254 |
|
| 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 | + |
1254 | 1299 | def test_get_and_set_namespace_and_service_properties(namespace, service, metrics, capsys): |
1255 | 1300 | # GIVEN Metrics instance is initialized without namespace and service |
1256 | 1301 | my_metrics = Metrics() |
|
0 commit comments