Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.metrics; |
| 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 | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 10 | import org.restlet.representation.Representation; |
| 11 | import org.restlet.resource.Get; |
| 12 | import org.restlet.resource.ServerResource; |
| 13 | |
| 14 | import java.util.ArrayList; |
Ray Milkey | 14c4651 | 2014-07-08 18:30:31 -0700 | [diff] [blame] | 15 | import java.util.Arrays; |
| 16 | import java.util.HashSet; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 17 | import java.util.List; |
| 18 | import java.util.Map; |
| 19 | |
| 20 | /** |
| 21 | * REST APIs for Metrics objects. |
| 22 | */ |
| 23 | public class MetricsResource extends ServerResource { |
| 24 | |
| 25 | /** |
Ray Milkey | 14c4651 | 2014-07-08 18:30:31 -0700 | [diff] [blame] | 26 | * 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 Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 62 | * 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 Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame^] | 68 | public Representation retrieve() { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 69 | 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 Milkey | 14c4651 | 2014-07-08 18:30:31 -0700 | [diff] [blame] | 82 | final String metricIdsString = getQuery().getValues("ids"); |
| 83 | |
| 84 | final MetricFilter filter = new MetricNameFilter(metricIdsString); |
| 85 | |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 86 | for (final Map.Entry<String, Timer> timer : |
Ray Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame^] | 87 | OnosMetrics.getTimers(filter).entrySet()) { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 88 | 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 Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame^] | 94 | OnosMetrics.getGauges(filter).entrySet()) { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 95 | 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 Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame^] | 101 | OnosMetrics.getCounters(filter).entrySet()) { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 102 | 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 Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame^] | 108 | OnosMetrics.getMeters(filter).entrySet()) { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 109 | 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 Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame^] | 115 | OnosMetrics.getHistograms(filter).entrySet()) { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 116 | histograms.add(new MetricsObjectResource.HistogramObjectResource( |
| 117 | histogram.getKey(), histogram.getValue())); |
| 118 | } |
| 119 | result.setHistograms(histograms); |
| 120 | |
| 121 | return toRepresentation(result, null); |
| 122 | } |
| 123 | |
| 124 | } |