blob: 40feaaac723d0d763571b7f31389cef3f8983b30 [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.MetricRegistry;
10import com.codahale.metrics.Timer;
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")
69 public Representation retrieve() throws Exception {
70 final MetricRegistry registry = OnosMetrics.getMetricsRegistry();
71 final MetricsObjectResource result = new MetricsObjectResource();
72
73 final List<MetricsObjectResource.TimerObjectResource> timers =
74 new ArrayList<>();
75 final List<MetricsObjectResource.GaugeObjectResource> gauges =
76 new ArrayList<>();
77 final List<MetricsObjectResource.CounterObjectResource> counters =
78 new ArrayList<>();
79 final List<MetricsObjectResource.MeterObjectResource> meters =
80 new ArrayList<>();
81 final List<MetricsObjectResource.HistogramObjectResource> histograms =
82 new ArrayList<>();
83
Ray Milkey14c46512014-07-08 18:30:31 -070084 final String metricIdsString = getQuery().getValues("ids");
85
86 final MetricFilter filter = new MetricNameFilter(metricIdsString);
87
Ray Milkey26921af2014-06-30 16:27:40 -070088 for (final Map.Entry<String, Timer> timer :
Ray Milkey14c46512014-07-08 18:30:31 -070089 registry.getTimers(filter).entrySet()) {
Ray Milkey26921af2014-06-30 16:27:40 -070090 timers.add(new MetricsObjectResource.TimerObjectResource(
91 timer.getKey(), timer.getValue()));
92 }
93 result.setTimers(timers);
94
95 for (final Map.Entry<String, Gauge> gauge :
Ray Milkey14c46512014-07-08 18:30:31 -070096 registry.getGauges(filter).entrySet()) {
Ray Milkey26921af2014-06-30 16:27:40 -070097 gauges.add(new MetricsObjectResource.GaugeObjectResource(
98 gauge.getKey(), gauge.getValue()));
99 }
100 result.setGauges(gauges);
101
102 for (final Map.Entry<String, Counter> counter :
Ray Milkey14c46512014-07-08 18:30:31 -0700103 registry.getCounters(filter).entrySet()) {
Ray Milkey26921af2014-06-30 16:27:40 -0700104 counters.add(new MetricsObjectResource.CounterObjectResource(
105 counter.getKey(), counter.getValue()));
106 }
107 result.setCounters(counters);
108
109 for (final Map.Entry<String, Meter> meter :
Ray Milkey14c46512014-07-08 18:30:31 -0700110 registry.getMeters(filter).entrySet()) {
Ray Milkey26921af2014-06-30 16:27:40 -0700111 meters.add(new MetricsObjectResource.MeterObjectResource(
112 meter.getKey(), meter.getValue()));
113 }
114 result.setMeters(meters);
115
116 for (final Map.Entry<String, Histogram> histogram :
Ray Milkey14c46512014-07-08 18:30:31 -0700117 registry.getHistograms(filter).entrySet()) {
Ray Milkey26921af2014-06-30 16:27:40 -0700118 histograms.add(new MetricsObjectResource.HistogramObjectResource(
119 histogram.getKey(), histogram.getValue()));
120 }
121 result.setHistograms(histograms);
122
123 return toRepresentation(result, null);
124 }
125
126}