Enforce a naming scheme on Metrics
Implemented a naming scheme and a set of methods to
enforce a naming convention for Metrics. Metrics names
will be of the form:
component.feature.local-name
An example might be
Topology.Intents.RequestQueueDepth
Change-Id: I4f2379474e194f4119bb8d9bebb8d3bb275440d3
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 bfd603f..3006b0c 100644
--- a/src/main/java/net/onrc/onos/core/metrics/OnosMetrics.java
+++ b/src/main/java/net/onrc/onos/core/metrics/OnosMetrics.java
@@ -1,6 +1,11 @@
package net.onrc.onos.core.metrics;
+import com.codahale.metrics.Counter;
+import com.codahale.metrics.Histogram;
+import com.codahale.metrics.Meter;
+import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricRegistry;
+import com.codahale.metrics.Timer;
/**
* This class acts a singleton to hold the Metrics registry for ONOS.
@@ -13,9 +18,168 @@
*/
private OnosMetrics() {}
+ /**
+ * Components that can hold Metrics. This is used as the first part of
+ * a Metric's name.
+ */
+ public enum MetricsComponents {
+ /**
+ * Global scope, not associated with a particular component.
+ */
+ GLOBAL("Global"),
+
+ /**
+ * Topology component.
+ */
+ TOPOLOGY("Topology");
+
+ private final String name;
+
+ /**
+ * Constructor allows specifying an alternate string name.
+ *
+ * @param name string for the name of the component
+ */
+ private MetricsComponents(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public String toString() {
+ return name;
+ }
+ }
+
+ /**
+ * Features that can hold Metrics. This is used as the second part of
+ * a Metric's name.
+ */
+ public enum MetricsFeatures {
+ /**
+ * Global scope, not associated with a particular feature.
+ */
+ GLOBAL("Global"),
+
+ /**
+ * Topology Intents Framework feature. (example)
+ */
+ TOPOLOGY_INTENTS("IntentsFramework");
+
+ private final String name;
+
+ /**
+ * Constructor allows specifying an alternate string name.
+ *
+ * @param name string for the name of the component
+ */
+ private MetricsFeatures(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public String toString() {
+ return name;
+ }
+ }
+
private static final MetricRegistry METRICS_REGISTRY = new MetricRegistry();
/**
+ * Generates a name for a Metric from its component and feature.
+ *
+ * @param component component the metric is defined in
+ * @param feature feature the metric is defined in
+ * @param metricName local name of the metric
+ *
+ * @return full name of the metric
+ */
+ public static String generateName(final MetricsComponents component,
+ final MetricsFeatures feature,
+ final String metricName) {
+ return MetricRegistry.name(component.toString(),
+ feature.toString(),
+ metricName);
+ }
+
+ /**
+ * Creates a Counter metric.
+ *
+ * @param component component the Counter is defined in
+ * @param feature feature the Counter is defined in
+ * @param metricName local name of the metric
+ * @return Counter Meteric
+ */
+ public static Counter createCounter(final MetricsComponents component,
+ final MetricsFeatures feature,
+ final String metricName) {
+ final String name = generateName(component, feature, metricName);
+ return METRICS_REGISTRY.counter(name);
+ }
+
+ /**
+ * Creates a Histogram metric.
+ *
+ * @param component component the Histogram is defined in
+ * @param feature feature the Histogram is defined in
+ * @param metricName local name of the metric
+ * @return Histogram Metric
+ */
+ public static Histogram createHistogram(final MetricsComponents component,
+ final MetricsFeatures feature,
+ final String metricName) {
+ final String name = generateName(component, feature, metricName);
+ return METRICS_REGISTRY.histogram(name);
+ }
+
+ /**
+ * Creates a Timer metric.
+ *
+ * @param component component the Timer is defined in
+ * @param feature feature the Timeer is defined in
+ * @param metricName local name of the metric
+ * @return Timer Metric
+ */
+ public static Timer createTimer(final MetricsComponents component,
+ final MetricsFeatures feature,
+ final String metricName) {
+ final String name = generateName(component, feature, metricName);
+ return METRICS_REGISTRY.timer(name);
+ }
+
+ /**
+ * Creates a Meter metric.
+ *
+ * @param component component the Meter is defined in
+ * @param feature feature the Meter is defined in
+ * @param metricName local name of the metric
+ * @return Meter Metric
+ */
+ public static Meter createMeter(final MetricsComponents component,
+ final MetricsFeatures feature,
+ final String metricName) {
+ final String name = generateName(component, feature, metricName);
+ return METRICS_REGISTRY.meter(name);
+ }
+
+ /**
+ * Registers an already created Metric. This is used for situation where a
+ * caller needs to allocate its own Metric, but still register it with the
+ * system.
+ *
+ * @param component component the Metric is defined in
+ * @param feature feature the metric is defined in
+ * @param metricName local name of the metric
+ * @param metric Metric to register
+ */
+ public static void registerMetric(MetricsComponents component,
+ MetricsFeatures feature,
+ String metricName,
+ 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.
*
diff --git a/src/test/java/net/onrc/onos/api/rest/TestOnosMetrics.java b/src/test/java/net/onrc/onos/api/rest/TestOnosMetrics.java
new file mode 100644
index 0000000..1169c9f
--- /dev/null
+++ b/src/test/java/net/onrc/onos/api/rest/TestOnosMetrics.java
@@ -0,0 +1,23 @@
+package net.onrc.onos.api.rest;
+
+import net.onrc.onos.core.metrics.OnosMetrics;
+import org.junit.Test;
+
+import static net.onrc.onos.core.util.UtilityClassChecker.assertThatClassIsUtility;
+
+
+// TODO - move this class to someplace more appropriate
+
+/**
+ * Unit tests for the OnosMetrics class.
+ */
+public class TestOnosMetrics {
+
+ /**
+ * Make sure that the Onos Metrics class is a utility class.
+ */
+ @Test
+ public void assureThatOnosMetricsIsUtility() {
+ assertThatClassIsUtility(OnosMetrics.class);
+ }
+}
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsCounters.java b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsCounters.java
index 84fb4db..86523c0 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsCounters.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsCounters.java
@@ -49,20 +49,35 @@
// Test Counter data objects
private static final String COUNTER1_NAME = "COUNTER1";
+ private static final String COUNTER1_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ COUNTER1_NAME);
private static final int COUNTER1_COUNT = 0;
private static final String COUNTER2_NAME = "COUNTER2";
+ private static final String COUNTER2_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ COUNTER2_NAME);
private static final int COUNTER2_COUNT = -1;
private static final String COUNTER3_NAME = "COUNTER3";
+ private static final String COUNTER3_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ COUNTER3_NAME);
private static final int COUNTER3_COUNT = 5;
private final Counter counter1 =
- OnosMetrics.getMetricsRegistry().counter(COUNTER1_NAME);
+ OnosMetrics.createCounter(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ COUNTER1_NAME);
private final Counter counter2 =
- OnosMetrics.getMetricsRegistry().counter(COUNTER2_NAME);
+ OnosMetrics.createCounter(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ COUNTER2_NAME);
private final Counter counter3 =
- OnosMetrics.getMetricsRegistry().counter(COUNTER3_NAME);
+ OnosMetrics.createCounter(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ COUNTER3_NAME);
/**
* Create some test data for the tests.
@@ -121,15 +136,15 @@
// Check the values for counter 1
final JSONObject counter1Container = counters.getJSONObject(0);
- checkCounter(counter1Container, COUNTER1_NAME, COUNTER1_COUNT);
+ checkCounter(counter1Container, COUNTER1_FULL_NAME, COUNTER1_COUNT);
// Check the values for counter 1
final JSONObject counter2Container = counters.getJSONObject(1);
- checkCounter(counter2Container, COUNTER2_NAME, COUNTER2_COUNT);
+ checkCounter(counter2Container, COUNTER2_FULL_NAME, COUNTER2_COUNT);
// Check the values for counter 1
final JSONObject counter3Container = counters.getJSONObject(2);
- checkCounter(counter3Container, COUNTER3_NAME, COUNTER3_COUNT);
+ checkCounter(counter3Container, COUNTER3_FULL_NAME, COUNTER3_COUNT);
}
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsFilters.java b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsFilters.java
index e35eb1f..3dae1dd 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsFilters.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsFilters.java
@@ -15,7 +15,6 @@
import org.powermock.modules.junit4.PowerMockRunner;
import org.restlet.resource.ClientResource;
-import static com.codahale.metrics.MetricRegistry.name;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
@@ -54,25 +53,70 @@
}
// Test data objects
- private static final String TIMER1_NAME = name("timer1");
- private static final String TIMER2_NAME = name("timer2");
- private static final String TIMER3_NAME = name("timer3");
+ private static final String TIMER1_NAME = "timer1";
+ private static final String TIMER1_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ "timer1");
+ private static final String TIMER2_NAME = "timer2";
+ private static final String TIMER2_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ "timer2");
+ private static final String TIMER3_NAME = "timer3";
+ private static final String TIMER3_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ "timer3");
- private static final String GAUGE1_NAME = name("gauge1");
- private static final String GAUGE2_NAME = name("gauge2");
- private static final String GAUGE3_NAME = name("gauge3");
+ private static final String GAUGE1_NAME = "gauge1";
+ private static final String GAUGE1_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ "gauge1");
+ private static final String GAUGE2_NAME = "gauge2";
+ private static final String GAUGE2_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ "gauge2");
+ private static final String GAUGE3_NAME = "gauge3";
+ private static final String GAUGE3_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ "gauge3");
- private static final String COUNTER1_NAME = name("counter1");
- private static final String COUNTER2_NAME = name("counter2");
- private static final String COUNTER3_NAME = name("counter3");
+ private static final String COUNTER1_NAME = "counter1";
+ private static final String COUNTER1_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ "counter1");
+ private static final String COUNTER2_NAME = "counter2";
+ private static final String COUNTER2_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ "counter2");
+ private static final String COUNTER3_NAME = "counter3";
+ private static final String COUNTER3_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ "counter3");
- private static final String METER1_NAME = name("meter1");
- private static final String METER2_NAME = name("meter2");
- private static final String METER3_NAME = name("meter3");
+ private static final String METER1_NAME = "meter1";
+ private static final String METER1_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ "meter1");
+ private static final String METER2_NAME = "meter2";
+ private static final String METER2_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ "meter2");
+ private static final String METER3_NAME = "meter3";
+ private static final String METER3_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ "meter3");
- private static final String HISTOGRAM1_NAME = name("histogram1");
- private static final String HISTOGRAM2_NAME = name("histogram2");
- private static final String HISTOGRAM3_NAME = name("histogram3");
+ private static final String HISTOGRAM1_NAME = "histogram1";
+ private static final String HISTOGRAM1_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ "histogram1");
+ private static final String HISTOGRAM2_NAME = "histogram2";
+ private static final String HISTOGRAM2_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ "histogram2");
+ private static final String HISTOGRAM3_NAME = "histogram3";
+ private static final String HISTOGRAM3_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ "histogram3");
final Gauge<Integer> testGauge = new Gauge<Integer>() {
@Override
@@ -85,25 +129,58 @@
* Creates Metrics objects for test.
*/
private void createMetrics() {
- OnosMetrics.getMetricsRegistry().timer(TIMER1_NAME);
- OnosMetrics.getMetricsRegistry().timer(TIMER2_NAME);
- OnosMetrics.getMetricsRegistry().timer(TIMER3_NAME);
+ OnosMetrics.createTimer(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ TIMER1_NAME);
+ OnosMetrics.createTimer(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ TIMER2_NAME);
+ OnosMetrics.createTimer(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ TIMER3_NAME);
- OnosMetrics.getMetricsRegistry().counter(COUNTER1_NAME);
- OnosMetrics.getMetricsRegistry().counter(COUNTER2_NAME);
- OnosMetrics.getMetricsRegistry().counter(COUNTER3_NAME);
+ OnosMetrics.createCounter(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ COUNTER1_NAME);
+ OnosMetrics.createCounter(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ COUNTER2_NAME);
+ OnosMetrics.createCounter(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ COUNTER3_NAME);
- OnosMetrics.getMetricsRegistry().meter(METER1_NAME);
- OnosMetrics.getMetricsRegistry().meter(METER2_NAME);
- OnosMetrics.getMetricsRegistry().meter(METER3_NAME);
+ OnosMetrics.createMeter(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ METER1_NAME);
+ OnosMetrics.createMeter(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ METER2_NAME);
+ OnosMetrics.createMeter(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ METER3_NAME);
- OnosMetrics.getMetricsRegistry().histogram(HISTOGRAM1_NAME);
- OnosMetrics.getMetricsRegistry().histogram(HISTOGRAM2_NAME);
- OnosMetrics.getMetricsRegistry().histogram(HISTOGRAM3_NAME);
+ OnosMetrics.createHistogram(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ HISTOGRAM1_NAME);
+ OnosMetrics.createHistogram(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ HISTOGRAM2_NAME);
+ OnosMetrics.createHistogram(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ HISTOGRAM3_NAME);
- OnosMetrics.getMetricsRegistry().register(GAUGE1_NAME, testGauge);
- OnosMetrics.getMetricsRegistry().register(GAUGE2_NAME, testGauge);
- OnosMetrics.getMetricsRegistry().register(GAUGE3_NAME, testGauge);
+ OnosMetrics.registerMetric(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ GAUGE1_NAME,
+ testGauge);
+ OnosMetrics.registerMetric(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ GAUGE2_NAME,
+ testGauge);
+ OnosMetrics.registerMetric(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ GAUGE3_NAME,
+ testGauge);
}
/**
@@ -144,7 +221,7 @@
// Read the metrics from the REST API for the test data
final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
- client.addQueryParameter("ids", "timer1,timer2");
+ client.addQueryParameter("ids", TIMER1_FULL_NAME + "," + TIMER2_FULL_NAME);
final JSONObject metrics = getJSONObject(client);
assertThat(metrics.length(), is(equalTo(5)));
@@ -155,10 +232,10 @@
assertThat(timers.length(), is(2));
final JSONObject jsonTimer1 = timers.getJSONObject(0);
- assertThat(jsonTimer1.getString("name"), is(equalTo("timer1")));
+ assertThat(jsonTimer1.getString("name"), is(equalTo(TIMER1_FULL_NAME)));
final JSONObject jsonTimer2 = timers.getJSONObject(1);
- assertThat(jsonTimer2.getString("name"), is(equalTo("timer2")));
+ assertThat(jsonTimer2.getString("name"), is(equalTo(TIMER2_FULL_NAME)));
// There should be no histograms, gauges, meters or counters
checkEmptyLists(metrics, "histograms", "gauges", "meters", "counters");
@@ -174,7 +251,7 @@
// Read the metrics from the REST API for the test data
final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
- client.addQueryParameter("ids", "timer1");
+ client.addQueryParameter("ids", TIMER1_FULL_NAME);
final JSONObject metrics = getJSONObject(client);
assertThat(metrics.length(), is(equalTo(5)));
@@ -185,7 +262,7 @@
assertThat(timers.length(), is(1));
final JSONObject jsonTimer1 = timers.getJSONObject(0);
- assertThat(jsonTimer1.getString("name"), is(equalTo("timer1")));
+ assertThat(jsonTimer1.getString("name"), is(equalTo(TIMER1_FULL_NAME)));
// There should be no histograms, gauges, meters or counters
@@ -203,7 +280,10 @@
// Read the metrics from the REST API for the test data
final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
- client.addQueryParameter("ids", "timer1,gauge2,histogram3");
+ client.addQueryParameter("ids",
+ TIMER1_FULL_NAME + "," +
+ GAUGE2_FULL_NAME + "," +
+ HISTOGRAM3_FULL_NAME);
final JSONObject metrics = getJSONObject(client);
assertThat(metrics.length(), is(equalTo(5)));
@@ -214,7 +294,7 @@
assertThat(timers.length(), is(1));
final JSONObject jsonTimer1 = timers.getJSONObject(0);
- assertThat(jsonTimer1.getString("name"), is(equalTo("timer1")));
+ assertThat(jsonTimer1.getString("name"), is(equalTo(TIMER1_FULL_NAME)));
// There should be 1 gauge that matches the filter
final JSONArray gauges = metrics.getJSONArray("gauges");
@@ -222,7 +302,7 @@
assertThat(gauges.length(), is(1));
final JSONObject jsonGauge1 = gauges.getJSONObject(0);
- assertThat(jsonGauge1.getString("name"), is(equalTo("gauge2")));
+ assertThat(jsonGauge1.getString("name"), is(equalTo(GAUGE2_FULL_NAME)));
// There should be 1 histogram that matches the filter
final JSONArray histograms = metrics.getJSONArray("histograms");
@@ -230,7 +310,7 @@
assertThat(histograms.length(), is(1));
final JSONObject jsonHistogram1 = histograms.getJSONObject(0);
- assertThat(jsonHistogram1.getString("name"), is(equalTo("histogram3")));
+ assertThat(jsonHistogram1.getString("name"), is(equalTo(HISTOGRAM3_FULL_NAME)));
// There should be no meters or counters
checkEmptyLists(metrics, "meters", "counters");
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsGauges.java b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsGauges.java
index 91cc86f..c7b0e96 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsGauges.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsGauges.java
@@ -1,7 +1,6 @@
package net.onrc.onos.api.rest;
import com.codahale.metrics.Gauge;
-import com.codahale.metrics.MetricRegistry;
import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule;
import net.onrc.onos.core.metrics.OnosMetrics;
import org.json.JSONArray;
@@ -50,40 +49,63 @@
// Test data for Gauges
private static final String GAUGE1_NAME = "gauge1";
+ private static final String GAUGE1_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ GAUGE1_NAME);
private static final int GAUGE1_VALUE = 0;
private static final String GAUGE2_NAME = "gauge2";
+ private static final String GAUGE2_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ GAUGE2_NAME);
private static final int GAUGE2_VALUE = -1;
private static final String GAUGE3_NAME = "gauge3";
+ private static final String GAUGE3_FULL_NAME = OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ GAUGE3_NAME);
private static final int GAUGE3_VALUE = 123456789;
- private final Gauge<Integer> gauge1 = OnosMetrics.getMetricsRegistry().
- register(MetricRegistry.name(GAUGE1_NAME),
+ private final Gauge<Integer> gauge1 =
new Gauge<Integer>() {
@Override
public Integer getValue() {
return GAUGE1_VALUE;
}
- });
+ };
- private final Gauge<Integer> gauge2 = OnosMetrics.getMetricsRegistry().
- register(MetricRegistry.name(GAUGE2_NAME),
+ private final Gauge<Integer> gauge2 =
new Gauge<Integer>() {
@Override
public Integer getValue() {
return GAUGE2_VALUE;
}
- });
+ };
- private final Gauge<Integer> gauge3 = OnosMetrics.getMetricsRegistry().
- register(MetricRegistry.name(GAUGE3_NAME),
+ private final Gauge<Integer> gauge3 =
new Gauge<Integer>() {
@Override
public Integer getValue() {
return GAUGE3_VALUE;
}
- });
+ };
+
+ private void registerGauges() {
+ OnosMetrics.registerMetric(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ GAUGE1_NAME,
+ gauge1);
+
+ OnosMetrics.registerMetric(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ GAUGE2_NAME,
+ gauge2);
+
+ OnosMetrics.registerMetric(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ GAUGE3_NAME,
+ gauge3);
+ }
/**
* Check that the JSON for a Gauge obect has the correct data values.
@@ -117,6 +139,7 @@
*/
@Test
public void testGauges() throws Exception {
+ registerGauges();
// Read the metrics from the REST API for the test data
final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
@@ -134,15 +157,15 @@
// Check the values for gauge 1
final JSONObject gauge1Container = gauges.getJSONObject(0);
- checkGauge(gauge1Container, GAUGE1_NAME, gauge1);
+ checkGauge(gauge1Container, GAUGE1_FULL_NAME, gauge1);
// Check the values for gauge 2
final JSONObject gauge2Container = gauges.getJSONObject(1);
- checkGauge(gauge2Container, GAUGE2_NAME, gauge2);
+ checkGauge(gauge2Container, GAUGE2_FULL_NAME, gauge2);
// Check the values for gauge 3
final JSONObject gauge3Container = gauges.getJSONObject(2);
- checkGauge(gauge3Container, GAUGE3_NAME, gauge3);
+ checkGauge(gauge3Container, GAUGE3_FULL_NAME, gauge3);
}
}
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsHistograms.java b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsHistograms.java
index ae528db..772174e 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsHistograms.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsHistograms.java
@@ -56,23 +56,46 @@
// Test data for Histograms
private static final String HISTOGRAM1_NAME = "HISTOGRAM1";
+ private static final String HISTOGRAM1_FULL_NAME =
+ OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ HISTOGRAM1_NAME);
private static final int[] HISTOGRAM1_VALUES =
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
private static final String HISTOGRAM2_NAME = "HISTOGRAM2";
+ private static final String HISTOGRAM2_FULL_NAME =
+ OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ HISTOGRAM2_NAME);
private static final int[] HISTOGRAM2_VALUES =
{100, 100, 100, 100, 100, 100, 100};
private static final String HISTOGRAM3_NAME = "HISTOGRAM3";
+ private static final String HISTOGRAM3_FULL_NAME =
+ OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ HISTOGRAM3_NAME);
private static final int[] HISTOGRAM3_VALUES =
{555};
private final Histogram histogram1 =
- OnosMetrics.getMetricsRegistry().histogram(HISTOGRAM1_NAME);
+ OnosMetrics.createHistogram(
+ OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ HISTOGRAM1_NAME);
+
private final Histogram histogram2 =
- OnosMetrics.getMetricsRegistry().histogram(HISTOGRAM2_NAME);
+ OnosMetrics.createHistogram(
+ OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ HISTOGRAM2_NAME);
+
private final Histogram histogram3 =
- OnosMetrics.getMetricsRegistry().histogram(HISTOGRAM3_NAME);
+ OnosMetrics.createHistogram(
+ OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ HISTOGRAM3_NAME);
/**
* Add each int in an array to a Histogram.
@@ -188,15 +211,15 @@
// Check the values for histogram 1
final JSONObject histogram1Container = histograms.getJSONObject(0);
- checkHistogram(histogram1Container, HISTOGRAM1_NAME, HISTOGRAM1_VALUES);
+ checkHistogram(histogram1Container, HISTOGRAM1_FULL_NAME, HISTOGRAM1_VALUES);
// Check the values for histogram 2
final JSONObject histogram2Container = histograms.getJSONObject(1);
- checkHistogram(histogram2Container, HISTOGRAM2_NAME, HISTOGRAM2_VALUES);
+ checkHistogram(histogram2Container, HISTOGRAM2_FULL_NAME, HISTOGRAM2_VALUES);
// Check the values for histogram 3
final JSONObject histogram3Container = histograms.getJSONObject(2);
- checkHistogram(histogram3Container, HISTOGRAM3_NAME, HISTOGRAM3_VALUES);
+ checkHistogram(histogram3Container, HISTOGRAM3_FULL_NAME, HISTOGRAM3_VALUES);
}
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsMeters.java b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsMeters.java
index c1b21b1..4cfb631 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsMeters.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsMeters.java
@@ -1,8 +1,6 @@
package net.onrc.onos.api.rest;
import com.codahale.metrics.Meter;
-import com.codahale.metrics.Metric;
-import com.codahale.metrics.MetricSet;
import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule;
import net.onrc.onos.core.metrics.OnosMetrics;
import org.json.JSONArray;
@@ -16,9 +14,6 @@
import org.powermock.modules.junit4.PowerMockRunner;
import org.restlet.resource.ClientResource;
-import java.util.HashMap;
-import java.util.Map;
-
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.equalTo;
@@ -58,12 +53,27 @@
// Test data for Meters
private static final String METER1_NAME = "METER1";
+ private static final String METER1_FULL_NAME =
+ OnosMetrics.generateName(
+ OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ METER1_NAME);
private static final int METER1_ITERATIONS = 1;
private static final String METER2_NAME = "METER2";
+ private static final String METER2_FULL_NAME =
+ OnosMetrics.generateName(
+ OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ METER2_NAME);
private static final int METER2_ITERATIONS = 10;
private static final String METER3_NAME = "METER3";
+ private static final String METER3_FULL_NAME =
+ OnosMetrics.generateName(
+ OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ METER3_NAME);
private static final int METER3_ITERATIONS = 100;
private final Meter meter1 = new Meter(mockClock);
@@ -91,17 +101,20 @@
fillMeter(meter2, METER2_ITERATIONS);
fillMeter(meter3, METER3_ITERATIONS);
- final MetricSet meterSet = new MetricSet() {
- @Override
- public Map<String, Metric> getMetrics() {
- final Map<String, Metric> meters = new HashMap<>();
- meters.put(METER1_NAME, meter1);
- meters.put(METER2_NAME, meter2);
- meters.put(METER3_NAME, meter3);
- return meters;
- }
- };
- OnosMetrics.getMetricsRegistry().registerAll(meterSet);
+ OnosMetrics.registerMetric(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ METER1_NAME,
+ meter1);
+
+ OnosMetrics.registerMetric(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ METER2_NAME,
+ meter2);
+
+ OnosMetrics.registerMetric(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ METER3_NAME,
+ meter3);
}
/**
@@ -175,15 +188,15 @@
// Check the values for meter 1
final JSONObject meter1Container = meters.getJSONObject(0);
- checkMeter(meter1, meter1Container, METER1_NAME, METER1_ITERATIONS);
+ checkMeter(meter1, meter1Container, METER1_FULL_NAME, METER1_ITERATIONS);
// Check the values for meter 2
final JSONObject meter2Container = meters.getJSONObject(1);
- checkMeter(meter2, meter2Container, METER2_NAME, METER2_ITERATIONS);
+ checkMeter(meter2, meter2Container, METER2_FULL_NAME, METER2_ITERATIONS);
// Check the values for meter 3
final JSONObject meter3Container = meters.getJSONObject(2);
- checkMeter(meter3, meter3Container, METER3_NAME, METER3_ITERATIONS);
+ checkMeter(meter3, meter3Container, METER3_FULL_NAME, METER3_ITERATIONS);
}
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsTimers.java b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsTimers.java
index e6670b4..af0e7f8 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsTimers.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsTimers.java
@@ -1,7 +1,5 @@
package net.onrc.onos.api.rest;
-import com.codahale.metrics.Metric;
-import com.codahale.metrics.MetricSet;
import com.codahale.metrics.Reservoir;
import com.codahale.metrics.SlidingWindowReservoir;
import com.codahale.metrics.Timer;
@@ -18,10 +16,6 @@
import org.powermock.modules.junit4.PowerMockRunner;
import org.restlet.resource.ClientResource;
-import java.util.HashMap;
-import java.util.Map;
-
-import static com.codahale.metrics.MetricRegistry.name;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.equalTo;
@@ -52,13 +46,19 @@
// Test data objects for Timers
// timer1 will be called 3 times
- private static final String TIMER1_NAME = name(TestRestMetricsTimers.class,
- "timer1");
+ private static final String TIMER1_NAME = "timer1";
+ private static final String TIMER1_FULL_NAME =
+ OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ TIMER1_NAME);
private static final int TIMER1_COUNT = 3;
- // timer1 will be called 10 times
- private static final String TIMER2_NAME = name(TestRestMetricsTimers.class,
- "timer2");
+ // timer2 will be called 10 times
+ private static final String TIMER2_NAME = "timer2";
+ private static final String TIMER2_FULL_NAME =
+ OnosMetrics.generateName(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ TIMER2_NAME);
private static final int TIMER2_COUNT = 10;
private static final int RESERVOIR_SIZE = 100;
@@ -89,16 +89,16 @@
// add the two timers to the registry so the REST APIs will pick them
// up
- final MetricSet timerSet = new MetricSet() {
- @Override
- public Map<String, Metric> getMetrics() {
- final Map<String, Metric> timers = new HashMap<>();
- timers.put(TIMER1_NAME, timer1);
- timers.put(TIMER2_NAME, timer2);
- return timers;
- }
- };
- OnosMetrics.getMetricsRegistry().registerAll(timerSet);
+
+ OnosMetrics.registerMetric(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ TIMER1_NAME,
+ timer1);
+
+ OnosMetrics.registerMetric(OnosMetrics.MetricsComponents.GLOBAL,
+ OnosMetrics.MetricsFeatures.GLOBAL,
+ TIMER2_NAME,
+ timer2);
}
/**
@@ -174,7 +174,7 @@
final String timer1Name = timer1Values.getString("name");
assertThat(timer1Name, is(notNullValue()));
- assertThat(timer1Name, is(equalTo(TIMER1_NAME)));
+ assertThat(timer1Name, is(equalTo(TIMER1_FULL_NAME)));
final JSONObject timer1TimerObject = timer1Values.getJSONObject("timer");
assertThat(timer1TimerObject, is(notNullValue()));
@@ -194,7 +194,7 @@
final String timer2Name = timer2Values.getString("name");
assertThat(timer2Name, is(notNullValue()));
- assertThat(timer2Name, is(equalTo(TIMER2_NAME)));
+ assertThat(timer2Name, is(equalTo(TIMER2_FULL_NAME)));
final JSONObject timer2TimerObject = timer2Values.getJSONObject("timer");
assertThat(timer2TimerObject, is(notNullValue()));