blob: 31f23ddd504d6266e977369b0175e4dceea673ab [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;
31
32import javax.ws.rs.GET;
33import javax.ws.rs.Path;
34import javax.ws.rs.PathParam;
35import javax.ws.rs.Produces;
36import javax.ws.rs.core.MediaType;
37import javax.ws.rs.core.Response;
38import java.util.Comparator;
39import java.util.Map;
40
41/**
42 * Query metrics.
43 */
44@Path("metrics")
45public class MetricsWebResource extends AbstractWebResource {
46
Jian Licc730a62016-05-10 16:36:16 -070047 private final MetricsService service = get(MetricsService.class);
48 private final ObjectNode root = mapper().createObjectNode();
Jian Li72315152015-12-10 17:20:43 -080049
50 /**
Jian Licc730a62016-05-10 16:36:16 -070051 * Gets stats information of all metrics. Returns array of all information for
Jian Li72315152015-12-10 17:20:43 -080052 * all metrics.
53 *
Jian Licc730a62016-05-10 16:36:16 -070054 * @return 200 OK with metric information as array
Jian Li72315152015-12-10 17:20:43 -080055 * @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 /**
Jian Licc730a62016-05-10 16:36:16 -070072 * Gets stats information of a metric. Returns array of all information for the
Jian Li72315152015-12-10 17:20:43 -080073 * specified metric.
74 *
75 * @param metricName metric name
Jian Licc730a62016-05-10 16:36:16 -070076 * @return 200 OK with metric information as array
Jian Li72315152015-12-10 17:20:43 -080077 * @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}