diff --git a/changelog/@unreleased/pr-1589.v2.yml b/changelog/@unreleased/pr-1589.v2.yml new file mode 100644 index 000000000..0fccd124d --- /dev/null +++ b/changelog/@unreleased/pr-1589.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Tritium registry 'getMetrics()' doesn't throw on duplicate names + links: + - https://github.com/palantir/tritium/pull/1589 diff --git a/tritium-registry/src/main/java/com/palantir/tritium/metrics/registry/AbstractTaggedMetricRegistry.java b/tritium-registry/src/main/java/com/palantir/tritium/metrics/registry/AbstractTaggedMetricRegistry.java index 5929a4a0b..3f3535514 100644 --- a/tritium-registry/src/main/java/com/palantir/tritium/metrics/registry/AbstractTaggedMetricRegistry.java +++ b/tritium-registry/src/main/java/com/palantir/tritium/metrics/registry/AbstractTaggedMetricRegistry.java @@ -182,7 +182,7 @@ public final Map getMetrics() { .forEach((metricName, metric) -> result.put(RealMetricName.create(metricName, tag.getKey(), tag.getValue()), metric))); - return result.build(); + return result.buildKeepingLast(); } @Override diff --git a/tritium-registry/src/test/java/com/palantir/tritium/metrics/registry/TaggedMetricRegistryTest.java b/tritium-registry/src/test/java/com/palantir/tritium/metrics/registry/TaggedMetricRegistryTest.java index b23a54735..81fa48ac7 100644 --- a/tritium-registry/src/test/java/com/palantir/tritium/metrics/registry/TaggedMetricRegistryTest.java +++ b/tritium-registry/src/test/java/com/palantir/tritium/metrics/registry/TaggedMetricRegistryTest.java @@ -17,6 +17,7 @@ package com.palantir.tritium.metrics.registry; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -30,7 +31,9 @@ import com.codahale.metrics.Metric; import com.codahale.metrics.Timer; import com.palantir.tritium.registry.test.TestTaggedMetricRegistries; +import java.util.Collections; import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Supplier; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -301,6 +304,34 @@ void testGetMetrics(TaggedMetricRegistry registry) { assertThat(counter.getCount()).isOne(); } + @ParameterizedTest + @MethodSource(TestTaggedMetricRegistries.REGISTRIES) + void testDuplicateMetricNames(TaggedMetricRegistry registry) { + registry.addMetrics( + "tag1", + "value", + () -> Collections.singletonMap( + MetricName.builder() + .safeName("name") + .putSafeTags("tag2", "value") + .build(), + new Meter())); + registry.addMetrics( + "tag2", + "value", + () -> Collections.singletonMap( + MetricName.builder() + .safeName("name") + .putSafeTags("tag1", "value") + .build(), + new Meter())); + assertThatCode(registry::getMetrics).doesNotThrowAnyException(); + assertThat(registry.getMetrics()).hasSize(1); + AtomicInteger elements = new AtomicInteger(); + registry.forEachMetric((_name, _metric) -> elements.incrementAndGet()); + assertThat(elements).hasValue(2); + } + private static void assertMetric( TaggedMetricRegistry registry, String name, String tagKey, String tagValue, Meter meter) { assertThat(registry.getMetrics())