mirror of
https://github.com/google/nomulus.git
synced 2025-05-16 17:37:13 +02:00
Add a convenience method to clear all registered metrics
Also making these methods public so that other test methods can use them. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=174074038
This commit is contained in:
parent
b0e062d725
commit
d3254eaaeb
2 changed files with 11 additions and 12 deletions
|
@ -36,11 +36,10 @@ public final class MetricRegistryImpl implements MetricRegistry {
|
||||||
/** The canonical registry for metrics. The map key is the metric name. */
|
/** The canonical registry for metrics. The map key is the metric name. */
|
||||||
private final ConcurrentHashMap<String, Metric<?>> registeredMetrics = new ConcurrentHashMap<>();
|
private final ConcurrentHashMap<String, Metric<?>> registeredMetrics = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Production code must use {@link getDefault}, since this returns the {@link MetricRegistry}
|
* 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
|
* that {@link MetricReporter} uses. Test code that does not use {@link MetricReporter} can use
|
||||||
* use this constructor to get an isolated instance of the registry.
|
* this constructor to get an isolated instance of the registry.
|
||||||
*/
|
*/
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
public MetricRegistryImpl() {}
|
public MetricRegistryImpl() {}
|
||||||
|
@ -144,11 +143,16 @@ public final class MetricRegistryImpl implements MetricRegistry {
|
||||||
* write intervals.
|
* write intervals.
|
||||||
*/
|
*/
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void unregisterMetric(String name) {
|
public void unregisterMetric(String name) {
|
||||||
registeredMetrics.remove(name);
|
registeredMetrics.remove(name);
|
||||||
logger.info("Unregistered metric: " + name);
|
logger.info("Unregistered metric: " + name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
public void unregisterAllMetrics() {
|
||||||
|
registeredMetrics.clear();
|
||||||
|
}
|
||||||
|
|
||||||
/** Registers a metric. */
|
/** Registers a metric. */
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void registerMetric(String name, Metric<?> metric) {
|
void registerMetric(String name, Metric<?> metric) {
|
||||||
|
|
|
@ -37,19 +37,14 @@ import org.junit.runners.JUnit4;
|
||||||
@RunWith(JUnit4.class)
|
@RunWith(JUnit4.class)
|
||||||
public class MetricRegistryImplTest {
|
public class MetricRegistryImplTest {
|
||||||
|
|
||||||
@Rule
|
@Rule public final ExpectedException thrown = ExpectedException.none();
|
||||||
public final ExpectedException thrown = ExpectedException.none();
|
|
||||||
|
|
||||||
private final LabelDescriptor label =
|
private final LabelDescriptor label =
|
||||||
LabelDescriptor.create("test_labelname", "test_labeldescription");
|
LabelDescriptor.create("test_labelname", "test_labeldescription");
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void clearMetrics() {
|
public void clearMetrics() {
|
||||||
ImmutableList<Metric<?>> metrics = MetricRegistryImpl.getDefault().getRegisteredMetrics();
|
MetricRegistryImpl.getDefault().unregisterAllMetrics();
|
||||||
|
|
||||||
for (Metric<?> metric : metrics) {
|
|
||||||
MetricRegistryImpl.getDefault().unregisterMetric(metric.getMetricSchema().name());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue