blob: a2a99f987808fa87aa510158df2d97d2e5016645 [file] [log] [blame]
Yuta HIGUCHI6a462832014-11-23 23:56:03 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Yuta HIGUCHI6a462832014-11-23 23:56:03 -08003 *
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'Connorabafb502014-12-02 22:26:20 -080017package org.onosproject.cli;
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080018
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080019import com.codahale.metrics.Counter;
20import com.codahale.metrics.Gauge;
21import com.codahale.metrics.Histogram;
22import com.codahale.metrics.Meter;
23import com.codahale.metrics.Metric;
24import com.codahale.metrics.MetricFilter;
25import com.codahale.metrics.Snapshot;
26import com.codahale.metrics.Timer;
Jian Lifb16e992016-02-22 19:03:50 +090027import com.fasterxml.jackson.databind.ObjectMapper;
28import com.fasterxml.jackson.databind.node.ObjectNode;
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080029import com.google.common.base.Strings;
30import com.google.common.collect.Ordering;
31import com.google.common.collect.TreeMultimap;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070032import org.apache.karaf.shell.api.action.Argument;
33import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070034import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070035import org.apache.karaf.shell.api.action.lifecycle.Service;
Flavio Castro6baf95d2015-08-11 14:48:49 -070036import org.onlab.metrics.MetricsService;
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070037import org.onlab.util.Tools;
Flavio Castro6baf95d2015-08-11 14:48:49 -070038
39import java.util.Comparator;
40import java.util.Map;
41import java.util.Map.Entry;
42
43import static java.lang.String.format;
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080044
45/**
46 * Prints metrics in the system.
47 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070048@Service
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080049@Command(scope = "onos", name = "metrics",
50 description = "Prints metrics in the system")
51public class MetricsListCommand extends AbstractShellCommand {
52
Jian Lifb16e992016-02-22 19:03:50 +090053 private static final String COUNTER = "counter";
54
55 private static final String GAUGE = "gauge";
56 private static final String VALUE = "value";
57
58 private static final String METER = "meter";
59 private static final String MEAN_RATE = "mean_rate";
60 private static final String ONE_MIN_RATE = "1_min_rate";
61 private static final String FIVE_MIN_RATE = "5_min_rate";
62 private static final String FIFT_MIN_RATE = "15_min_rate";
63
64 private static final String HISTOGRAM = "histogram";
65 private static final String MIN = "min";
66 private static final String MAX = "max";
67 private static final String MEAN = "mean";
68 private static final String STDDEV = "stddev";
69
70 private static final String TIMER = "timer";
71
Flavio Castro4b519412015-07-24 12:57:59 -070072 @Argument(index = 0, name = "metricName", description = "Name of Metric",
73 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070074 @Completion(MetricNameCompleter.class)
Flavio Castro4b519412015-07-24 12:57:59 -070075 String metricName = null;
76
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080077 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070078 protected void doExecute() {
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080079 MetricsService metricsService = get(MetricsService.class);
80
Flavio Castro4b519412015-07-24 12:57:59 -070081 MetricFilter filter = metricName != null ? (name, metric) -> name.equals(metricName) : MetricFilter.ALL;
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080082
83 TreeMultimap<String, Metric> matched = listMetrics(metricsService, filter);
84 matched.asMap().forEach((name, metrics) -> {
Jian Lifb16e992016-02-22 19:03:50 +090085 if (outputJson()) {
86 metrics.forEach(metric -> print("%s", json(metric)));
87 } else {
88 metrics.forEach(metric -> printMetric(name, metric));
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080089 }
90 });
91 }
92
93 /**
94 * Print metric object.
95 *
96 * @param name metric name
97 * @param metric metric object
98 */
99 private void printMetric(String name, Metric metric) {
100 final String heading;
101
102 if (metric instanceof Counter) {
103 heading = format("-- %s : [%s] --", name, "Counter");
104 print(heading);
105 Counter counter = (Counter) metric;
106 print(" count = %d", counter.getCount());
107
108 } else if (metric instanceof Gauge) {
109 heading = format("-- %s : [%s] --", name, "Gauge");
110 print(heading);
111 @SuppressWarnings("rawtypes")
112 Gauge gauge = (Gauge) metric;
113 final Object value = gauge.getValue();
114 if (name.endsWith("EpochMs") && value instanceof Long) {
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700115 print(" value = %s (%s)", value, Tools.defaultOffsetDataTime((Long) value));
Yuta HIGUCHI6a462832014-11-23 23:56:03 -0800116 } else {
117 print(" value = %s", value);
118 }
119
120 } else if (metric instanceof Histogram) {
121 heading = format("-- %s : [%s] --", name, "Histogram");
122 print(heading);
123 final Histogram histogram = (Histogram) metric;
124 final Snapshot snapshot = histogram.getSnapshot();
125 print(" count = %d", histogram.getCount());
126 print(" min = %d", snapshot.getMin());
127 print(" max = %d", snapshot.getMax());
128 print(" mean = %f", snapshot.getMean());
129 print(" stddev = %f", snapshot.getStdDev());
130
131 } else if (metric instanceof Meter) {
132 heading = format("-- %s : [%s] --", name, "Meter");
133 print(heading);
134 final Meter meter = (Meter) metric;
135 print(" count = %d", meter.getCount());
136 print(" mean rate = %f", meter.getMeanRate());
137 print(" 1-minute rate = %f", meter.getOneMinuteRate());
138 print(" 5-minute rate = %f", meter.getFiveMinuteRate());
139 print(" 15-minute rate = %f", meter.getFifteenMinuteRate());
140
141 } else if (metric instanceof Timer) {
142 heading = format("-- %s : [%s] --", name, "Timer");
143 print(heading);
144 final Timer timer = (Timer) metric;
145 final Snapshot snapshot = timer.getSnapshot();
146 print(" count = %d", timer.getCount());
Flavio Castro6baf95d2015-08-11 14:48:49 -0700147 print(" mean rate = %f per second", timer.getMeanRate());
148 print(" 1-minute rate = %f per second", timer.getOneMinuteRate());
149 print(" 5-minute rate = %f per second", timer.getFiveMinuteRate());
150 print(" 15-minute rate = %f per second", timer.getFifteenMinuteRate());
Yuta HIGUCHI6a462832014-11-23 23:56:03 -0800151 print(" min = %f ms", nanoToMs(snapshot.getMin()));
152 print(" max = %f ms", nanoToMs(snapshot.getMax()));
153 print(" mean = %f ms", nanoToMs(snapshot.getMean()));
154 print(" stddev = %f ms", nanoToMs(snapshot.getStdDev()));
155 } else {
156 heading = format("-- %s : [%s] --", name, metric.getClass().getCanonicalName());
157 print(heading);
158 print("Unknown Metric type:{}", metric.getClass().getCanonicalName());
159 }
160 print(Strings.repeat("-", heading.length()));
161 }
162
163 @SuppressWarnings("rawtypes")
164 private TreeMultimap<String, Metric> listMetrics(MetricsService metricsService, MetricFilter filter) {
165 TreeMultimap<String, Metric> metrics = TreeMultimap.create(Comparator.naturalOrder(), Ordering.arbitrary());
166
167 Map<String, Counter> counters = metricsService.getCounters(filter);
168 for (Entry<String, Counter> entry : counters.entrySet()) {
169 metrics.put(entry.getKey(), entry.getValue());
170 }
171 Map<String, Gauge> gauges = metricsService.getGauges(filter);
172 for (Entry<String, Gauge> entry : gauges.entrySet()) {
173 metrics.put(entry.getKey(), entry.getValue());
174 }
175 Map<String, Histogram> histograms = metricsService.getHistograms(filter);
176 for (Entry<String, Histogram> entry : histograms.entrySet()) {
177 metrics.put(entry.getKey(), entry.getValue());
178 }
179 Map<String, Meter> meters = metricsService.getMeters(filter);
180 for (Entry<String, Meter> entry : meters.entrySet()) {
181 metrics.put(entry.getKey(), entry.getValue());
182 }
183 Map<String, Timer> timers = metricsService.getTimers(filter);
184 for (Entry<String, Timer> entry : timers.entrySet()) {
185 metrics.put(entry.getKey(), entry.getValue());
186 }
187
188 return metrics;
189 }
190
Jian Lifb16e992016-02-22 19:03:50 +0900191 /**
192 * Creates a json object for a certain metric.
193 *
194 * @param metric metric object
195 * @return json object
196 */
197 private ObjectNode json(Metric metric) {
198 ObjectMapper mapper = new ObjectMapper();
199 ObjectNode objectNode = mapper.createObjectNode();
200 ObjectNode dataNode = mapper.createObjectNode();
201
202 if (metric instanceof Counter) {
203 dataNode.put(COUNTER, ((Counter) metric).getCount());
204 objectNode.set(COUNTER, dataNode);
205 } else if (metric instanceof Gauge) {
206 objectNode.put(VALUE, ((Gauge) metric).getValue().toString());
207 objectNode.set(GAUGE, dataNode);
208 } else if (metric instanceof Meter) {
209 dataNode.put(COUNTER, ((Meter) metric).getCount());
210 dataNode.put(MEAN_RATE, ((Meter) metric).getMeanRate());
211 dataNode.put(ONE_MIN_RATE, ((Meter) metric).getOneMinuteRate());
212 dataNode.put(FIVE_MIN_RATE, ((Meter) metric).getFiveMinuteRate());
213 dataNode.put(FIFT_MIN_RATE, ((Meter) metric).getFifteenMinuteRate());
214 objectNode.set(METER, dataNode);
215 } else if (metric instanceof Histogram) {
216 dataNode.put(COUNTER, ((Histogram) metric).getCount());
217 dataNode.put(MEAN, ((Histogram) metric).getSnapshot().getMean());
218 dataNode.put(MIN, ((Histogram) metric).getSnapshot().getMin());
219 dataNode.put(MAX, ((Histogram) metric).getSnapshot().getMax());
220 dataNode.put(STDDEV, ((Histogram) metric).getSnapshot().getStdDev());
221 objectNode.set(HISTOGRAM, dataNode);
222 } else if (metric instanceof Timer) {
223 dataNode.put(COUNTER, ((Timer) metric).getCount());
224 dataNode.put(MEAN_RATE, ((Timer) metric).getMeanRate());
225 dataNode.put(ONE_MIN_RATE, ((Timer) metric).getOneMinuteRate());
226 dataNode.put(FIVE_MIN_RATE, ((Timer) metric).getFiveMinuteRate());
227 dataNode.put(FIFT_MIN_RATE, ((Timer) metric).getFifteenMinuteRate());
228 dataNode.put(MEAN, nanoToMs(((Timer) metric).getSnapshot().getMean()));
229 dataNode.put(MIN, nanoToMs(((Timer) metric).getSnapshot().getMin()));
230 dataNode.put(MAX, nanoToMs(((Timer) metric).getSnapshot().getMax()));
231 dataNode.put(STDDEV, nanoToMs(((Timer) metric).getSnapshot().getStdDev()));
232 objectNode.set(TIMER, dataNode);
233 }
234 return objectNode;
235 }
236
Yuta HIGUCHI6a462832014-11-23 23:56:03 -0800237 private double nanoToMs(double nano) {
238 return nano / 1_000_000D;
239 }
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700240
241
Yuta HIGUCHI6a462832014-11-23 23:56:03 -0800242}