Add unit support to cloudwatch get_metric_statistics (#3932)

* Add unit support to cloudwatch get_metric_statistics

* fix linting
This commit is contained in:
usmangani1 2021-05-15 12:35:41 +05:30 committed by GitHub
parent 58fd4aeaae
commit 03b2009a1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 5 deletions

View File

@ -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:

View File

@ -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 = """<GetMetricStatisticsResponse xmlns="http://m
{% endif %}
<Timestamp>{{ datapoint.timestamp }}</Timestamp>
{% if datapoint.unit is not none %}
<Unit>{{ datapoint.unit }}</Unit>
{% endif %}
</member>
{% endfor %}
</Datapoints>

View File

@ -158,6 +158,7 @@ def test_get_metric_statistics():
value=1.5,
dimensions={"InstanceId": ["i-0123456,i-0123457"]},
timestamp=metric_timestamp,
unit="Count",
)
metric_kwargs = dict(
@ -167,6 +168,7 @@ def test_get_metric_statistics():
end_time=datetime.now(),
period=3600,
statistics=["Minimum"],
unit="Count",
)
datapoints = conn.get_metric_statistics(**metric_kwargs)
@ -175,6 +177,19 @@ def test_get_metric_statistics():
datapoint.should.have.key("Minimum").which.should.equal(1.5)
datapoint.should.have.key("Timestamp").which.should.equal(metric_timestamp)
metric_kwargs = dict(
namespace="tester",
metric_name="metric",
start_time=metric_timestamp,
end_time=datetime.now(),
period=3600,
statistics=["Minimum"],
unit="Percent",
)
datapoints = conn.get_metric_statistics(**metric_kwargs)
datapoints.should.have.length_of(0)
# TODO: THIS IS CURRENTLY BROKEN!
# @mock_s3_deprecated