Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame] | 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; |
Jayasree Ghosh | cfcf52c | 2016-09-21 18:15:18 +0530 | [diff] [blame] | 31 | import org.onlab.util.ItemNotFoundException; |
Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame] | 32 | |
| 33 | import javax.ws.rs.GET; |
| 34 | import javax.ws.rs.Path; |
| 35 | import javax.ws.rs.PathParam; |
| 36 | import javax.ws.rs.Produces; |
| 37 | import javax.ws.rs.core.MediaType; |
| 38 | import javax.ws.rs.core.Response; |
| 39 | import java.util.Comparator; |
| 40 | import java.util.Map; |
| 41 | |
| 42 | /** |
| 43 | * Query metrics. |
| 44 | */ |
| 45 | @Path("metrics") |
| 46 | public class MetricsWebResource extends AbstractWebResource { |
| 47 | |
Jayasree Ghosh | cfcf52c | 2016-09-21 18:15:18 +0530 | [diff] [blame] | 48 | private static final String E_METRIC_NAME_NOT_FOUND = "Metric Name is not found"; |
| 49 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 50 | private final MetricsService service = get(MetricsService.class); |
| 51 | private final ObjectNode root = mapper().createObjectNode(); |
Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame] | 52 | |
| 53 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 54 | * Gets stats information of all metrics. Returns array of all information for |
Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame] | 55 | * all metrics. |
| 56 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 57 | * @return 200 OK with metric information as array |
Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame] | 58 | * @onos.rsModel Metrics |
| 59 | */ |
| 60 | @GET |
| 61 | @Produces(MediaType.APPLICATION_JSON) |
| 62 | public Response getAllMetrics() { |
| 63 | ArrayNode metricsNode = root.putArray("metrics"); |
| 64 | service.getMetrics().forEach((name, metric) -> { |
| 65 | ObjectNode item = mapper().createObjectNode(); |
| 66 | item.put("name", name); |
| 67 | item.set("metric", codec(Metric.class).encode(metric, this)); |
| 68 | metricsNode.add(item); |
| 69 | }); |
| 70 | |
| 71 | return ok(root).build(); |
| 72 | } |
| 73 | |
| 74 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 75 | * Gets stats information of a metric. Returns array of all information for the |
Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame] | 76 | * specified metric. |
| 77 | * |
| 78 | * @param metricName metric name |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 79 | * @return 200 OK with metric information as array |
Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame] | 80 | * @onos.rsModel Metric |
| 81 | */ |
| 82 | @GET |
| 83 | @Produces(MediaType.APPLICATION_JSON) |
| 84 | @Path("{metricName}") |
| 85 | public Response getMetricByName(@PathParam("metricName") String metricName) { |
| 86 | ObjectNode metricNode = root.putObject("metric"); |
| 87 | MetricFilter filter = metricName != null ? (name, metric) -> name.equals(metricName) : MetricFilter.ALL; |
| 88 | TreeMultimap<String, Metric> matched = listMetrics(service, filter); |
| 89 | |
Jayasree Ghosh | cfcf52c | 2016-09-21 18:15:18 +0530 | [diff] [blame] | 90 | if (matched.isEmpty()) { |
| 91 | throw new ItemNotFoundException(E_METRIC_NAME_NOT_FOUND); |
| 92 | } |
| 93 | |
Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame] | 94 | matched.asMap().get(metricName).forEach(m -> { |
| 95 | metricNode.set(metricName, codec(Metric.class).encode(m, this)); |
| 96 | }); |
| 97 | |
| 98 | return ok(root).build(); |
| 99 | } |
| 100 | |
| 101 | private TreeMultimap<String, Metric> listMetrics(MetricsService metricsService, MetricFilter filter) { |
| 102 | TreeMultimap<String, Metric> metrics = TreeMultimap.create(Comparator.naturalOrder(), Ordering.arbitrary()); |
| 103 | |
| 104 | Map<String, Counter> counters = metricsService.getCounters(filter); |
| 105 | for (Map.Entry<String, Counter> entry : counters.entrySet()) { |
| 106 | metrics.put(entry.getKey(), entry.getValue()); |
| 107 | } |
| 108 | Map<String, Gauge> gauges = metricsService.getGauges(filter); |
| 109 | for (Map.Entry<String, Gauge> entry : gauges.entrySet()) { |
| 110 | metrics.put(entry.getKey(), entry.getValue()); |
| 111 | } |
| 112 | Map<String, Histogram> histograms = metricsService.getHistograms(filter); |
| 113 | for (Map.Entry<String, Histogram> entry : histograms.entrySet()) { |
| 114 | metrics.put(entry.getKey(), entry.getValue()); |
| 115 | } |
| 116 | Map<String, Meter> meters = metricsService.getMeters(filter); |
| 117 | for (Map.Entry<String, Meter> entry : meters.entrySet()) { |
| 118 | metrics.put(entry.getKey(), entry.getValue()); |
| 119 | } |
| 120 | Map<String, Timer> timers = metricsService.getTimers(filter); |
| 121 | for (Map.Entry<String, Timer> entry : timers.entrySet()) { |
| 122 | metrics.put(entry.getKey(), entry.getValue()); |
| 123 | } |
| 124 | |
| 125 | return metrics; |
| 126 | } |
| 127 | } |