Skip to content

Commit

Permalink
Add MetricRegistries metricsPrefixedBy and metricsMatching (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
schlosna authored Sep 29, 2017
1 parent ba44bd3 commit 7fb2132
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.codahale.metrics.Gauge;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Reservoir;
import com.google.common.annotations.VisibleForTesting;
Expand All @@ -31,6 +32,8 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.annotation.concurrent.GuardedBy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -119,6 +122,34 @@ static <T extends Metric> T getOrAdd(MetricRegistry metrics, String name, Metric
throw new IllegalArgumentException(name + " is already used for a different type of metric for " + metric);
}

/**
* Creates a {@link MetricFilter} predicate to match metrics with names starting with the specified prefix.
*
* @param prefix metric name prefix
* @return metric filter
*/
public static MetricFilter metricsPrefixedBy(final String prefix) {
checkNotNull(prefix, "prefix");
return (name, metric) -> name.startsWith(prefix);
}

/**
* Returns a sorted map of metrics from the specified registry matching the specified filter.
*
* @param metrics metric registry
* @param filter metric filter predicate
* @return sorted map of metrics
*/
public static SortedMap<String, Metric> metricsMatching(MetricRegistry metrics, MetricFilter filter) {
SortedMap<String, Metric> matchingMetrics = new TreeMap<>();
metrics.getMetrics().forEach((key, value) -> {
if (filter.matches(key, value)) {
matchingMetrics.put(key, value);
}
});
return matchingMetrics;
}

/**
* Register specified cache with the given metric registry.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.common.cache.LoadingCache;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.SortedMap;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Test;
Expand Down Expand Up @@ -90,7 +91,7 @@ public String load(Integer key) throws Exception {
});
MetricRegistries.registerCache(metrics, cache, "test");

assertThat(metrics.getGauges(metricsPrefixedBy("test")).keySet()).containsExactly(
assertThat(metrics.getGauges(MetricRegistries.metricsPrefixedBy("test")).keySet()).containsExactly(
"test.cache.estimated.size",
"test.cache.eviction.count",
"test.cache.hit.count",
Expand Down Expand Up @@ -145,7 +146,7 @@ public String load(Integer key) throws Exception {

MetricRegistries.registerCache(metrics, cache, "test");

assertThat(metrics.getGauges(metricsPrefixedBy("test")).keySet()).containsExactly(
assertThat(metrics.getGauges(MetricRegistries.metricsPrefixedBy("test")).keySet()).containsExactly(
"test.cache.estimated.size",
"test.cache.eviction.count",
"test.cache.hit.count",
Expand Down Expand Up @@ -220,13 +221,40 @@ public void testInaccessibleConstructor() {
}
}

private static MetricFilter metricsPrefixedBy(final String prefix) {
return new MetricFilter() {
@Override
public boolean matches(String name, Metric metric) {
return name.startsWith(prefix);
}
};
@Test
public void testMetricsPrefixedBy() {
MetricFilter metricFilter = MetricRegistries.metricsPrefixedBy("test");

Metric metric = mock(Metric.class);
assertThat(metricFilter.matches("test", metric)).isTrue();
assertThat(metricFilter.matches("test", null)).isTrue();
assertThat(metricFilter.matches("test.foo", metric)).isTrue();
assertThat(metricFilter.matches("testing", metric)).isTrue();
assertThat(metricFilter.matches("bar", metric)).isFalse();
assertThat(metricFilter.matches("bar", null)).isFalse();
}

@Test(expected = NullPointerException.class)
public void testNullPrefixMetricsPrefixedBy() {
assertThat(MetricRegistries.metricsPrefixedBy(null)).isNotNull();
}

@Test
public void testMetricsMatching() {
MetricFilter palantirFilter = (name, metric) -> name.startsWith("test");

metrics.counter("test.a");
metrics.timer("test.b");
metrics.counter("non.matching");

SortedMap<String, Metric> metricsMatching = MetricRegistries.metricsMatching(metrics, palantirFilter);
assertThat(metricsMatching.size()).isEqualTo(2);
assertThat(metricsMatching.keySet())
.containsAllOf("test.a", "test.b")
.inOrder();
assertThat(metricsMatching.values())
.containsExactly(metrics.counter("test.a"), metrics.timer("test.b"))
.inOrder();
}

private static void report(MetricRegistry metrics) {
Expand Down

0 comments on commit 7fb2132

Please sign in to comment.