Committed by
Gerrit Code Review
[ONOS-401] Support JSON output parameter for MetricsListCommand
Change-Id: Id327a4d406dc924ec53dc54bb9939654759d947a
Showing
1 changed file
with
71 additions
and
2 deletions
| ... | @@ -24,6 +24,8 @@ import com.codahale.metrics.Metric; | ... | @@ -24,6 +24,8 @@ import com.codahale.metrics.Metric; |
| 24 | import com.codahale.metrics.MetricFilter; | 24 | import com.codahale.metrics.MetricFilter; |
| 25 | import com.codahale.metrics.Snapshot; | 25 | import com.codahale.metrics.Snapshot; |
| 26 | import com.codahale.metrics.Timer; | 26 | import com.codahale.metrics.Timer; |
| 27 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 28 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 27 | import com.google.common.base.Strings; | 29 | import com.google.common.base.Strings; |
| 28 | import com.google.common.collect.Ordering; | 30 | import com.google.common.collect.Ordering; |
| 29 | import com.google.common.collect.TreeMultimap; | 31 | import com.google.common.collect.TreeMultimap; |
| ... | @@ -45,6 +47,25 @@ import static java.lang.String.format; | ... | @@ -45,6 +47,25 @@ import static java.lang.String.format; |
| 45 | description = "Prints metrics in the system") | 47 | description = "Prints metrics in the system") |
| 46 | public class MetricsListCommand extends AbstractShellCommand { | 48 | public class MetricsListCommand extends AbstractShellCommand { |
| 47 | 49 | ||
| 50 | + private static final String COUNTER = "counter"; | ||
| 51 | + | ||
| 52 | + private static final String GAUGE = "gauge"; | ||
| 53 | + private static final String VALUE = "value"; | ||
| 54 | + | ||
| 55 | + private static final String METER = "meter"; | ||
| 56 | + private static final String MEAN_RATE = "mean_rate"; | ||
| 57 | + private static final String ONE_MIN_RATE = "1_min_rate"; | ||
| 58 | + private static final String FIVE_MIN_RATE = "5_min_rate"; | ||
| 59 | + private static final String FIFT_MIN_RATE = "15_min_rate"; | ||
| 60 | + | ||
| 61 | + private static final String HISTOGRAM = "histogram"; | ||
| 62 | + private static final String MIN = "min"; | ||
| 63 | + private static final String MAX = "max"; | ||
| 64 | + private static final String MEAN = "mean"; | ||
| 65 | + private static final String STDDEV = "stddev"; | ||
| 66 | + | ||
| 67 | + private static final String TIMER = "timer"; | ||
| 68 | + | ||
| 48 | @Argument(index = 0, name = "metricName", description = "Name of Metric", | 69 | @Argument(index = 0, name = "metricName", description = "Name of Metric", |
| 49 | required = false, multiValued = false) | 70 | required = false, multiValued = false) |
| 50 | String metricName = null; | 71 | String metricName = null; |
| ... | @@ -57,8 +78,10 @@ public class MetricsListCommand extends AbstractShellCommand { | ... | @@ -57,8 +78,10 @@ public class MetricsListCommand extends AbstractShellCommand { |
| 57 | 78 | ||
| 58 | TreeMultimap<String, Metric> matched = listMetrics(metricsService, filter); | 79 | TreeMultimap<String, Metric> matched = listMetrics(metricsService, filter); |
| 59 | matched.asMap().forEach((name, metrics) -> { | 80 | matched.asMap().forEach((name, metrics) -> { |
| 60 | - for (Metric metric : metrics) { | 81 | + if (outputJson()) { |
| 61 | - printMetric(name, metric); | 82 | + metrics.forEach(metric -> print("%s", json(metric))); |
| 83 | + } else { | ||
| 84 | + metrics.forEach(metric -> printMetric(name, metric)); | ||
| 62 | } | 85 | } |
| 63 | }); | 86 | }); |
| 64 | } | 87 | } |
| ... | @@ -161,6 +184,52 @@ public class MetricsListCommand extends AbstractShellCommand { | ... | @@ -161,6 +184,52 @@ public class MetricsListCommand extends AbstractShellCommand { |
| 161 | return metrics; | 184 | return metrics; |
| 162 | } | 185 | } |
| 163 | 186 | ||
| 187 | + /** | ||
| 188 | + * Creates a json object for a certain metric. | ||
| 189 | + * | ||
| 190 | + * @param metric metric object | ||
| 191 | + * @return json object | ||
| 192 | + */ | ||
| 193 | + private ObjectNode json(Metric metric) { | ||
| 194 | + ObjectMapper mapper = new ObjectMapper(); | ||
| 195 | + ObjectNode objectNode = mapper.createObjectNode(); | ||
| 196 | + ObjectNode dataNode = mapper.createObjectNode(); | ||
| 197 | + | ||
| 198 | + if (metric instanceof Counter) { | ||
| 199 | + dataNode.put(COUNTER, ((Counter) metric).getCount()); | ||
| 200 | + objectNode.set(COUNTER, dataNode); | ||
| 201 | + } else if (metric instanceof Gauge) { | ||
| 202 | + objectNode.put(VALUE, ((Gauge) metric).getValue().toString()); | ||
| 203 | + objectNode.set(GAUGE, dataNode); | ||
| 204 | + } else if (metric instanceof Meter) { | ||
| 205 | + dataNode.put(COUNTER, ((Meter) metric).getCount()); | ||
| 206 | + dataNode.put(MEAN_RATE, ((Meter) metric).getMeanRate()); | ||
| 207 | + dataNode.put(ONE_MIN_RATE, ((Meter) metric).getOneMinuteRate()); | ||
| 208 | + dataNode.put(FIVE_MIN_RATE, ((Meter) metric).getFiveMinuteRate()); | ||
| 209 | + dataNode.put(FIFT_MIN_RATE, ((Meter) metric).getFifteenMinuteRate()); | ||
| 210 | + objectNode.set(METER, dataNode); | ||
| 211 | + } else if (metric instanceof Histogram) { | ||
| 212 | + dataNode.put(COUNTER, ((Histogram) metric).getCount()); | ||
| 213 | + dataNode.put(MEAN, ((Histogram) metric).getSnapshot().getMean()); | ||
| 214 | + dataNode.put(MIN, ((Histogram) metric).getSnapshot().getMin()); | ||
| 215 | + dataNode.put(MAX, ((Histogram) metric).getSnapshot().getMax()); | ||
| 216 | + dataNode.put(STDDEV, ((Histogram) metric).getSnapshot().getStdDev()); | ||
| 217 | + objectNode.set(HISTOGRAM, dataNode); | ||
| 218 | + } else if (metric instanceof Timer) { | ||
| 219 | + dataNode.put(COUNTER, ((Timer) metric).getCount()); | ||
| 220 | + dataNode.put(MEAN_RATE, ((Timer) metric).getMeanRate()); | ||
| 221 | + dataNode.put(ONE_MIN_RATE, ((Timer) metric).getOneMinuteRate()); | ||
| 222 | + dataNode.put(FIVE_MIN_RATE, ((Timer) metric).getFiveMinuteRate()); | ||
| 223 | + dataNode.put(FIFT_MIN_RATE, ((Timer) metric).getFifteenMinuteRate()); | ||
| 224 | + dataNode.put(MEAN, nanoToMs(((Timer) metric).getSnapshot().getMean())); | ||
| 225 | + dataNode.put(MIN, nanoToMs(((Timer) metric).getSnapshot().getMin())); | ||
| 226 | + dataNode.put(MAX, nanoToMs(((Timer) metric).getSnapshot().getMax())); | ||
| 227 | + dataNode.put(STDDEV, nanoToMs(((Timer) metric).getSnapshot().getStdDev())); | ||
| 228 | + objectNode.set(TIMER, dataNode); | ||
| 229 | + } | ||
| 230 | + return objectNode; | ||
| 231 | + } | ||
| 232 | + | ||
| 164 | private double nanoToMs(double nano) { | 233 | private double nanoToMs(double nano) { |
| 165 | return nano / 1_000_000D; | 234 | return nano / 1_000_000D; |
| 166 | } | 235 | } | ... | ... |
-
Please register or login to post a comment