Skip to content

Commit f4fdcba

Browse files
authored
fix(metrics): raise SchemaValidationError for >8 metric dimensions (#1240)
1 parent f62d07a commit f4fdcba

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

Diff for: aws_lambda_powertools/metrics/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ def serialize_metric_set(
178178
metadata = self.metadata_set
179179

180180
if self.service and not self.dimension_set.get("service"):
181-
self.dimension_set["service"] = self.service
181+
# self.service won't be a float
182+
self.add_dimension(name="service", value=self.service) # type: ignore[arg-type]
182183

183184
if len(metrics) == 0:
184185
raise SchemaValidationError("Must contain at least one metric.")

Diff for: docs/core/metrics.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ If you're new to Amazon CloudWatch, there are two terminologies you must be awar
3030

3131
Metric has two global settings that will be used across all metrics emitted:
3232

33-
Setting | Description | Environment variable | Constructor parameter
34-
------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------- | -------------------------------------------------
35-
**Metric namespace** | Logical container where all metrics will be placed e.g. `ServerlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace`
36-
**Service** | Optionally, sets **service** metric dimension across all metrics e.g. `payment` | `POWERTOOLS_SERVICE_NAME` | `service`
33+
| Setting | Description | Environment variable | Constructor parameter |
34+
| -------------------- | ------------------------------------------------------------------------------- | ------------------------------ | --------------------- |
35+
| **Metric namespace** | Logical container where all metrics will be placed e.g. `ServerlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace` |
36+
| **Service** | Optionally, sets **service** metric dimension across all metrics e.g. `payment` | `POWERTOOLS_SERVICE_NAME` | `service` |
3737

3838
???+ tip
3939
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
191191
???+ tip "Tip: Metric validation"
192192
If metrics are provided, and any of the following criteria are not met, **`SchemaValidationError`** exception will be raised:
193193

194-
* Maximum of 9 dimensions
194+
* Maximum of 8 user-defined dimensions
195195
* Namespace is set, and no more than one
196196
* Metric units must be [supported by CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html)
197197

Diff for: tests/functional/test_metrics.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,11 @@ def test_schema_no_metrics(service, namespace):
319319
my_metrics.serialize_metric_set()
320320

321321

322-
def test_exceed_number_of_dimensions(metric, namespace):
322+
def test_exceed_number_of_dimensions(metric, namespace, monkeypatch):
323323
# GIVEN we we have more dimensions than CloudWatch supports
324-
dimensions = [{"name": f"test_{i}", "value": "test"} for i in range(11)]
324+
# and that service dimension is injected like a user-defined dimension (N+1)
325+
monkeypatch.setenv("POWERTOOLS_SERVICE_NAME", "test_service")
326+
dimensions = [{"name": f"test_{i}", "value": "test"} for i in range(9)]
325327

326328
# WHEN we attempt to serialize them into a valid EMF object
327329
# THEN it should fail validation and raise SchemaValidationError

0 commit comments

Comments
 (0)