blob: 89746e3b1721b64e7cce45291c7c1f563a6c539b [file] [log] [blame]
Ray Milkeya34ed042014-07-17 16:19:52 -07001package net.onrc.onos.core.metrics.web;
Ray Milkey26921af2014-06-30 16:27:40 -07002
Ray Milkey14c46512014-07-08 18:30:31 -07003import com.codahale.metrics.Counter;
Ray Milkey26921af2014-06-30 16:27:40 -07004import com.codahale.metrics.Gauge;
Ray Milkey14c46512014-07-08 18:30:31 -07005import com.codahale.metrics.Histogram;
6import com.codahale.metrics.Meter;
7import com.codahale.metrics.Metric;
8import com.codahale.metrics.MetricFilter;
Ray Milkey26921af2014-06-30 16:27:40 -07009import com.codahale.metrics.Timer;
Ray Milkeya34ed042014-07-17 16:19:52 -070010import net.onrc.onos.core.metrics.OnosMetrics;
Ray Milkey26921af2014-06-30 16:27:40 -070011import org.restlet.representation.Representation;
12import org.restlet.resource.Get;
13import org.restlet.resource.ServerResource;
14
15import java.util.ArrayList;
Ray Milkey14c46512014-07-08 18:30:31 -070016import java.util.Arrays;
17import java.util.HashSet;
Ray Milkey26921af2014-06-30 16:27:40 -070018import java.util.List;
19import java.util.Map;
20
21/**
22 * REST APIs for Metrics objects.
23 */
24public class MetricsResource extends ServerResource {
25
26 /**
Ray Milkey14c46512014-07-08 18:30:31 -070027 * Metric filter to allow selecting metrics by name.
28 */
29 private static class MetricNameFilter implements MetricFilter {
30 final HashSet<String> names;
31
32 /**
33 * Hide default constructor.
34 */
35 @SuppressWarnings("unused")
36 private MetricNameFilter() {
37 names = null;
38 }
39
40 /**
41 * Initializes a filter for the given name list.
42 *
43 * @param nameListString comma separated list of strings of the
44 * names of metrics to query
45 */
46 public MetricNameFilter(final String nameListString) {
47
48 if (nameListString == null) {
49 names = null;
50 } else {
51 List<String> nameList = Arrays.asList(nameListString.split(","));
52 names = new HashSet<>();
53 names.addAll(nameList);
54 }
55 }
56
57 @Override
58 public boolean matches(String s, Metric metric) {
59 return names == null || names.contains(s);
60 }
61 }
62 /**
Ray Milkey26921af2014-06-30 16:27:40 -070063 * REST API to get all of the system's metrics.
64 *
65 * @return a Representation object containing the metrics
66 */
67 @Get("json")
68 @SuppressWarnings("rawtypes")
Ray Milkey128651a2014-07-14 11:24:28 -070069 public Representation retrieve() {
Ray Milkey26921af2014-06-30 16:27:40 -070070 final MetricsObjectResource result = new MetricsObjectResource();
71
72 final List<MetricsObjectResource.TimerObjectResource> timers =
73 new ArrayList<>();
74 final List<MetricsObjectResource.GaugeObjectResource> gauges =
75 new ArrayList<>();
76 final List<MetricsObjectResource.CounterObjectResource> counters =
77 new ArrayList<>();
78 final List<MetricsObjectResource.MeterObjectResource> meters =
79 new ArrayList<>();
80 final List<MetricsObjectResource.HistogramObjectResource> histograms =
81 new ArrayList<>();
82
Ray Milkey14c46512014-07-08 18:30:31 -070083 final String metricIdsString = getQuery().getValues("ids");
84
85 final MetricFilter filter = new MetricNameFilter(metricIdsString);
86
Ray Milkey26921af2014-06-30 16:27:40 -070087 for (final Map.Entry<String, Timer> timer :
Ray Milkey128651a2014-07-14 11:24:28 -070088 OnosMetrics.getTimers(filter).entrySet()) {
Ray Milkey26921af2014-06-30 16:27:40 -070089 timers.add(new MetricsObjectResource.TimerObjectResource(
90 timer.getKey(), timer.getValue()));
91 }
92 result.setTimers(timers);
93
94 for (final Map.Entry<String, Gauge> gauge :
Ray Milkey128651a2014-07-14 11:24:28 -070095 OnosMetrics.getGauges(filter).entrySet()) {
Ray Milkey26921af2014-06-30 16:27:40 -070096 gauges.add(new MetricsObjectResource.GaugeObjectResource(
97 gauge.getKey(), gauge.getValue()));
98 }
99 result.setGauges(gauges);
100
101 for (final Map.Entry<String, Counter> counter :
Ray Milkey128651a2014-07-14 11:24:28 -0700102 OnosMetrics.getCounters(filter).entrySet()) {
Ray Milkey26921af2014-06-30 16:27:40 -0700103 counters.add(new MetricsObjectResource.CounterObjectResource(
104 counter.getKey(), counter.getValue()));
105 }
106 result.setCounters(counters);
107
108 for (final Map.Entry<String, Meter> meter :
Ray Milkey128651a2014-07-14 11:24:28 -0700109 OnosMetrics.getMeters(filter).entrySet()) {
Ray Milkey26921af2014-06-30 16:27:40 -0700110 meters.add(new MetricsObjectResource.MeterObjectResource(
111 meter.getKey(), meter.getValue()));
112 }
113 result.setMeters(meters);
114
115 for (final Map.Entry<String, Histogram> histogram :
Ray Milkey128651a2014-07-14 11:24:28 -0700116 OnosMetrics.getHistograms(filter).entrySet()) {
Ray Milkey26921af2014-06-30 16:27:40 -0700117 histograms.add(new MetricsObjectResource.HistogramObjectResource(
118 histogram.getKey(), histogram.getValue()));
119 }
120 result.setHistograms(histograms);
121
122 return toRepresentation(result, null);
123 }
124
125}