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