CloudWatch: Add custom label param to get_metric_data (#5728)

This commit is contained in:
Maël Mandis 2022-12-02 23:00:25 +01:00 committed by GitHub
parent 08c65a9a3c
commit e4c0d0618c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 1 deletions

View File

@ -603,7 +603,13 @@ class CloudWatchBackend(BaseBackend):
if scan_by == "TimestampDescending" and len(timestamps) > 0:
timestamps.reverse()
result_vals.reverse()
label = query["metric_stat._metric._metric_name"] + " " + stat
label = (
query["label"]
if "label" in query
else query["metric_stat._metric._metric_name"] + " " + stat
)
results.append(
{
"id": query["id"],

View File

@ -1513,3 +1513,74 @@ def test_put_metric_alarm_error_evaluate_low_sample_count_percentile():
"Option unknown is not supported. "
"Supported options for parameter EvaluateLowSampleCountPercentile are evaluate and ignore."
)
@mock_cloudwatch
def test_get_metric_data_with_custom_label():
utc_now = datetime.now(tz=pytz.utc)
cloudwatch = boto3.client("cloudwatch", "eu-west-1")
namespace = "my_namespace/"
label = "MyCustomLabel"
# put metric data
cloudwatch.put_metric_data(
Namespace=namespace,
MetricData=[
{
"MetricName": "metric1",
"Value": 50,
"Timestamp": utc_now,
},
{
"MetricName": "metric1",
"Value": -50,
"Timestamp": utc_now,
},
],
)
# get_metric_data
response = cloudwatch.get_metric_data(
MetricDataQueries=[
{
"Id": "result_without_custom_label",
"MetricStat": {
"Metric": {
"Namespace": namespace,
"MetricName": "metric1",
},
"Period": 60,
"Stat": "SampleCount",
},
},
{
"Id": "result_with_custom_label",
"Label": label,
"MetricStat": {
"Metric": {
"Namespace": namespace,
"MetricName": "metric1",
},
"Period": 60,
"Stat": "SampleCount",
},
},
],
StartTime=utc_now - timedelta(seconds=60),
EndTime=utc_now + timedelta(seconds=60),
)
expected_values = {
"result_without_custom_label": "metric1 SampleCount",
"result_with_custom_label": label,
}
for id_, expected_value in expected_values.items():
metric_result_data = list(
filter(
lambda result_data: result_data["Id"] == id_,
response["MetricDataResults"],
)
)
len(metric_result_data).should.equal(1)
metric_result_data[0]["Label"].should.equal(expected_value)