diff --git a/moto/cloudwatch/models.py b/moto/cloudwatch/models.py index 4546eccce..009492e19 100644 --- a/moto/cloudwatch/models.py +++ b/moto/cloudwatch/models.py @@ -168,7 +168,7 @@ def are_dimensions_same(metric_dimensions, dimensions): class MetricDatum(BaseModel): - def __init__(self, namespace, name, value, dimensions, timestamp): + def __init__(self, namespace, name, value, dimensions, timestamp, unit=None): self.namespace = namespace self.name = name self.value = value @@ -176,6 +176,7 @@ class MetricDatum(BaseModel): self.dimensions = [ Dimension(dimension["Name"], dimension["Value"]) for dimension in dimensions ] + self.unit = unit def filter(self, namespace, name, dimensions, already_present_metrics): if namespace and namespace != self.namespace: @@ -385,6 +386,7 @@ class CloudWatchBackend(BaseBackend): float(metric_member.get("Value", 0)), metric_member.get("Dimensions.member", _EMPTY_LIST), timestamp, + metric_member.get("Unit"), ) ) @@ -449,7 +451,7 @@ class CloudWatchBackend(BaseBackend): return results def get_metric_statistics( - self, namespace, metric_name, start_time, end_time, period, stats + self, namespace, metric_name, start_time, end_time, period, stats, unit=None ): period_delta = timedelta(seconds=period) filtered_data = [ @@ -460,6 +462,9 @@ class CloudWatchBackend(BaseBackend): and start_time <= md.timestamp <= end_time ] + if unit: + filtered_data = [md for md in filtered_data if md.unit == unit] + # earliest to oldest filtered_data = sorted(filtered_data, key=lambda x: x.timestamp) if not filtered_data: diff --git a/moto/cloudwatch/responses.py b/moto/cloudwatch/responses.py index 063dff8b6..35430cc9e 100644 --- a/moto/cloudwatch/responses.py +++ b/moto/cloudwatch/responses.py @@ -119,7 +119,6 @@ class CloudWatchResponse(BaseResponse): def put_metric_data(self): namespace = self._get_param("Namespace") metric_data = self._get_multi_param("MetricData.member") - self.cloudwatch_backend.put_metric_data(namespace, metric_data) template = self.response_template(PUT_METRIC_DATA_TEMPLATE) return template.render() @@ -151,7 +150,7 @@ class CloudWatchResponse(BaseResponse): unit = self._get_param("Unit") extended_statistics = self._get_param("ExtendedStatistics") dimensions = self._get_param("Dimensions") - if unit or extended_statistics or dimensions: + if extended_statistics or dimensions: raise NotImplementedError() # TODO: this should instead throw InvalidParameterCombination @@ -161,7 +160,7 @@ class CloudWatchResponse(BaseResponse): ) datapoints = self.cloudwatch_backend.get_metric_statistics( - namespace, metric_name, start_time, end_time, period, statistics + namespace, metric_name, start_time, end_time, period, statistics, unit ) template = self.response_template(GET_METRIC_STATISTICS_TEMPLATE) return template.render(label=metric_name, datapoints=datapoints) @@ -514,7 +513,9 @@ GET_METRIC_STATISTICS_TEMPLATE = """