From 3f79f7f6c7d04f0506a7dc5b67a7ffb4390f2d5a Mon Sep 17 00:00:00 2001 From: Ranjithkumar Krishnan Date: Fri, 31 Dec 2021 16:55:48 +0100 Subject: [PATCH] Fix cloudwatch list metrics localstack #5000 (#4732) --- moto/cloudwatch/models.py | 11 +++-- .../test_cloudwatch/test_cloudwatch_boto3.py | 47 +++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/moto/cloudwatch/models.py b/moto/cloudwatch/models.py index 7cc887688..2a2d4f691 100644 --- a/moto/cloudwatch/models.py +++ b/moto/cloudwatch/models.py @@ -220,9 +220,14 @@ class MetricDatum(BaseModel): return False for metric in already_present_metrics or []: - if self.dimensions and are_dimensions_same( - metric.dimensions, self.dimensions - ): + if ( + ( + self.dimensions + and are_dimensions_same(metric.dimensions, self.dimensions) + ) + and self.name == metric.name + and self.namespace == metric.namespace + ): # should be considered as already present only when name, namespace and dimensions all three are same return False if dimensions and any( diff --git a/tests/test_cloudwatch/test_cloudwatch_boto3.py b/tests/test_cloudwatch/test_cloudwatch_boto3.py index d9c4bcd1a..e27008980 100644 --- a/tests/test_cloudwatch/test_cloudwatch_boto3.py +++ b/tests/test_cloudwatch/test_cloudwatch_boto3.py @@ -400,6 +400,53 @@ def test_list_metrics_without_value(): results[0]["Dimensions"].should.equal([{"Name": "D1", "Value": "V1"}]) +@mock_cloudwatch +def test_list_metrics_with_same_dimensions_different_metric_name(): + cloudwatch = boto3.client("cloudwatch", "eu-west-1") + + # create metrics with same namespace and dimensions but different metric names + cloudwatch.put_metric_data( + Namespace="unique/", + MetricData=[ + { + "MetricName": "metric1", + "Dimensions": [{"Name": "D1", "Value": "V1"}], + "Unit": "Seconds", + } + ], + ) + + cloudwatch.put_metric_data( + Namespace="unique/", + MetricData=[ + { + "MetricName": "metric2", + "Dimensions": [{"Name": "D1", "Value": "V1"}], + "Unit": "Seconds", + } + ], + ) + + results = cloudwatch.list_metrics()["Metrics"] + results.should.have.length_of(2) + + # duplicating existing metric + cloudwatch.put_metric_data( + Namespace="unique/", + MetricData=[ + { + "MetricName": "metric1", + "Dimensions": [{"Name": "D1", "Value": "V1"}], + "Unit": "Seconds", + } + ], + ) + + # asserting only unique values are returned + results = cloudwatch.list_metrics()["Metrics"] + results.should.have.length_of(2) + + def create_metrics(cloudwatch, namespace, metrics=5, data_points=5): for i in range(0, metrics): metric_name = "metric" + str(i)