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