Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 17 | package org.onosproject.cli; |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 18 | |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 19 | import com.codahale.metrics.Counter; |
| 20 | import com.codahale.metrics.Gauge; |
| 21 | import com.codahale.metrics.Histogram; |
| 22 | import com.codahale.metrics.Meter; |
| 23 | import com.codahale.metrics.Metric; |
| 24 | import com.codahale.metrics.MetricFilter; |
| 25 | import com.codahale.metrics.Snapshot; |
| 26 | import com.codahale.metrics.Timer; |
Jian Li | fb16e99 | 2016-02-22 19:03:50 +0900 | [diff] [blame] | 27 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 28 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 29 | import com.google.common.base.Strings; |
| 30 | import com.google.common.collect.Ordering; |
| 31 | import com.google.common.collect.TreeMultimap; |
Flavio Castro | 6baf95d | 2015-08-11 14:48:49 -0700 | [diff] [blame] | 32 | import org.apache.karaf.shell.commands.Argument; |
| 33 | import org.apache.karaf.shell.commands.Command; |
| 34 | import org.joda.time.LocalDateTime; |
| 35 | import org.onlab.metrics.MetricsService; |
| 36 | |
| 37 | import java.util.Comparator; |
| 38 | import java.util.Map; |
| 39 | import java.util.Map.Entry; |
| 40 | |
| 41 | import static java.lang.String.format; |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Prints metrics in the system. |
| 45 | */ |
| 46 | @Command(scope = "onos", name = "metrics", |
| 47 | description = "Prints metrics in the system") |
| 48 | public class MetricsListCommand extends AbstractShellCommand { |
| 49 | |
Jian Li | fb16e99 | 2016-02-22 19:03:50 +0900 | [diff] [blame] | 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 | |
Flavio Castro | 4b51941 | 2015-07-24 12:57:59 -0700 | [diff] [blame] | 69 | @Argument(index = 0, name = "metricName", description = "Name of Metric", |
| 70 | required = false, multiValued = false) |
| 71 | String metricName = null; |
| 72 | |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 73 | @Override |
| 74 | protected void execute() { |
| 75 | MetricsService metricsService = get(MetricsService.class); |
| 76 | |
Flavio Castro | 4b51941 | 2015-07-24 12:57:59 -0700 | [diff] [blame] | 77 | MetricFilter filter = metricName != null ? (name, metric) -> name.equals(metricName) : MetricFilter.ALL; |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 78 | |
| 79 | TreeMultimap<String, Metric> matched = listMetrics(metricsService, filter); |
| 80 | matched.asMap().forEach((name, metrics) -> { |
Jian Li | fb16e99 | 2016-02-22 19:03:50 +0900 | [diff] [blame] | 81 | if (outputJson()) { |
| 82 | metrics.forEach(metric -> print("%s", json(metric))); |
| 83 | } else { |
| 84 | metrics.forEach(metric -> printMetric(name, metric)); |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 85 | } |
| 86 | }); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Print metric object. |
| 91 | * |
| 92 | * @param name metric name |
| 93 | * @param metric metric object |
| 94 | */ |
| 95 | private void printMetric(String name, Metric metric) { |
| 96 | final String heading; |
| 97 | |
| 98 | if (metric instanceof Counter) { |
| 99 | heading = format("-- %s : [%s] --", name, "Counter"); |
| 100 | print(heading); |
| 101 | Counter counter = (Counter) metric; |
| 102 | print(" count = %d", counter.getCount()); |
| 103 | |
| 104 | } else if (metric instanceof Gauge) { |
| 105 | heading = format("-- %s : [%s] --", name, "Gauge"); |
| 106 | print(heading); |
| 107 | @SuppressWarnings("rawtypes") |
| 108 | Gauge gauge = (Gauge) metric; |
| 109 | final Object value = gauge.getValue(); |
| 110 | if (name.endsWith("EpochMs") && value instanceof Long) { |
| 111 | print(" value = %s (%s)", value, new LocalDateTime(value)); |
| 112 | } else { |
| 113 | print(" value = %s", value); |
| 114 | } |
| 115 | |
| 116 | } else if (metric instanceof Histogram) { |
| 117 | heading = format("-- %s : [%s] --", name, "Histogram"); |
| 118 | print(heading); |
| 119 | final Histogram histogram = (Histogram) metric; |
| 120 | final Snapshot snapshot = histogram.getSnapshot(); |
| 121 | print(" count = %d", histogram.getCount()); |
| 122 | print(" min = %d", snapshot.getMin()); |
| 123 | print(" max = %d", snapshot.getMax()); |
| 124 | print(" mean = %f", snapshot.getMean()); |
| 125 | print(" stddev = %f", snapshot.getStdDev()); |
| 126 | |
| 127 | } else if (metric instanceof Meter) { |
| 128 | heading = format("-- %s : [%s] --", name, "Meter"); |
| 129 | print(heading); |
| 130 | final Meter meter = (Meter) metric; |
| 131 | print(" count = %d", meter.getCount()); |
| 132 | print(" mean rate = %f", meter.getMeanRate()); |
| 133 | print(" 1-minute rate = %f", meter.getOneMinuteRate()); |
| 134 | print(" 5-minute rate = %f", meter.getFiveMinuteRate()); |
| 135 | print(" 15-minute rate = %f", meter.getFifteenMinuteRate()); |
| 136 | |
| 137 | } else if (metric instanceof Timer) { |
| 138 | heading = format("-- %s : [%s] --", name, "Timer"); |
| 139 | print(heading); |
| 140 | final Timer timer = (Timer) metric; |
| 141 | final Snapshot snapshot = timer.getSnapshot(); |
| 142 | print(" count = %d", timer.getCount()); |
Flavio Castro | 6baf95d | 2015-08-11 14:48:49 -0700 | [diff] [blame] | 143 | print(" mean rate = %f per second", timer.getMeanRate()); |
| 144 | print(" 1-minute rate = %f per second", timer.getOneMinuteRate()); |
| 145 | print(" 5-minute rate = %f per second", timer.getFiveMinuteRate()); |
| 146 | print(" 15-minute rate = %f per second", timer.getFifteenMinuteRate()); |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 147 | print(" min = %f ms", nanoToMs(snapshot.getMin())); |
| 148 | print(" max = %f ms", nanoToMs(snapshot.getMax())); |
| 149 | print(" mean = %f ms", nanoToMs(snapshot.getMean())); |
| 150 | print(" stddev = %f ms", nanoToMs(snapshot.getStdDev())); |
| 151 | } else { |
| 152 | heading = format("-- %s : [%s] --", name, metric.getClass().getCanonicalName()); |
| 153 | print(heading); |
| 154 | print("Unknown Metric type:{}", metric.getClass().getCanonicalName()); |
| 155 | } |
| 156 | print(Strings.repeat("-", heading.length())); |
| 157 | } |
| 158 | |
| 159 | @SuppressWarnings("rawtypes") |
| 160 | private TreeMultimap<String, Metric> listMetrics(MetricsService metricsService, MetricFilter filter) { |
| 161 | TreeMultimap<String, Metric> metrics = TreeMultimap.create(Comparator.naturalOrder(), Ordering.arbitrary()); |
| 162 | |
| 163 | Map<String, Counter> counters = metricsService.getCounters(filter); |
| 164 | for (Entry<String, Counter> entry : counters.entrySet()) { |
| 165 | metrics.put(entry.getKey(), entry.getValue()); |
| 166 | } |
| 167 | Map<String, Gauge> gauges = metricsService.getGauges(filter); |
| 168 | for (Entry<String, Gauge> entry : gauges.entrySet()) { |
| 169 | metrics.put(entry.getKey(), entry.getValue()); |
| 170 | } |
| 171 | Map<String, Histogram> histograms = metricsService.getHistograms(filter); |
| 172 | for (Entry<String, Histogram> entry : histograms.entrySet()) { |
| 173 | metrics.put(entry.getKey(), entry.getValue()); |
| 174 | } |
| 175 | Map<String, Meter> meters = metricsService.getMeters(filter); |
| 176 | for (Entry<String, Meter> entry : meters.entrySet()) { |
| 177 | metrics.put(entry.getKey(), entry.getValue()); |
| 178 | } |
| 179 | Map<String, Timer> timers = metricsService.getTimers(filter); |
| 180 | for (Entry<String, Timer> entry : timers.entrySet()) { |
| 181 | metrics.put(entry.getKey(), entry.getValue()); |
| 182 | } |
| 183 | |
| 184 | return metrics; |
| 185 | } |
| 186 | |
Jian Li | fb16e99 | 2016-02-22 19:03:50 +0900 | [diff] [blame] | 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 | |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 233 | private double nanoToMs(double nano) { |
| 234 | return nano / 1_000_000D; |
| 235 | } |
| 236 | } |