Ray Milkey | a34ed04 | 2014-07-17 16:19:52 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.metrics.web; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 2 | |
Ray Milkey | 14c4651 | 2014-07-08 18:30:31 -0700 | [diff] [blame] | 3 | import com.codahale.metrics.Counter; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 4 | import com.codahale.metrics.Gauge; |
Ray Milkey | 14c4651 | 2014-07-08 18:30:31 -0700 | [diff] [blame] | 5 | import com.codahale.metrics.Histogram; |
| 6 | import com.codahale.metrics.Meter; |
| 7 | import com.codahale.metrics.Metric; |
| 8 | import com.codahale.metrics.MetricFilter; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 9 | import com.codahale.metrics.Timer; |
Ray Milkey | a34ed04 | 2014-07-17 16:19:52 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.metrics.OnosMetrics; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 11 | import org.restlet.representation.Representation; |
| 12 | import org.restlet.resource.Get; |
| 13 | import org.restlet.resource.ServerResource; |
| 14 | |
| 15 | import java.util.ArrayList; |
Ray Milkey | 14c4651 | 2014-07-08 18:30:31 -0700 | [diff] [blame] | 16 | import java.util.Arrays; |
| 17 | import java.util.HashSet; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 18 | import java.util.List; |
| 19 | import java.util.Map; |
Ray Milkey | 40eb9c8 | 2014-07-18 10:28:11 -0700 | [diff] [blame] | 20 | import java.util.Set; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * REST APIs for Metrics objects. |
| 24 | */ |
| 25 | public class MetricsResource extends ServerResource { |
| 26 | |
| 27 | /** |
Ray Milkey | 14c4651 | 2014-07-08 18:30:31 -0700 | [diff] [blame] | 28 | * Metric filter to allow selecting metrics by name. |
| 29 | */ |
| 30 | private static class MetricNameFilter implements MetricFilter { |
Ray Milkey | 40eb9c8 | 2014-07-18 10:28:11 -0700 | [diff] [blame] | 31 | private final Set<String> names; |
Ray Milkey | 14c4651 | 2014-07-08 18:30:31 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Hide default constructor. |
| 35 | */ |
| 36 | @SuppressWarnings("unused") |
| 37 | private MetricNameFilter() { |
| 38 | names = null; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Initializes a filter for the given name list. |
| 43 | * |
| 44 | * @param nameListString comma separated list of strings of the |
| 45 | * names of metrics to query |
| 46 | */ |
| 47 | public MetricNameFilter(final String nameListString) { |
| 48 | |
| 49 | if (nameListString == null) { |
| 50 | names = null; |
| 51 | } else { |
| 52 | List<String> nameList = Arrays.asList(nameListString.split(",")); |
| 53 | names = new HashSet<>(); |
| 54 | names.addAll(nameList); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public boolean matches(String s, Metric metric) { |
| 60 | return names == null || names.contains(s); |
| 61 | } |
| 62 | } |
| 63 | /** |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 64 | * REST API to get all of the system's metrics. |
| 65 | * |
| 66 | * @return a Representation object containing the metrics |
| 67 | */ |
| 68 | @Get("json") |
| 69 | @SuppressWarnings("rawtypes") |
Ray Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame] | 70 | public Representation retrieve() { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 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 Milkey | 14c4651 | 2014-07-08 18:30:31 -0700 | [diff] [blame] | 84 | final String metricIdsString = getQuery().getValues("ids"); |
| 85 | |
| 86 | final MetricFilter filter = new MetricNameFilter(metricIdsString); |
| 87 | |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 88 | for (final Map.Entry<String, Timer> timer : |
Ray Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame] | 89 | OnosMetrics.getTimers(filter).entrySet()) { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 90 | 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 Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame] | 96 | OnosMetrics.getGauges(filter).entrySet()) { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 97 | 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 Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame] | 103 | OnosMetrics.getCounters(filter).entrySet()) { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 104 | 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 Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame] | 110 | OnosMetrics.getMeters(filter).entrySet()) { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 111 | 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 Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame] | 117 | OnosMetrics.getHistograms(filter).entrySet()) { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 118 | histograms.add(new MetricsObjectResource.HistogramObjectResource( |
| 119 | histogram.getKey(), histogram.getValue())); |
| 120 | } |
| 121 | result.setHistograms(histograms); |
| 122 | |
| 123 | return toRepresentation(result, null); |
| 124 | } |
| 125 | |
| 126 | } |