From 3cb455edb7046e0e237dd432a1edd59c88bcbaed Mon Sep 17 00:00:00 2001 From: tzven0 <36624215+tzven0@users.noreply.github.com> Date: Wed, 27 Apr 2022 22:43:26 +0200 Subject: [PATCH] [CloudWatch] Take into account _metric_name if dimensions are set (#5068) --- moto/cloudwatch/models.py | 1 + .../test_cloudwatch/test_cloudwatch_boto3.py | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/moto/cloudwatch/models.py b/moto/cloudwatch/models.py index 6a247c754..2e07c9c6b 100644 --- a/moto/cloudwatch/models.py +++ b/moto/cloudwatch/models.py @@ -503,6 +503,7 @@ class CloudWatchBackend(BaseBackend): md for md in period_md if sorted(md.dimensions) == sorted(dimensions) + and md.name == query_name ] metric_values = [m.value for m in query_period_data] diff --git a/tests/test_cloudwatch/test_cloudwatch_boto3.py b/tests/test_cloudwatch/test_cloudwatch_boto3.py index d8ee2aad7..a9c725de3 100644 --- a/tests/test_cloudwatch/test_cloudwatch_boto3.py +++ b/tests/test_cloudwatch/test_cloudwatch_boto3.py @@ -498,6 +498,78 @@ def create_metrics_with_dimensions(cloudwatch, namespace, data_points=5): ) +@mock_cloudwatch +def test_get_metric_data_for_multiple_metrics_w_same_dimensions(): + utc_now = datetime.now(tz=pytz.utc) + cloudwatch = boto3.client("cloudwatch", "eu-west-1") + namespace = "my_namespace/" + cloudwatch.put_metric_data( + Namespace=namespace, + MetricData=[ + { + "MetricName": "metric1", + "Dimensions": [{"Name": "Name", "Value": "B"}], + "Value": 50, + }, + { + "MetricName": "metric2", + "Dimensions": [{"Name": "Name", "Value": "B"}], + "Value": 25, + "Unit": "Microseconds", + }, + ], + ) + # get_metric_data 1 + response1 = cloudwatch.get_metric_data( + MetricDataQueries=[ + { + "Id": "result1", + "MetricStat": { + "Metric": { + "Namespace": namespace, + "MetricName": "metric1", + "Dimensions": [{"Name": "Name", "Value": "B"}], + }, + "Period": 60, + "Stat": "Sum", + }, + }, + ], + StartTime=utc_now - timedelta(seconds=60), + EndTime=utc_now + timedelta(seconds=60), + ) + # + len(response1["MetricDataResults"]).should.equal(1) + + res1 = response1["MetricDataResults"][0] + res1["Values"].should.equal([50.0]) + + # get_metric_data 2 + response2 = cloudwatch.get_metric_data( + MetricDataQueries=[ + { + "Id": "result2", + "MetricStat": { + "Metric": { + "Namespace": namespace, + "MetricName": "metric2", + "Dimensions": [{"Name": "Name", "Value": "B"}], + }, + "Period": 60, + "Stat": "Sum", + }, + }, + ], + StartTime=utc_now - timedelta(seconds=60), + EndTime=utc_now + timedelta(seconds=60), + ) + # + len(response2["MetricDataResults"]).should.equal(1) + + res2 = response2["MetricDataResults"][0] + res2["Values"].should.equal([25.0]) + + @mock_cloudwatch def test_get_metric_data_within_timeframe(): utc_now = datetime.now(tz=pytz.utc)