From 17fd12f0317af6b425150cf1833a34414a9e4422 Mon Sep 17 00:00:00 2001 From: Leandro Damascena Date: Fri, 5 Aug 2022 00:03:09 +0100 Subject: [PATCH 1/4] improv(metrics): Adding support to 30 dimensions --- aws_lambda_powertools/metrics/base.py | 4 ++-- tests/functional/test_metrics.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/aws_lambda_powertools/metrics/base.py b/aws_lambda_powertools/metrics/base.py index fde191a8d94..29a780d0af1 100644 --- a/aws_lambda_powertools/metrics/base.py +++ b/aws_lambda_powertools/metrics/base.py @@ -14,7 +14,7 @@ logger = logging.getLogger(__name__) MAX_METRICS = 100 -MAX_DIMENSIONS = 9 +MAX_DIMENSIONS = 29 class MetricUnit(Enum): @@ -233,7 +233,7 @@ def add_dimension(self, name: str, value: str) -> None: Dimension value """ logger.debug(f"Adding dimension: {name}:{value}") - if len(self.dimension_set) == 9: + if len(self.dimension_set) == MAX_DIMENSIONS: raise SchemaValidationError( f"Maximum number of dimensions exceeded ({MAX_DIMENSIONS}): Unable to add dimension {name}." ) diff --git a/tests/functional/test_metrics.py b/tests/functional/test_metrics.py index 6cf33a3eaa4..78cc27029c3 100644 --- a/tests/functional/test_metrics.py +++ b/tests/functional/test_metrics.py @@ -320,8 +320,8 @@ def test_schema_no_metrics(service, namespace): def test_exceed_number_of_dimensions(metric, namespace): - # GIVEN we have more dimensions than CloudWatch supports - dimensions = [{"name": f"test_{i}", "value": "test"} for i in range(11)] + # GIVEN we have more dimensions than CloudWatch supports (N+1) + dimensions = [{"name": f"test_{i}", "value": "test"} for i in range(31)] # WHEN we attempt to serialize them into a valid EMF object # THEN it should fail validation and raise SchemaValidationError @@ -332,9 +332,9 @@ def test_exceed_number_of_dimensions(metric, namespace): def test_exceed_number_of_dimensions_with_service(metric, namespace, monkeypatch): - # GIVEN we have service set and add more dimensions than CloudWatch supports (N+1) + # GIVEN we have service set and add more dimensions than CloudWatch supports (N-1) monkeypatch.setenv("POWERTOOLS_SERVICE_NAME", "test_service") - dimensions = [{"name": f"test_{i}", "value": "test"} for i in range(9)] + dimensions = [{"name": f"test_{i}", "value": "test"} for i in range(29)] # WHEN we attempt to serialize them into a valid EMF object # THEN it should fail validation and raise SchemaValidationError From b0e76e8d327a857483dd55495c1efc57fce34596 Mon Sep 17 00:00:00 2001 From: Leandro Damascena Date: Fri, 5 Aug 2022 09:41:34 +0100 Subject: [PATCH 2/4] improv(metrics): Using MAX_DIMENSIONS constant in test file to avoid changes in future --- tests/functional/test_metrics.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/functional/test_metrics.py b/tests/functional/test_metrics.py index 78cc27029c3..68c3d46edf0 100644 --- a/tests/functional/test_metrics.py +++ b/tests/functional/test_metrics.py @@ -8,7 +8,7 @@ from aws_lambda_powertools import Metrics, single_metric from aws_lambda_powertools.metrics import MetricUnit, MetricUnitError, MetricValueError, SchemaValidationError from aws_lambda_powertools.metrics import metrics as metrics_global -from aws_lambda_powertools.metrics.base import MetricManager +from aws_lambda_powertools.metrics.base import MAX_DIMENSIONS, MetricManager @pytest.fixture(scope="function", autouse=True) @@ -321,7 +321,7 @@ def test_schema_no_metrics(service, namespace): def test_exceed_number_of_dimensions(metric, namespace): # GIVEN we have more dimensions than CloudWatch supports (N+1) - dimensions = [{"name": f"test_{i}", "value": "test"} for i in range(31)] + dimensions = [{"name": f"test_{i}", "value": "test"} for i in range(MAX_DIMENSIONS + 2)] # WHEN we attempt to serialize them into a valid EMF object # THEN it should fail validation and raise SchemaValidationError @@ -334,7 +334,7 @@ def test_exceed_number_of_dimensions(metric, namespace): def test_exceed_number_of_dimensions_with_service(metric, namespace, monkeypatch): # GIVEN we have service set and add more dimensions than CloudWatch supports (N-1) monkeypatch.setenv("POWERTOOLS_SERVICE_NAME", "test_service") - dimensions = [{"name": f"test_{i}", "value": "test"} for i in range(29)] + dimensions = [{"name": f"test_{i}", "value": "test"} for i in range(MAX_DIMENSIONS)] # WHEN we attempt to serialize them into a valid EMF object # THEN it should fail validation and raise SchemaValidationError From caaa61fe1a47fdffbeb817a4bb359cbfc61d8054 Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Fri, 5 Aug 2022 13:27:38 +0200 Subject: [PATCH 3/4] docs(metrics): update max dimensions Signed-off-by: heitorlessa --- docs/core/metrics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/metrics.md b/docs/core/metrics.md index 843e35b7eb8..45e3ce1a4c0 100644 --- a/docs/core/metrics.md +++ b/docs/core/metrics.md @@ -133,7 +133,7 @@ This decorator also **validates**, **serializes**, and **flushes** all your metr ???+ tip "Tip: Metric validation" If metrics are provided, and any of the following criteria are not met, **`SchemaValidationError`** exception will be raised: - * Maximum of 8 user-defined dimensions + * Maximum of 29 user-defined dimensions * Namespace is set, and no more than one * Metric units must be [supported by CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html) From 81a217178930f807665eeb19003472d75af75783 Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Fri, 5 Aug 2022 13:37:33 +0200 Subject: [PATCH 4/4] chore(tests): n+1 is sufficient to trigger exception Signed-off-by: heitorlessa --- tests/functional/test_metrics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functional/test_metrics.py b/tests/functional/test_metrics.py index 68c3d46edf0..1a52d84d4fe 100644 --- a/tests/functional/test_metrics.py +++ b/tests/functional/test_metrics.py @@ -321,7 +321,7 @@ def test_schema_no_metrics(service, namespace): def test_exceed_number_of_dimensions(metric, namespace): # GIVEN we have more dimensions than CloudWatch supports (N+1) - dimensions = [{"name": f"test_{i}", "value": "test"} for i in range(MAX_DIMENSIONS + 2)] + dimensions = [{"name": f"test_{i}", "value": "test"} for i in range(MAX_DIMENSIONS + 1)] # WHEN we attempt to serialize them into a valid EMF object # THEN it should fail validation and raise SchemaValidationError