Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.rest.resources; |
| 17 | |
| 18 | import com.codahale.metrics.Counter; |
| 19 | import com.codahale.metrics.Gauge; |
| 20 | import com.codahale.metrics.Metric; |
| 21 | import com.codahale.metrics.Histogram; |
| 22 | import com.codahale.metrics.Meter; |
| 23 | import com.codahale.metrics.Timer; |
| 24 | import com.codahale.metrics.MetricFilter; |
| 25 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 26 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 27 | import com.google.common.collect.Ordering; |
| 28 | import com.google.common.collect.TreeMultimap; |
| 29 | import org.onlab.metrics.MetricsService; |
| 30 | import org.onosproject.rest.AbstractWebResource; |
| 31 | |
| 32 | import javax.ws.rs.GET; |
| 33 | import javax.ws.rs.Path; |
| 34 | import javax.ws.rs.PathParam; |
| 35 | import javax.ws.rs.Produces; |
| 36 | import javax.ws.rs.core.MediaType; |
| 37 | import javax.ws.rs.core.Response; |
| 38 | import java.util.Comparator; |
| 39 | import java.util.Map; |
| 40 | |
| 41 | /** |
| 42 | * Query metrics. |
| 43 | */ |
| 44 | @Path("metrics") |
| 45 | public class MetricsWebResource extends AbstractWebResource { |
| 46 | |
| 47 | final MetricsService service = get(MetricsService.class); |
| 48 | final ObjectNode root = mapper().createObjectNode(); |
| 49 | |
| 50 | /** |
| 51 | * Get stats information of all metrics. Returns array of all information for |
| 52 | * all metrics. |
| 53 | * |
| 54 | * @return metric information as array |
| 55 | * @onos.rsModel Metrics |
| 56 | */ |
| 57 | @GET |
| 58 | @Produces(MediaType.APPLICATION_JSON) |
| 59 | public Response getAllMetrics() { |
| 60 | ArrayNode metricsNode = root.putArray("metrics"); |
| 61 | service.getMetrics().forEach((name, metric) -> { |
| 62 | ObjectNode item = mapper().createObjectNode(); |
| 63 | item.put("name", name); |
| 64 | item.set("metric", codec(Metric.class).encode(metric, this)); |
| 65 | metricsNode.add(item); |
| 66 | }); |
| 67 | |
| 68 | return ok(root).build(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get stats information of a metric. Returns array of all information for the |
| 73 | * specified metric. |
| 74 | * |
| 75 | * @param metricName metric name |
| 76 | * @return metric information as array |
| 77 | * @onos.rsModel Metric |
| 78 | */ |
| 79 | @GET |
| 80 | @Produces(MediaType.APPLICATION_JSON) |
| 81 | @Path("{metricName}") |
| 82 | public Response getMetricByName(@PathParam("metricName") String metricName) { |
| 83 | ObjectNode metricNode = root.putObject("metric"); |
| 84 | MetricFilter filter = metricName != null ? (name, metric) -> name.equals(metricName) : MetricFilter.ALL; |
| 85 | TreeMultimap<String, Metric> matched = listMetrics(service, filter); |
| 86 | |
| 87 | matched.asMap().get(metricName).forEach(m -> { |
| 88 | metricNode.set(metricName, codec(Metric.class).encode(m, this)); |
| 89 | }); |
| 90 | |
| 91 | return ok(root).build(); |
| 92 | } |
| 93 | |
| 94 | private TreeMultimap<String, Metric> listMetrics(MetricsService metricsService, MetricFilter filter) { |
| 95 | TreeMultimap<String, Metric> metrics = TreeMultimap.create(Comparator.naturalOrder(), Ordering.arbitrary()); |
| 96 | |
| 97 | Map<String, Counter> counters = metricsService.getCounters(filter); |
| 98 | for (Map.Entry<String, Counter> entry : counters.entrySet()) { |
| 99 | metrics.put(entry.getKey(), entry.getValue()); |
| 100 | } |
| 101 | Map<String, Gauge> gauges = metricsService.getGauges(filter); |
| 102 | for (Map.Entry<String, Gauge> entry : gauges.entrySet()) { |
| 103 | metrics.put(entry.getKey(), entry.getValue()); |
| 104 | } |
| 105 | Map<String, Histogram> histograms = metricsService.getHistograms(filter); |
| 106 | for (Map.Entry<String, Histogram> entry : histograms.entrySet()) { |
| 107 | metrics.put(entry.getKey(), entry.getValue()); |
| 108 | } |
| 109 | Map<String, Meter> meters = metricsService.getMeters(filter); |
| 110 | for (Map.Entry<String, Meter> entry : meters.entrySet()) { |
| 111 | metrics.put(entry.getKey(), entry.getValue()); |
| 112 | } |
| 113 | Map<String, Timer> timers = metricsService.getTimers(filter); |
| 114 | for (Map.Entry<String, Timer> entry : timers.entrySet()) { |
| 115 | metrics.put(entry.getKey(), entry.getValue()); |
| 116 | } |
| 117 | |
| 118 | return metrics; |
| 119 | } |
| 120 | } |