Remove visibility to codahale Metrics registry
Removed getRegistry() method from OnosMetrics class so
callers no longer have direct access to the Metrics
registry. Added APIs that were ncessary to query lists of
Metrics in the system, and an API to remove Metrics.
Change-Id: I7b9efc86dd990cee7a715ef44b228ef94e03367d
diff --git a/src/main/java/net/onrc/onos/core/metrics/MetricsResource.java b/src/main/java/net/onrc/onos/core/metrics/MetricsResource.java
index 40feaaa..f8d8a12 100644
--- a/src/main/java/net/onrc/onos/core/metrics/MetricsResource.java
+++ b/src/main/java/net/onrc/onos/core/metrics/MetricsResource.java
@@ -6,7 +6,6 @@
import com.codahale.metrics.Meter;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricFilter;
-import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
@@ -66,8 +65,7 @@
*/
@Get("json")
@SuppressWarnings("rawtypes")
- public Representation retrieve() throws Exception {
- final MetricRegistry registry = OnosMetrics.getMetricsRegistry();
+ public Representation retrieve() {
final MetricsObjectResource result = new MetricsObjectResource();
final List<MetricsObjectResource.TimerObjectResource> timers =
@@ -86,35 +84,35 @@
final MetricFilter filter = new MetricNameFilter(metricIdsString);
for (final Map.Entry<String, Timer> timer :
- registry.getTimers(filter).entrySet()) {
+ OnosMetrics.getTimers(filter).entrySet()) {
timers.add(new MetricsObjectResource.TimerObjectResource(
timer.getKey(), timer.getValue()));
}
result.setTimers(timers);
for (final Map.Entry<String, Gauge> gauge :
- registry.getGauges(filter).entrySet()) {
+ OnosMetrics.getGauges(filter).entrySet()) {
gauges.add(new MetricsObjectResource.GaugeObjectResource(
gauge.getKey(), gauge.getValue()));
}
result.setGauges(gauges);
for (final Map.Entry<String, Counter> counter :
- registry.getCounters(filter).entrySet()) {
+ OnosMetrics.getCounters(filter).entrySet()) {
counters.add(new MetricsObjectResource.CounterObjectResource(
counter.getKey(), counter.getValue()));
}
result.setCounters(counters);
for (final Map.Entry<String, Meter> meter :
- registry.getMeters(filter).entrySet()) {
+ OnosMetrics.getMeters(filter).entrySet()) {
meters.add(new MetricsObjectResource.MeterObjectResource(
meter.getKey(), meter.getValue()));
}
result.setMeters(meters);
for (final Map.Entry<String, Histogram> histogram :
- registry.getHistograms(filter).entrySet()) {
+ OnosMetrics.getHistograms(filter).entrySet()) {
histograms.add(new MetricsObjectResource.HistogramObjectResource(
histogram.getKey(), histogram.getValue()));
}
diff --git a/src/main/java/net/onrc/onos/core/metrics/OnosMetrics.java b/src/main/java/net/onrc/onos/core/metrics/OnosMetrics.java
index 3006b0c..62d9038 100644
--- a/src/main/java/net/onrc/onos/core/metrics/OnosMetrics.java
+++ b/src/main/java/net/onrc/onos/core/metrics/OnosMetrics.java
@@ -1,12 +1,16 @@
package net.onrc.onos.core.metrics;
import com.codahale.metrics.Counter;
+import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Metric;
+import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
+import java.util.Map;
+
/**
* This class acts a singleton to hold the Metrics registry for ONOS.
*/
@@ -171,22 +175,77 @@
* @param metricName local name of the metric
* @param metric Metric to register
*/
- public static void registerMetric(MetricsComponents component,
- MetricsFeatures feature,
- String metricName,
- Metric metric) {
+ public static void registerMetric(final MetricsComponents component,
+ final MetricsFeatures feature,
+ final String metricName,
+ final Metric metric) {
final String name = generateName(component, feature, metricName);
METRICS_REGISTRY.register(name, metric);
}
/**
- * Get the singleton Metrics registry. A single instance of
- * the registry is statically allocated and then used by all callers.
+ * Fetches the existing Timers.
*
- * @return Metrics registry
+ * @param filter filter to use to select Timers
+ * @return a map of the Timers that match the filter, with the key as the
+ * name String to the Timer.
*/
- public static MetricRegistry getMetricsRegistry() {
- return METRICS_REGISTRY;
+ public static Map<String, Timer> getTimers(final MetricFilter filter) {
+ return METRICS_REGISTRY.getTimers(filter);
+ }
+
+ /**
+ * Fetches the existing Gauges.
+ *
+ * @param filter filter to use to select Gauges
+ * @return a map of the Gauges that match the filter, with the key as the
+ * name String to the Gauge.
+ */
+ @SuppressWarnings("rawtypes")
+ public static Map<String, Gauge> getGauges(final MetricFilter filter) {
+ return METRICS_REGISTRY.getGauges(filter);
+ }
+
+ /**
+ * Fetches the existing Counters.
+ *
+ * @param filter filter to use to select Counters
+ * @return a map of the Counters that match the filter, with the key as the
+ * name String to the Counter.
+ */
+ public static Map<String, Counter> getCounters(final MetricFilter filter) {
+ return METRICS_REGISTRY.getCounters(filter);
+ }
+
+ /**
+ * Fetches the existing Meters.
+ *
+ * @param filter filter to use to select Meters
+ * @return a map of the Meters that match the filter, with the key as the
+ * name String to the Meter.
+ */
+ public static Map<String, Meter> getMeters(final MetricFilter filter) {
+ return METRICS_REGISTRY.getMeters(filter);
+ }
+
+ /**
+ * Fetches the existing Histograms.
+ *
+ * @param filter filter to use to select Histograms
+ * @return a map of the Histograms that match the filter, with the key as the
+ * name String to the Histogram.
+ */
+ public static Map<String, Histogram> getHistograms(final MetricFilter filter) {
+ return METRICS_REGISTRY.getHistograms(filter);
+ }
+
+ /**
+ * Removes all Metrics that match a given filter.
+ *
+ * @param filter filter to use to select the Metrics to remove.
+ */
+ public static void removeMatching(final MetricFilter filter) {
+ METRICS_REGISTRY.removeMatching(filter);
}
}