Skip to content

Commit ffe7c97

Browse files
committed
Merge branch 'develop' into improv/tracer
* develop: Bugfix: "per second" metric units (#27)
2 parents 8fa07e6 + 70d9277 commit ffe7c97

File tree

4 files changed

+86
-20
lines changed

4 files changed

+86
-20
lines changed

Diff for: python/HISTORY.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,19 @@
1313

1414
**0.8.1**
1515

16-
* **Metrics**: Fix incorrect metric units enum values for `*PerSecond` e.g. CountPerSecond
16+
* **Metrics**: Fix metric unit casting logic if one passes plain string (value or key)
17+
* **Metrics: **Fix `MetricUnit` enum values for
18+
- `BytesPerSecond`
19+
- `KilobytesPerSecond`
20+
- `MegabytesPerSecond`
21+
- `GigabytesPerSecond`
22+
- `TerabytesPerSecond`
23+
- `BitsPerSecond`
24+
- `KilobitsPerSecond`
25+
- `MegabitsPerSecond`
26+
- `GigabitsPerSecond`
27+
- `TerabitsPerSecond`
28+
- `CountPerSecond`
1729

1830
## April 24th
1931

Diff for: python/aws_lambda_powertools/helper/models.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,17 @@ class MetricUnit(Enum):
8989
Terabits = "Terabits"
9090
Percent = "Percent"
9191
Count = "Count"
92-
BytesPerSecond = "Second"
93-
KilobytesPerSecond = "Second"
94-
MegabytesPerSecond = "Second"
95-
GigabytesPerSecond = "Second"
96-
TerabytesPerSecond = "Second"
97-
BitsPerSecond = "Second"
98-
KilobitsPerSecond = "Second"
99-
MegabitsPerSecond = "Second"
100-
GigabitsPerSecond = "Second"
101-
TerabitsPerSecond = "Second"
102-
CountPerSecond = "Second"
92+
BytesPerSecond = "Bytes/Second"
93+
KilobytesPerSecond = "Kilobytes/Second"
94+
MegabytesPerSecond = "Megabytes/Second"
95+
GigabytesPerSecond = "Gigabytes/Second"
96+
TerabytesPerSecond = "Terabytes/Second"
97+
BitsPerSecond = "Bits/Second"
98+
KilobitsPerSecond = "Kilobits/Second"
99+
MegabitsPerSecond = "Megabits/Second"
100+
GigabitsPerSecond = "Gigabits/Second"
101+
TerabitsPerSecond = "Terabits/Second"
102+
CountPerSecond = "Count/Second"
103103

104104

105105
def build_metric_unit_from_str(unit: Union[str, MetricUnit]) -> MetricUnit:

Diff for: python/aws_lambda_powertools/metrics/base.py

+37-8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def __init__(self, metric_set: Dict[str, str] = None, dimension_set: Dict = None
5353
self.metric_set = metric_set or {}
5454
self.dimension_set = dimension_set or {}
5555
self.namespace = os.getenv("POWERTOOLS_METRICS_NAMESPACE") or namespace
56+
self._metric_units = [unit.value for unit in MetricUnit]
57+
self._metric_unit_options = list(MetricUnit.__members__)
5658

5759
def add_namespace(self, name: str):
5860
"""Adds given metric namespace
@@ -105,14 +107,8 @@ def add_metric(self, name: str, unit: MetricUnit, value: Union[float, int]):
105107
if not isinstance(value, numbers.Number):
106108
raise MetricValueError(f"{value} is not a valid number")
107109

108-
if not isinstance(unit, MetricUnit):
109-
try:
110-
unit = MetricUnit[unit]
111-
except KeyError:
112-
unit_options = list(MetricUnit.__members__)
113-
raise MetricUnitError(f"Invalid metric unit '{unit}', expected either option: {unit_options}")
114-
115-
metric = {"Unit": unit.value, "Value": float(value)}
110+
unit = self.__extract_metric_unit_value(unit=unit)
111+
metric = {"Unit": unit, "Value": float(value)}
116112
logger.debug(f"Adding metric: {name} with {metric}")
117113
self.metric_set[name] = metric
118114

@@ -205,3 +201,36 @@ def add_dimension(self, name: str, value: str):
205201
"""
206202
logger.debug(f"Adding dimension: {name}:{value}")
207203
self.dimension_set[name] = value
204+
205+
def __extract_metric_unit_value(self, unit: Union[str, MetricUnit]) -> str:
206+
"""Return metric value from metric unit whether that's str or MetricUnit enum
207+
208+
Parameters
209+
----------
210+
unit : Union[str, MetricUnit]
211+
Metric unit
212+
213+
Returns
214+
-------
215+
str
216+
Metric unit value (e.g. "Seconds", "Count/Second")
217+
218+
Raises
219+
------
220+
MetricUnitError
221+
When metric unit is not supported by CloudWatch
222+
"""
223+
224+
if isinstance(unit, str):
225+
if unit in self._metric_unit_options:
226+
unit = MetricUnit[unit].value
227+
228+
if unit not in self._metric_units: # str correta
229+
raise MetricUnitError(
230+
f"Invalid metric unit '{unit}', expected either option: {self._metric_unit_options}"
231+
)
232+
233+
if isinstance(unit, MetricUnit):
234+
unit = unit.value
235+
236+
return unit

Diff for: python/tests/functional/test_metrics.py

+25
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,28 @@ def lambda_handler(evt, context):
303303

304304
with pytest.raises(SchemaValidationError):
305305
lambda_handler({}, {})
306+
307+
308+
def test_all_metric_units_string(metric, dimension, namespace):
309+
310+
# metric unit as MetricUnit key e.g. "Seconds", "BytesPerSecond"
311+
for unit in MetricUnit:
312+
metric["unit"] = unit.name
313+
with single_metric(**metric) as my_metric:
314+
my_metric.add_dimension(**dimension)
315+
my_metric.add_namespace(**namespace)
316+
317+
with pytest.raises(MetricUnitError):
318+
metric["unit"] = "seconds"
319+
with single_metric(**metric) as my_metric:
320+
my_metric.add_dimension(**dimension)
321+
my_metric.add_namespace(**namespace)
322+
323+
all_metric_units = [unit.value for unit in MetricUnit]
324+
325+
# metric unit as MetricUnit value e.g. "Seconds", "Bytes/Second"
326+
for unit in all_metric_units:
327+
metric["unit"] = unit
328+
with single_metric(**metric) as my_metric:
329+
my_metric.add_dimension(**dimension)
330+
my_metric.add_namespace(**namespace)

0 commit comments

Comments
 (0)