blob: d16c62c7fbde7232dbe141fb4ec339eecec397b1 [file] [log] [blame]
Jian Li72315152015-12-10 17:20:43 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jian Li72315152015-12-10 17:20:43 -08003 *
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 */
16package org.onosproject.rest.resources;
17
18import com.codahale.metrics.Counter;
19import com.codahale.metrics.Gauge;
20import com.codahale.metrics.Metric;
21import com.codahale.metrics.Histogram;
22import com.codahale.metrics.Meter;
23import com.codahale.metrics.Timer;
24import com.codahale.metrics.MetricFilter;
25import com.fasterxml.jackson.databind.node.ArrayNode;
26import com.fasterxml.jackson.databind.node.ObjectNode;
27import com.google.common.collect.Ordering;
28import com.google.common.collect.TreeMultimap;
29import org.onlab.metrics.MetricsService;
30import org.onosproject.rest.AbstractWebResource;
Jayasree Ghoshcfcf52c2016-09-21 18:15:18 +053031import org.onlab.util.ItemNotFoundException;
Jian Li72315152015-12-10 17:20:43 -080032
33import javax.ws.rs.GET;
34import javax.ws.rs.Path;
35import javax.ws.rs.PathParam;
36import javax.ws.rs.Produces;
37import javax.ws.rs.core.MediaType;
38import javax.ws.rs.core.Response;
39import java.util.Comparator;
40import java.util.Map;
41
42/**
43 * Query metrics.
44 */
45@Path("metrics")
46public class MetricsWebResource extends AbstractWebResource {
47
Jayasree Ghoshcfcf52c2016-09-21 18:15:18 +053048 private static final String E_METRIC_NAME_NOT_FOUND = "Metric Name is not found";
49
Jian Licc730a62016-05-10 16:36:16 -070050 private final MetricsService service = get(MetricsService.class);
51 private final ObjectNode root = mapper().createObjectNode();
Jian Li72315152015-12-10 17:20:43 -080052
53 /**
Jian Licc730a62016-05-10 16:36:16 -070054 * Gets stats information of all metrics. Returns array of all information for
Jian Li72315152015-12-10 17:20:43 -080055 * all metrics.
56 *
Jian Licc730a62016-05-10 16:36:16 -070057 * @return 200 OK with metric information as array
Jian Li72315152015-12-10 17:20:43 -080058 * @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 Licc730a62016-05-10 16:36:16 -070075 * Gets stats information of a metric. Returns array of all information for the
Jian Li72315152015-12-10 17:20:43 -080076 * specified metric.
77 *
78 * @param metricName metric name
Jian Licc730a62016-05-10 16:36:16 -070079 * @return 200 OK with metric information as array
Jian Li72315152015-12-10 17:20:43 -080080 * @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 Ghoshcfcf52c2016-09-21 18:15:18 +053090 if (matched.isEmpty()) {
91 throw new ItemNotFoundException(E_METRIC_NAME_NOT_FOUND);
92 }
93
Jian Li72315152015-12-10 17:20:43 -080094 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}