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.MetricRegistry; |
| 10 | import com.codahale.metrics.Timer; |
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; |
| 20 | |
| 21 | /** |
| 22 | * REST APIs for Metrics objects. |
| 23 | */ |
| 24 | public class MetricsResource extends ServerResource { |
| 25 | |
| 26 | /** |
Ray Milkey | 14c4651 | 2014-07-08 18:30:31 -0700 | [diff] [blame^] | 27 | * 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 Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 63 | * 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 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 | 14c4651 | 2014-07-08 18:30:31 -0700 | [diff] [blame^] | 89 | registry.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 | 14c4651 | 2014-07-08 18:30:31 -0700 | [diff] [blame^] | 96 | registry.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 | 14c4651 | 2014-07-08 18:30:31 -0700 | [diff] [blame^] | 103 | registry.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 | 14c4651 | 2014-07-08 18:30:31 -0700 | [diff] [blame^] | 110 | registry.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 | 14c4651 | 2014-07-08 18:30:31 -0700 | [diff] [blame^] | 117 | registry.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 | } |