Skip to content

Commit f8fe2a3

Browse files
fix(metrics): add warning for invalid dimension values; prevent their addition to EMF blobs (#5542)
1 parent d111f3a commit f8fe2a3

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,20 @@ def add_dimension(self, name: str, value: str) -> None:
272272
raise SchemaValidationError(
273273
f"Maximum number of dimensions exceeded ({MAX_DIMENSIONS}): Unable to add dimension {name}.",
274274
)
275-
# Cast value to str according to EMF spec
276-
# Majority of values are expected to be string already, so
277-
# checking before casting improves performance in most cases
278-
self.dimension_set[name] = value if isinstance(value, str) else str(value)
275+
276+
value = value if isinstance(value, str) else str(value)
277+
278+
if not name.strip() or not value.strip():
279+
warnings.warn(
280+
f"The dimension {name} doesn't meet the requirements and won't be added. "
281+
"Ensure the dimension name and value are non empty strings",
282+
stacklevel=2,
283+
)
284+
else:
285+
# Cast value to str according to EMF spec
286+
# Majority of values are expected to be string already, so
287+
# checking before casting improves performance in most cases
288+
self.dimension_set[name] = value
279289

280290
def add_metadata(self, key: str, value: Any) -> None:
281291
"""Adds high cardinal metadata for metrics object

docs/core/metrics.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ If you're new to Amazon CloudWatch, there are five terminologies you must be awa
2525
* **Resolution**. It's a value representing the storage resolution for the corresponding metric. Metrics can be either Standard or High resolution. Read more [here](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/publishingMetrics.html#high-resolution-metrics){target="_blank"}.
2626

2727
<figure>
28-
<img src="../../media/metrics_terminology.png" />
28+
<img src="../../media/metrics_terminology.png" alt="Terminology" />
2929
<figcaption>Metric terminology, visually explained</figcaption>
3030
</figure>
3131

@@ -131,6 +131,8 @@ If you'd like to remove them at some point, you can use `clear_default_dimension
131131
--8<-- "examples/metrics/src/set_default_dimensions_log_metrics.py"
132132
```
133133

134+
**Note:** Dimensions with empty values will not be included.
135+
134136
### Changing default timestamp
135137

136138
When creating metrics, we use the current timestamp. If you want to change the timestamp of all the metrics you create, utilize the `set_timestamp` function. You can specify a datetime object or an integer representing an epoch timestamp in milliseconds.

tests/functional/metrics/required_dependencies/test_metrics_cloudwatch_emf.py

+19
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,25 @@ def test_clear_default_dimensions(namespace):
10451045
assert not my_metrics.default_dimensions
10461046

10471047

1048+
def test_add_dimensions_with_empty_value(namespace, capsys, metric):
1049+
# GIVEN Metrics is initialized
1050+
my_metrics = Metrics(namespace=namespace)
1051+
1052+
my_dimension = "my_empty_dimension"
1053+
1054+
# WHEN we try to add a dimension with empty value
1055+
with pytest.warns(UserWarning, match=f"The dimension {my_dimension} doesn't meet the requirements *"):
1056+
my_metrics.add_dimension(name="my_empty_dimension", value=" ")
1057+
1058+
my_metrics.add_metric(**metric)
1059+
my_metrics.flush_metrics()
1060+
1061+
output = capture_metrics_output(capsys)
1062+
1063+
# THEN the serialized metric should not contain this dimension
1064+
assert my_dimension not in output
1065+
1066+
10481067
def test_get_and_set_namespace_and_service_properties(namespace, service, metrics, capsys):
10491068
# GIVEN Metrics instance is initialized without namespace and service
10501069
my_metrics = Metrics()

0 commit comments

Comments
 (0)