Skip to content

Commit 8648078

Browse files
anafalcaoAna Falcao
and
Ana Falcao
authored
feat(metrics): warn when overwriting dimension (#5653)
* add warning to metrics overwrite dimensions * resolve conflicts cloudwatch file --------- Signed-off-by: Ana Falcão <[email protected]> Co-authored-by: Ana Falcao <[email protected]>
1 parent fb245cc commit 8648078

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.metric_properties import MetricResolution, MetricUnit
2323
from aws_lambda_powertools.shared import constants
2424
from aws_lambda_powertools.shared.functions import resolve_env_var_choice
25+
from aws_lambda_powertools.warnings import PowertoolsUserWarning
26+
2527

2628
if TYPE_CHECKING:
2729
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.types import CloudWatchEMFOutput
@@ -278,14 +280,22 @@ def add_dimension(self, name: str, value: str) -> None:
278280
if not name.strip() or not value.strip():
279281
warnings.warn(
280282
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",
283+
"Ensure the dimension name and value are non-empty strings",
284+
category=PowertoolsUserWarning,
282285
stacklevel=2,
283286
)
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
287+
return
288+
289+
if name in self.dimension_set or name in self.default_dimensions:
290+
warnings.warn(
291+
f"Dimension '{name}' has already been added. The previous value will be overwritten.",
292+
category=PowertoolsUserWarning,
293+
stacklevel=2,
294+
)
295+
296+
self.dimension_set[name] = value
297+
298+
289299

290300
def add_metadata(self, key: str, value: Any) -> None:
291301
"""Adds high cardinal metadata for metrics object
@@ -294,7 +304,7 @@ def add_metadata(self, key: str, value: Any) -> None:
294304
Instead, this will be searchable through logs.
295305
296306
If you're looking to add metadata to filter metrics, then
297-
use add_dimensions method.
307+
use add_dimension method.
298308
299309
Example
300310
-------

tests/unit/metrics/test_functions.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import warnings
23

34
from aws_lambda_powertools.metrics.functions import (
45
extract_cloudwatch_metric_resolution_value,
@@ -9,6 +10,18 @@
910
MetricUnitError,
1011
)
1112
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.metric_properties import MetricResolution, MetricUnit
13+
from aws_lambda_powertools.metrics import Metrics
14+
from aws_lambda_powertools.warnings import PowertoolsUserWarning
15+
16+
@pytest.fixture
17+
def warning_catcher(monkeypatch):
18+
caught_warnings = []
19+
20+
def custom_warn(message, category=None, stacklevel=1, source=None):
21+
caught_warnings.append(PowertoolsUserWarning(message))
22+
23+
monkeypatch.setattr(warnings, 'warn', custom_warn)
24+
return caught_warnings
1225

1326

1427
def test_extract_invalid_cloudwatch_metric_resolution_value():
@@ -61,3 +74,31 @@ def test_extract_valid_cloudwatch_metric_unit_value():
6174

6275
# THEN value must be extracted
6376
assert extracted_unit_value == unit
77+
78+
79+
def test_add_dimension_overwrite_warning(warning_catcher):
80+
"""
81+
Adds a dimension and then tries to add another with the same name
82+
but a different value. Verifies if the dimension is updated with
83+
the new value and warning is issued when an existing dimension
84+
is overwritten.
85+
"""
86+
metrics = Metrics(namespace="TestNamespace")
87+
88+
# GIVEN default dimension
89+
dimension_name = "test-dimension"
90+
value1 = "test-value-1"
91+
value2 = "test-value-2"
92+
93+
# WHEN adding the same dimension twice with different values
94+
metrics.add_dimension(dimension_name, value1)
95+
metrics.add_dimension(dimension_name, value2)
96+
97+
# THEN the dimension should be updated with the new value
98+
assert metrics._dimensions[dimension_name] == value2
99+
100+
# AND a warning should be issued with the exact message
101+
expected_warning = f"Dimension '{dimension_name}' has already been added. The previous value will be overwritten."
102+
assert any(str(w) == expected_warning for w in warning_catcher)
103+
104+

0 commit comments

Comments
 (0)