Skip to content

Commit

Permalink
Tritium registry 'getMetrics()' doesn't throw on duplicate names (#1589)
Browse files Browse the repository at this point in the history
Tritium registry 'getMetrics()' doesn't throw on duplicate names
  • Loading branch information
carterkozak authored Sep 30, 2022
1 parent de94a26 commit 38265a0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1589.v2.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public final Map<MetricName, Metric> getMetrics() {
.forEach((metricName, metric) ->
result.put(RealMetricName.create(metricName, tag.getKey(), tag.getValue()), metric)));

return result.build();
return result.buildKeepingLast();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 38265a0

Please sign in to comment.