Skip to content

Commit

Permalink
HBASE-28327 Add remove(String key, Metric metric) method to MetricReg…
Browse files Browse the repository at this point in the history
…istry interface (#5647)

Co-authored-by: Evie Boland <eboland@hubspot.com>
Signed-off-by: Bryan Beaudreault <bbeaudreault@apache.org>
  • Loading branch information
2 people authored and bbeaudreault committed Jan 25, 2024
1 parent 68bc533 commit be73106
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ public interface MetricRegistry extends MetricSet {
*/
boolean remove(String name);

/**
* Removes the metric with the given name only if it is registered to the provided metric.
* @param name the name of the metric
* @param metric the metric expected to be registered to the given name
* @return true if the metric is removed.
*/
boolean remove(String name, Metric metric);

/**
* Return the MetricRegistryInfo object for this registry.
* @return MetricRegistryInfo describing the registry.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public boolean remove(String name) {
return metrics.remove(name) != null;
}

@Override
public boolean remove(String name, Metric metric) {
return metrics.remove(name, metric);
}

@Override
public MetricRegistryInfo getMetricRegistryInfo() {
return info;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.hadoop.hbase.metrics.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -141,4 +142,22 @@ public void testGetMetrics() {
assertEquals(gauge, metrics.get("mygauge"));
assertEquals(timer, metrics.get("mytimer"));
}

@Test
public void testRemove() {
CounterImpl counter1 = new CounterImpl();
CounterImpl counter2 = new CounterImpl();
registry.register("mycounter", counter1);

boolean removed = registry.remove("mycounter", counter2);
Optional<Metric> metric = registry.get("mycounter");
assertFalse(removed);
assertTrue(metric.isPresent());
assertEquals(metric.get(), counter1);

removed = registry.remove("mycounter");
metric = registry.get("mycounter");
assertTrue(removed);
assertFalse(metric.isPresent());
}
}

0 comments on commit be73106

Please sign in to comment.