From f4fdcbae2d55c8267c2b1c5d7435ecb77d993141 Mon Sep 17 00:00:00 2001 From: Heitor Lessa Date: Tue, 7 Jun 2022 16:41:36 +0200 Subject: [PATCH 1/2] fix(metrics): raise SchemaValidationError for >8 metric dimensions (#1240) --- aws_lambda_powertools/metrics/base.py | 3 ++- docs/core/metrics.md | 10 +++++----- tests/functional/test_metrics.py | 6 ++++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/aws_lambda_powertools/metrics/base.py b/aws_lambda_powertools/metrics/base.py index 48a6f019e40..fde191a8d94 100644 --- a/aws_lambda_powertools/metrics/base.py +++ b/aws_lambda_powertools/metrics/base.py @@ -178,7 +178,8 @@ def serialize_metric_set( metadata = self.metadata_set if self.service and not self.dimension_set.get("service"): - self.dimension_set["service"] = self.service + # self.service won't be a float + self.add_dimension(name="service", value=self.service) # type: ignore[arg-type] if len(metrics) == 0: raise SchemaValidationError("Must contain at least one metric.") diff --git a/docs/core/metrics.md b/docs/core/metrics.md index 99ee17106b3..713ae874fe6 100644 --- a/docs/core/metrics.md +++ b/docs/core/metrics.md @@ -30,10 +30,10 @@ If you're new to Amazon CloudWatch, there are two terminologies you must be awar Metric has two global settings that will be used across all metrics emitted: -Setting | Description | Environment variable | Constructor parameter -------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------- -**Metric namespace** | Logical container where all metrics will be placed e.g. `ServerlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace` -**Service** | Optionally, sets **service** metric dimension across all metrics e.g. `payment` | `POWERTOOLS_SERVICE_NAME` | `service` +| Setting | Description | Environment variable | Constructor parameter | +| -------------------- | ------------------------------------------------------------------------------- | ------------------------------ | --------------------- | +| **Metric namespace** | Logical container where all metrics will be placed e.g. `ServerlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace` | +| **Service** | Optionally, sets **service** metric dimension across all metrics e.g. `payment` | `POWERTOOLS_SERVICE_NAME` | `service` | ???+ tip Use your application or main service as the metric namespace to easily group all metrics. @@ -191,7 +191,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 9 dimensions + * Maximum of 8 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) diff --git a/tests/functional/test_metrics.py b/tests/functional/test_metrics.py index 4a5df151dd1..4dbefd34603 100644 --- a/tests/functional/test_metrics.py +++ b/tests/functional/test_metrics.py @@ -319,9 +319,11 @@ def test_schema_no_metrics(service, namespace): my_metrics.serialize_metric_set() -def test_exceed_number_of_dimensions(metric, namespace): +def test_exceed_number_of_dimensions(metric, namespace, monkeypatch): # GIVEN we we have more dimensions than CloudWatch supports - dimensions = [{"name": f"test_{i}", "value": "test"} for i in range(11)] + # and that service dimension is injected like a user-defined dimension (N+1) + monkeypatch.setenv("POWERTOOLS_SERVICE_NAME", "test_service") + dimensions = [{"name": f"test_{i}", "value": "test"} for i in range(9)] # WHEN we attempt to serialize them into a valid EMF object # THEN it should fail validation and raise SchemaValidationError From 9d8dd1b71a771be6c8a30a90a0dd855f02e0d758 Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Tue, 7 Jun 2022 16:44:07 +0200 Subject: [PATCH 2/2] chore: bump version 1.26.1 --- CHANGELOG.md | 17 +++++++++++++++++ pyproject.toml | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f8205e2526..03f7ad2919f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file. This project follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) format for changes and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 1.26.1 - 2022-06-07 +### Bug Fixes + +* **metrics:** raise SchemaValidationError for >8 metric dimensions ([#1240](https://github.com/awslabs/aws-lambda-powertools-python/issues/1240)) + +### Documentation + +* **governance:** link roadmap and maintainers doc +* **maintainers:** initial maintainers playbook ([#1222](https://github.com/awslabs/aws-lambda-powertools-python/issues/1222)) +* **roadmap:** use pinned pause issue instead + +### Maintenance + +* **deps-dev:** bump mypy from 0.950 to 0.960 ([#1224](https://github.com/awslabs/aws-lambda-powertools-python/issues/1224)) +* **deps-dev:** bump mypy-boto3-secretsmanager from 1.23.0.post1 to 1.23.8 ([#1225](https://github.com/awslabs/aws-lambda-powertools-python/issues/1225)) + + ## 1.26.0 - 2022-05-20 ### Bug Fixes diff --git a/pyproject.toml b/pyproject.toml index 4ed3dae87b7..9eeedcd63c5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "aws_lambda_powertools" -version = "1.26.0" +version = "1.26.1" description = "A suite of utilities for AWS Lambda functions to ease adopting best practices such as tracing, structured logging, custom metrics, batching, idempotency, feature flags, and more." authors = ["Amazon Web Services"] include = ["aws_lambda_powertools/py.typed", "THIRD-PARTY-LICENSES"]