Skip to content

fix(metrics): raise SchemaValidationError for >8 metric dimensions #1240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aws_lambda_powertools/metrics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def serialize_metric_set(
metadata = self.metadata_set

if self.service and not self.dimension_set.get("service"):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not specific from this PR, but won't the condition not self.dimension_set.get("service") blow up if the key is not present? Shouldn't it be something like not self.dimension_set.get("service", "")?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, because .get will return None as the default sentinel value, hence why it's suitable within the if condition here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically, and self.dimension_set.get("service") is not None would also work here ;-)

>>> a = {}
>>> not a.get("blah")
True
>>> a.get("blah") is None
True

self.dimension_set["service"] = self.service
self.add_dimension(name="service", value=self.service)

if len(metrics) == 0:
raise SchemaValidationError("Must contain at least one metric.")
Expand Down
10 changes: 5 additions & 5 deletions docs/core/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 4 additions & 2 deletions tests/functional/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down