diff --git a/java/google/registry/monitoring/metrics/MetricRegistryImpl.java b/java/google/registry/monitoring/metrics/MetricRegistryImpl.java index 0bb695f05..d490a1ecc 100644 --- a/java/google/registry/monitoring/metrics/MetricRegistryImpl.java +++ b/java/google/registry/monitoring/metrics/MetricRegistryImpl.java @@ -36,11 +36,10 @@ public final class MetricRegistryImpl implements MetricRegistry { /** The canonical registry for metrics. The map key is the metric name. */ private final ConcurrentHashMap> registeredMetrics = new ConcurrentHashMap<>(); - /** - * Production code must use {@link getDefault}, since this returns the {@link MetricRegistry} - * that {@link MetricReporter} uses. Test code that does not use {@link MetricReporter} can - * use this constructor to get an isolated instance of the registry. + * Production code must use {@link #getDefault}, since this returns the {@link MetricRegistry} + * that {@link MetricReporter} uses. Test code that does not use {@link MetricReporter} can use + * this constructor to get an isolated instance of the registry. */ @VisibleForTesting public MetricRegistryImpl() {} @@ -144,11 +143,16 @@ public final class MetricRegistryImpl implements MetricRegistry { * write intervals. */ @VisibleForTesting - void unregisterMetric(String name) { + public void unregisterMetric(String name) { registeredMetrics.remove(name); logger.info("Unregistered metric: " + name); } + @VisibleForTesting + public void unregisterAllMetrics() { + registeredMetrics.clear(); + } + /** Registers a metric. */ @VisibleForTesting void registerMetric(String name, Metric metric) { diff --git a/javatests/google/registry/monitoring/metrics/MetricRegistryImplTest.java b/javatests/google/registry/monitoring/metrics/MetricRegistryImplTest.java index f3d8542e6..a6a21deb7 100644 --- a/javatests/google/registry/monitoring/metrics/MetricRegistryImplTest.java +++ b/javatests/google/registry/monitoring/metrics/MetricRegistryImplTest.java @@ -37,19 +37,14 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) public class MetricRegistryImplTest { - @Rule - public final ExpectedException thrown = ExpectedException.none(); + @Rule public final ExpectedException thrown = ExpectedException.none(); private final LabelDescriptor label = LabelDescriptor.create("test_labelname", "test_labeldescription"); @After public void clearMetrics() { - ImmutableList> metrics = MetricRegistryImpl.getDefault().getRegisteredMetrics(); - - for (Metric metric : metrics) { - MetricRegistryImpl.getDefault().unregisterMetric(metric.getMetricSchema().name()); - } + MetricRegistryImpl.getDefault().unregisterAllMetrics(); } @Test