Skip to content

Commit

Permalink
Merge branch 'develop' into improv/tracer
Browse files Browse the repository at this point in the history
* develop:
  Bugfix: "per second" metric units (#27)
  • Loading branch information
heitorlessa committed May 12, 2020
2 parents 8fa07e6 + 70d9277 commit ffe7c97
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 20 deletions.
14 changes: 13 additions & 1 deletion python/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@

**0.8.1**

* **Metrics**: Fix incorrect metric units enum values for `*PerSecond` e.g. CountPerSecond
* **Metrics**: Fix metric unit casting logic if one passes plain string (value or key)
* **Metrics: **Fix `MetricUnit` enum values for
- `BytesPerSecond`
- `KilobytesPerSecond`
- `MegabytesPerSecond`
- `GigabytesPerSecond`
- `TerabytesPerSecond`
- `BitsPerSecond`
- `KilobitsPerSecond`
- `MegabitsPerSecond`
- `GigabitsPerSecond`
- `TerabitsPerSecond`
- `CountPerSecond`

## April 24th

Expand Down
22 changes: 11 additions & 11 deletions python/aws_lambda_powertools/helper/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,17 @@ class MetricUnit(Enum):
Terabits = "Terabits"
Percent = "Percent"
Count = "Count"
BytesPerSecond = "Second"
KilobytesPerSecond = "Second"
MegabytesPerSecond = "Second"
GigabytesPerSecond = "Second"
TerabytesPerSecond = "Second"
BitsPerSecond = "Second"
KilobitsPerSecond = "Second"
MegabitsPerSecond = "Second"
GigabitsPerSecond = "Second"
TerabitsPerSecond = "Second"
CountPerSecond = "Second"
BytesPerSecond = "Bytes/Second"
KilobytesPerSecond = "Kilobytes/Second"
MegabytesPerSecond = "Megabytes/Second"
GigabytesPerSecond = "Gigabytes/Second"
TerabytesPerSecond = "Terabytes/Second"
BitsPerSecond = "Bits/Second"
KilobitsPerSecond = "Kilobits/Second"
MegabitsPerSecond = "Megabits/Second"
GigabitsPerSecond = "Gigabits/Second"
TerabitsPerSecond = "Terabits/Second"
CountPerSecond = "Count/Second"


def build_metric_unit_from_str(unit: Union[str, MetricUnit]) -> MetricUnit:
Expand Down
45 changes: 37 additions & 8 deletions python/aws_lambda_powertools/metrics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def __init__(self, metric_set: Dict[str, str] = None, dimension_set: Dict = None
self.metric_set = metric_set or {}
self.dimension_set = dimension_set or {}
self.namespace = os.getenv("POWERTOOLS_METRICS_NAMESPACE") or namespace
self._metric_units = [unit.value for unit in MetricUnit]
self._metric_unit_options = list(MetricUnit.__members__)

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

if not isinstance(unit, MetricUnit):
try:
unit = MetricUnit[unit]
except KeyError:
unit_options = list(MetricUnit.__members__)
raise MetricUnitError(f"Invalid metric unit '{unit}', expected either option: {unit_options}")

metric = {"Unit": unit.value, "Value": float(value)}
unit = self.__extract_metric_unit_value(unit=unit)
metric = {"Unit": unit, "Value": float(value)}
logger.debug(f"Adding metric: {name} with {metric}")
self.metric_set[name] = metric

Expand Down Expand Up @@ -205,3 +201,36 @@ def add_dimension(self, name: str, value: str):
"""
logger.debug(f"Adding dimension: {name}:{value}")
self.dimension_set[name] = value

def __extract_metric_unit_value(self, unit: Union[str, MetricUnit]) -> str:
"""Return metric value from metric unit whether that's str or MetricUnit enum
Parameters
----------
unit : Union[str, MetricUnit]
Metric unit
Returns
-------
str
Metric unit value (e.g. "Seconds", "Count/Second")
Raises
------
MetricUnitError
When metric unit is not supported by CloudWatch
"""

if isinstance(unit, str):
if unit in self._metric_unit_options:
unit = MetricUnit[unit].value

if unit not in self._metric_units: # str correta
raise MetricUnitError(
f"Invalid metric unit '{unit}', expected either option: {self._metric_unit_options}"
)

if isinstance(unit, MetricUnit):
unit = unit.value

return unit
25 changes: 25 additions & 0 deletions python/tests/functional/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,28 @@ def lambda_handler(evt, context):

with pytest.raises(SchemaValidationError):
lambda_handler({}, {})


def test_all_metric_units_string(metric, dimension, namespace):

# metric unit as MetricUnit key e.g. "Seconds", "BytesPerSecond"
for unit in MetricUnit:
metric["unit"] = unit.name
with single_metric(**metric) as my_metric:
my_metric.add_dimension(**dimension)
my_metric.add_namespace(**namespace)

with pytest.raises(MetricUnitError):
metric["unit"] = "seconds"
with single_metric(**metric) as my_metric:
my_metric.add_dimension(**dimension)
my_metric.add_namespace(**namespace)

all_metric_units = [unit.value for unit in MetricUnit]

# metric unit as MetricUnit value e.g. "Seconds", "Bytes/Second"
for unit in all_metric_units:
metric["unit"] = unit
with single_metric(**metric) as my_metric:
my_metric.add_dimension(**dimension)
my_metric.add_namespace(**namespace)

0 comments on commit ffe7c97

Please sign in to comment.