Jian Li
Committed by Gerrit Code Review

[Falcon][ONOS-3601] Add REST API for metrics service with unit test

Change-Id: I33ec561d1d83c6f1167e960bc2f684a117e6ea9c
......@@ -15,6 +15,7 @@
*/
package org.onosproject.codec.impl;
import com.codahale.metrics.Metric;
import com.google.common.collect.ImmutableSet;
import org.apache.felix.scr.annotations.Activate;
......@@ -104,6 +105,7 @@ public class CodecManager implements CodecService {
registerCodec(Load.class, new LoadCodec());
registerCodec(TableStatisticsEntry.class, new TableStatisticsEntryCodec());
registerCodec(PortStatistics.class, new PortStatisticsCodec());
registerCodec(Metric.class, new MetricCodec());
log.info("Started");
}
......
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.codec.impl;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Metric;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Timer;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Codec for the Metric class.
*/
public class MetricCodec extends JsonCodec<Metric> {
// JSON field names
private static final String COUNTER = "counter";
private static final String GAUGE = "gauge";
private static final String VALUE = "value";
private static final String METER = "meter";
private static final String MEAN_RATE = "mean_rate";
private static final String ONE_MIN_RATE = "1_min_rate";
private static final String FIVE_MIN_RATE = "5_min_rate";
private static final String FIFT_MIN_RATE = "15_min_rate";
private static final String HISTOGRAM = "histogram";
private static final String MIN = "min";
private static final String MAX = "max";
private static final String MEAN = "mean";
private static final String STDDEV = "stddev";
private static final String TIMER = "timer";
@Override
public ObjectNode encode(Metric metric, CodecContext context) {
checkNotNull(metric, "Metric cannot be null");
ObjectNode objectNode = context.mapper().createObjectNode();
ObjectNode dataNode = context.mapper().createObjectNode();
if (metric instanceof Counter) {
dataNode.put(COUNTER, ((Counter) metric).getCount());
objectNode.set(COUNTER, dataNode);
} else if (metric instanceof Gauge) {
objectNode.put(VALUE, ((Gauge) metric).getValue().toString());
objectNode.set(GAUGE, dataNode);
} else if (metric instanceof Meter) {
dataNode.put(COUNTER, ((Meter) metric).getCount());
dataNode.put(MEAN_RATE, ((Meter) metric).getMeanRate());
dataNode.put(ONE_MIN_RATE, ((Meter) metric).getOneMinuteRate());
dataNode.put(FIVE_MIN_RATE, ((Meter) metric).getFiveMinuteRate());
dataNode.put(FIFT_MIN_RATE, ((Meter) metric).getFifteenMinuteRate());
objectNode.set(METER, dataNode);
} else if (metric instanceof Histogram) {
dataNode.put(COUNTER, ((Histogram) metric).getCount());
dataNode.put(MEAN, ((Histogram) metric).getSnapshot().getMean());
dataNode.put(MIN, ((Histogram) metric).getSnapshot().getMin());
dataNode.put(MAX, ((Histogram) metric).getSnapshot().getMax());
dataNode.put(STDDEV, ((Histogram) metric).getSnapshot().getStdDev());
objectNode.set(HISTOGRAM, dataNode);
} else if (metric instanceof Timer) {
dataNode.put(COUNTER, ((Timer) metric).getCount());
dataNode.put(MEAN_RATE, ((Timer) metric).getMeanRate());
dataNode.put(ONE_MIN_RATE, ((Timer) metric).getOneMinuteRate());
dataNode.put(FIVE_MIN_RATE, ((Timer) metric).getFiveMinuteRate());
dataNode.put(FIFT_MIN_RATE, ((Timer) metric).getFifteenMinuteRate());
dataNode.put(MEAN, nanoToMs(((Timer) metric).getSnapshot().getMean()));
dataNode.put(MIN, nanoToMs(((Timer) metric).getSnapshot().getMin()));
dataNode.put(MAX, nanoToMs(((Timer) metric).getSnapshot().getMax()));
dataNode.put(STDDEV, nanoToMs(((Timer) metric).getSnapshot().getStdDev()));
objectNode.set(TIMER, dataNode);
}
return objectNode;
}
private double nanoToMs(double nano) {
return nano / 1_000_000D;
}
}
......@@ -102,7 +102,9 @@
org.onlab.osgi.*,
org.onlab.packet.*,
org.onlab.rest.*,
org.onosproject.*
org.onosproject.*,
org.onlab.metrics.*,
com.codahale.metrics.*
</Import-Package>
<Web-ContextPath>${web.context}</Web-ContextPath>
</instructions>
......
......@@ -40,7 +40,8 @@ public class CoreWebApplication extends AbstractWebApplication {
TopologyWebResource.class,
ConfigWebResource.class,
PathsWebResource.class,
StatisticsWebResource.class
StatisticsWebResource.class,
MetricsWebResource.class
);
}
}
......
/*
* Copyright 2014-2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.rest.resources;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Metric;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Timer;
import com.codahale.metrics.MetricFilter;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Ordering;
import com.google.common.collect.TreeMultimap;
import org.onlab.metrics.MetricsService;
import org.onosproject.rest.AbstractWebResource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Comparator;
import java.util.Map;
/**
* Query metrics.
*/
@Path("metrics")
public class MetricsWebResource extends AbstractWebResource {
final MetricsService service = get(MetricsService.class);
final ObjectNode root = mapper().createObjectNode();
/**
* Get stats information of all metrics. Returns array of all information for
* all metrics.
*
* @return metric information as array
* @onos.rsModel Metrics
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAllMetrics() {
ArrayNode metricsNode = root.putArray("metrics");
service.getMetrics().forEach((name, metric) -> {
ObjectNode item = mapper().createObjectNode();
item.put("name", name);
item.set("metric", codec(Metric.class).encode(metric, this));
metricsNode.add(item);
});
return ok(root).build();
}
/**
* Get stats information of a metric. Returns array of all information for the
* specified metric.
*
* @param metricName metric name
* @return metric information as array
* @onos.rsModel Metric
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{metricName}")
public Response getMetricByName(@PathParam("metricName") String metricName) {
ObjectNode metricNode = root.putObject("metric");
MetricFilter filter = metricName != null ? (name, metric) -> name.equals(metricName) : MetricFilter.ALL;
TreeMultimap<String, Metric> matched = listMetrics(service, filter);
matched.asMap().get(metricName).forEach(m -> {
metricNode.set(metricName, codec(Metric.class).encode(m, this));
});
return ok(root).build();
}
private TreeMultimap<String, Metric> listMetrics(MetricsService metricsService, MetricFilter filter) {
TreeMultimap<String, Metric> metrics = TreeMultimap.create(Comparator.naturalOrder(), Ordering.arbitrary());
Map<String, Counter> counters = metricsService.getCounters(filter);
for (Map.Entry<String, Counter> entry : counters.entrySet()) {
metrics.put(entry.getKey(), entry.getValue());
}
Map<String, Gauge> gauges = metricsService.getGauges(filter);
for (Map.Entry<String, Gauge> entry : gauges.entrySet()) {
metrics.put(entry.getKey(), entry.getValue());
}
Map<String, Histogram> histograms = metricsService.getHistograms(filter);
for (Map.Entry<String, Histogram> entry : histograms.entrySet()) {
metrics.put(entry.getKey(), entry.getValue());
}
Map<String, Meter> meters = metricsService.getMeters(filter);
for (Map.Entry<String, Meter> entry : meters.entrySet()) {
metrics.put(entry.getKey(), entry.getValue());
}
Map<String, Timer> timers = metricsService.getTimers(filter);
for (Map.Entry<String, Timer> entry : timers.entrySet()) {
metrics.put(entry.getKey(), entry.getValue());
}
return metrics;
}
}
{
"type": "object",
"title": "metric",
"required": [
"name",
"metric"
],
"properties": {
"name": {
"type": "string",
"example": "cpu"
},
"metric": {
"type": "object",
"title": "metric",
"optional": [
"counter",
"gauge",
"meter",
"histogram",
"timer"
],
"properties": {
"counter": {
"type": "object",
"required": [
"counter"
],
"properties": {
"counter": {
"type": "integer",
"example": "1"
}
}
},
"gauge": {
"type": "object",
"required": [
"value"
],
"properties": {
"value": "string",
"example": "1"
}
},
"meter": {
"type": "object",
"required": [
"counter",
"mean_rate",
"1_min_rate",
"5_min_rate",
"15_min_rate"
],
"properties": {
"counter": {
"type": "integer",
"example": "1"
},
"mean_rate": {
"type": "double",
"example": "1.0"
},
"1_min_rate": {
"type": "double",
"example": "1.0"
},
"5_min_rate": {
"type": "double",
"example": "1.0"
},
"15_min_rate": {
"type": "double",
"example": "1.0"
}
}
},
"histogram": {
"type": "object",
"required": [
"counter",
"mean",
"min",
"max",
"stddev"
],
"properties": {
"counter": {
"type": "integer",
"example": "1"
},
"mean": {
"type": "double",
"example": "1.0"
},
"min": {
"type": "double",
"example": "1.0"
},
"max": {
"type": "double",
"example": "1.0"
},
"stddev": {
"type": "double",
"example": "1.0"
}
}
},
"timer": {
"type": "object",
"required": [
"counter",
"mean_rate",
"1_min_rate",
"5_min_rate",
"15_min_rate",
"mean",
"min",
"max",
"stddev"
],
"properties": {
"counter": {
"type": "integer",
"example": "1"
},
"mean_rate": {
"type": "double",
"example": "1.0"
},
"1_min_rate": {
"type": "double",
"example": "1.0"
},
"5_min_rate": {
"type": "double",
"example": "1.0"
},
"15_min_rate": {
"type": "double",
"example": "1.0"
},
"mean": {
"type": "double",
"example": "1.0"
},
"min": {
"type": "double",
"example": "1.0"
},
"max": {
"type": "double",
"example": "1.0"
},
"stddev": {
"type": "double",
"example": "1.0"
}
}
}
}
}
}
}
\ No newline at end of file
{
"type": "object",
"title": "metrics",
"required": [
"metrics"
],
"properties": {
"metrics": {
"type": "array",
"xml": {
"name": "metrics",
"wrapped": true
},
"items": {
"type": "object",
"title": "metric",
"required": [
"name",
"metric"
],
"properties": {
"name": {
"type": "string",
"example": "cpu"
},
"metric": {
"type": "object",
"optional": [
"counter",
"gauge",
"meter",
"histogram",
"timer"
],
"properties": {
"counter": {
"type": "object",
"required": [
"counter"
],
"properties": {
"counter": {
"type": "integer",
"example": "1"
}
}
},
"gauge": {
"type": "object",
"required": [
"value"
],
"properties": {
"value": "string",
"example": "1"
}
},
"meter": {
"type": "object",
"required": [
"counter",
"mean_rate",
"1_min_rate",
"5_min_rate",
"15_min_rate"
],
"properties": {
"counter": {
"type": "integer",
"example": "1"
},
"mean_rate": {
"type": "double",
"example": "1.0"
},
"1_min_rate": {
"type": "double",
"example": "1.0"
},
"5_min_rate": {
"type": "double",
"example": "1.0"
},
"15_min_rate": {
"type": "double",
"example": "1.0"
}
}
},
"histogram": {
"type": "object",
"required": [
"counter",
"mean",
"min",
"max",
"stddev"
],
"properties": {
"counter": {
"type": "integer",
"example": "1"
},
"mean": {
"type": "double",
"example": "1.0"
},
"min": {
"type": "double",
"example": "1.0"
},
"max": {
"type": "double",
"example": "1.0"
},
"stddev": {
"type": "double",
"example": "1.0"
}
}
},
"timer": {
"type": "object",
"required": [
"counter",
"mean_rate",
"1_min_rate",
"5_min_rate",
"15_min_rate",
"mean",
"min",
"max",
"stddev"
],
"properties": {
"counter": {
"type": "integer",
"example": "1"
},
"mean_rate": {
"type": "double",
"example": "1.0"
},
"1_min_rate": {
"type": "double",
"example": "1.0"
},
"5_min_rate": {
"type": "double",
"example": "1.0"
},
"15_min_rate": {
"type": "double",
"example": "1.0"
},
"mean": {
"type": "double",
"example": "1.0"
},
"min": {
"type": "double",
"example": "1.0"
},
"max": {
"type": "double",
"example": "1.0"
},
"stddev": {
"type": "double",
"example": "1.0"
}
}
}
}
}
}
}
}
}
}
\ No newline at end of file
/*
* Copyright 2014-2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.rest;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Metric;
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.google.common.collect.ImmutableMap;
import com.sun.jersey.api.client.WebResource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.metrics.MetricsService;
import org.onlab.osgi.ServiceDirectory;
import org.onlab.osgi.TestServiceDirectory;
import org.onlab.rest.BaseResource;
import org.onosproject.codec.CodecService;
import org.onosproject.codec.impl.CodecManager;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
/**
* Unit tests for Metrics REST APIs.
*/
public class MetricsResourceTest extends ResourceTest {
MetricsService mockMetricsService;
/**
* Initializes test mocks and environment.
*/
@Before
public void setUpTest() {
mockMetricsService = createMock(MetricsService.class);
// Register the services needed for the test
final CodecManager codecService = new CodecManager();
codecService.activate();
ServiceDirectory testDirectory =
new TestServiceDirectory()
.add(MetricsService.class, mockMetricsService)
.add(CodecService.class, codecService);
BaseResource.setServiceDirectory(testDirectory);
}
/**
* Verifies mocks.
*/
@After
public void tearDownTest() {
verify(mockMetricsService);
}
/**
* Tests that a fetch of a non-existent object throws an exception.
*/
@Test
public void testBadGet() {
Counter onosCounter = new Counter();
onosCounter.inc();
Meter onosMeter = new Meter();
onosMeter.mark();
ImmutableMap<String, Metric> metrics =
new ImmutableMap.Builder<String, Metric>()
.put("onosCounter", onosCounter)
.put("onosMeter", onosMeter)
.build();
expect(mockMetricsService.getMetrics())
.andReturn(metrics)
.anyTimes();
replay(mockMetricsService);
WebResource rs = resource();
String response = rs.path("metrics").get(String.class);
assertThat(response, containsString("{\"metrics\":["));
JsonObject result = JsonObject.readFrom(response);
assertThat(result, notNullValue());
JsonArray jsonMetrics = result.get("metrics").asArray();
assertThat(jsonMetrics, notNullValue());
assertThat(jsonMetrics.size(), is(2));
}
}