diff --git a/src/main/java/software/amazon/cloudwatchlogs/emf/model/MetricDirective.java b/src/main/java/software/amazon/cloudwatchlogs/emf/model/MetricDirective.java index 8d52094a..e3e056e9 100644 --- a/src/main/java/software/amazon/cloudwatchlogs/emf/model/MetricDirective.java +++ b/src/main/java/software/amazon/cloudwatchlogs/emf/model/MetricDirective.java @@ -51,7 +51,16 @@ class MetricDirective { shouldUseDefaultDimension = true; } + /** + * Adds a dimension set to the end of the collection. + * + * @param dimensionSet + */ void putDimensionSet(DimensionSet dimensionSet) { + // Duplicate dimensions sets are removed before being added to the end of the collection. + // This ensures only latest dimension value is used as a target member on the root EMF node. + // This operation is O(n^2), but acceptable given sets are capped at 10 dimensions + dimensions.removeIf(dim -> dim.getDimensionKeys().equals(dimensionSet.getDimensionKeys())); dimensions.add(dimensionSet); } diff --git a/src/test/java/software/amazon/cloudwatchlogs/emf/model/MetricDirectiveTest.java b/src/test/java/software/amazon/cloudwatchlogs/emf/model/MetricDirectiveTest.java index 50d1bf3b..31ab8d94 100644 --- a/src/test/java/software/amazon/cloudwatchlogs/emf/model/MetricDirectiveTest.java +++ b/src/test/java/software/amazon/cloudwatchlogs/emf/model/MetricDirectiveTest.java @@ -102,7 +102,7 @@ public void testPutDimensions() throws JsonProcessingException { } @Test - public void testPutMultipleDimensionSets() throws JsonProcessingException { + public void testPutDimensionSetWhenMultipleDimensionSets() throws JsonProcessingException { MetricDirective metricDirective = new MetricDirective(); metricDirective.putDimensionSet(DimensionSet.of("Region", "us-east-1")); metricDirective.putDimensionSet(DimensionSet.of("Instance", "inst-1")); @@ -114,6 +114,57 @@ public void testPutMultipleDimensionSets() throws JsonProcessingException { "{\"Dimensions\":[[\"Region\"],[\"Instance\"]],\"Metrics\":[],\"Namespace\":\"aws-embedded-metrics\"}"); } + @Test + public void testPutDimensionSetWhenDuplicateDimensionSets() throws JsonProcessingException { + MetricDirective metricDirective = new MetricDirective(); + metricDirective.putDimensionSet(new DimensionSet()); + metricDirective.putDimensionSet(DimensionSet.of("Region", "us-east-1")); + metricDirective.putDimensionSet( + DimensionSet.of("Region", "us-east-1", "Instance", "inst-1")); + metricDirective.putDimensionSet( + DimensionSet.of("Instance", "inst-1", "Region", "us-east-1")); + metricDirective.putDimensionSet(DimensionSet.of("Instance", "inst-1")); + metricDirective.putDimensionSet(new DimensionSet()); + metricDirective.putDimensionSet(DimensionSet.of("Region", "us-east-1")); + metricDirective.putDimensionSet( + DimensionSet.of("Region", "us-east-1", "Instance", "inst-1")); + metricDirective.putDimensionSet( + DimensionSet.of("Instance", "inst-1", "Region", "us-east-1")); + metricDirective.putDimensionSet(DimensionSet.of("Instance", "inst-1")); + + String serializedMetricDirective = objectMapper.writeValueAsString(metricDirective); + + assertEquals( + "{\"Dimensions\":[[],[\"Region\"],[\"Instance\",\"Region\"],[\"Instance\"]],\"Metrics\":[],\"Namespace\":\"aws-embedded-metrics\"}", + serializedMetricDirective); + } + + @Test + public void testPutDimensionSetWhenDuplicateDimensionSetsWillSortCorrectly() + throws JsonProcessingException { + MetricDirective metricDirective = new MetricDirective(); + metricDirective.putDimensionSet(new DimensionSet()); + metricDirective.putDimensionSet(DimensionSet.of("Region", "us-east-1")); + metricDirective.putDimensionSet( + DimensionSet.of("Region", "us-east-1", "Instance", "inst-1")); + metricDirective.putDimensionSet( + DimensionSet.of("Instance", "inst-1", "Region", "us-east-1")); + metricDirective.putDimensionSet(DimensionSet.of("Instance", "inst-1")); + metricDirective.putDimensionSet( + DimensionSet.of("Region", "us-east-1", "Instance", "inst-1")); + metricDirective.putDimensionSet( + DimensionSet.of("Instance", "inst-1", "Region", "us-east-1")); + metricDirective.putDimensionSet(DimensionSet.of("Instance", "inst-1")); + metricDirective.putDimensionSet(DimensionSet.of("Region", "us-east-1")); + metricDirective.putDimensionSet(new DimensionSet()); + + String serializedMetricDirective = objectMapper.writeValueAsString(metricDirective); + + assertEquals( + "{\"Dimensions\":[[\"Instance\",\"Region\"],[\"Instance\"],[\"Region\"],[]],\"Metrics\":[],\"Namespace\":\"aws-embedded-metrics\"}", + serializedMetricDirective); + } + @Test public void testGetDimensionAfterSetDimensions() { MetricDirective metricDirective = new MetricDirective();