blob: 3cd951551e4826586ac3028610d3aa7b556b1e86 [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;
34import org.apache.karaf.shell.api.action.lifecycle.Service;
Flavio Castro6baf95d2015-08-11 14:48:49 -070035import org.onlab.metrics.MetricsService;
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070036import org.onlab.util.Tools;
Flavio Castro6baf95d2015-08-11 14:48:49 -070037
38import java.util.Comparator;
39import java.util.Map;
40import java.util.Map.Entry;
41
42import static java.lang.String.format;
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080043
44/**
45 * Prints metrics in the system.
46 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070047@Service
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080048@Command(scope = "onos", name = "metrics",
49 description = "Prints metrics in the system")
50public class MetricsListCommand extends AbstractShellCommand {
51
Jian Lifb16e992016-02-22 19:03:50 +090052 private static final String COUNTER = "counter";
53
54 private static final String GAUGE = "gauge";
55 private static final String VALUE = "value";
56
57 private static final String METER = "meter";
58 private static final String MEAN_RATE = "mean_rate";
59 private static final String ONE_MIN_RATE = "1_min_rate";
60 private static final String FIVE_MIN_RATE = "5_min_rate";
61 private static final String FIFT_MIN_RATE = "15_min_rate";
62
63 private static final String HISTOGRAM = "histogram";
64 private static final String MIN = "min";
65 private static final String MAX = "max";
66 private static final String MEAN = "mean";
67 private static final String STDDEV = "stddev";
68
69 private static final String TIMER = "timer";
70
Flavio Castro4b519412015-07-24 12:57:59 -070071 @Argument(index = 0, name = "metricName", description = "Name of Metric",
72 required = false, multiValued = false)
73 String metricName = null;
74
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080075 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076 protected void doExecute() {
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080077 MetricsService metricsService = get(MetricsService.class);
78
Flavio Castro4b519412015-07-24 12:57:59 -070079 MetricFilter filter = metricName != null ? (name, metric) -> name.equals(metricName) : MetricFilter.ALL;
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080080
81 TreeMultimap<String, Metric> matched = listMetrics(metricsService, filter);
82 matched.asMap().forEach((name, metrics) -> {
Jian Lifb16e992016-02-22 19:03:50 +090083 if (outputJson()) {
84 metrics.forEach(metric -> print("%s", json(metric)));
85 } else {
86 metrics.forEach(metric -> printMetric(name, metric));
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080087 }
88 });
89 }
90
91 /**
92 * Print metric object.
93 *
94 * @param name metric name
95 * @param metric metric object
96 */
97 private void printMetric(String name, Metric metric) {
98 final String heading;
99
100 if (metric instanceof Counter) {
101 heading = format("-- %s : [%s] --", name, "Counter");
102 print(heading);
103 Counter counter = (Counter) metric;
104 print(" count = %d", counter.getCount());
105
106 } else if (metric instanceof Gauge) {
107 heading = format("-- %s : [%s] --", name, "Gauge");
108 print(heading);
109 @SuppressWarnings("rawtypes")
110 Gauge gauge = (Gauge) metric;
111 final Object value = gauge.getValue();
112 if (name.endsWith("EpochMs") && value instanceof Long) {
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700113 print(" value = %s (%s)", value, Tools.defaultOffsetDataTime((Long) value));
Yuta HIGUCHI6a462832014-11-23 23:56:03 -0800114 } else {
115 print(" value = %s", value);
116 }
117
118 } else if (metric instanceof Histogram) {
119 heading = format("-- %s : [%s] --", name, "Histogram");
120 print(heading);
121 final Histogram histogram = (Histogram) metric;
122 final Snapshot snapshot = histogram.getSnapshot();
123 print(" count = %d", histogram.getCount());
124 print(" min = %d", snapshot.getMin());
125 print(" max = %d", snapshot.getMax());
126 print(" mean = %f", snapshot.getMean());
127 print(" stddev = %f", snapshot.getStdDev());
128
129 } else if (metric instanceof Meter) {
130 heading = format("-- %s : [%s] --", name, "Meter");
131 print(heading);
132 final Meter meter = (Meter) metric;
133 print(" count = %d", meter.getCount());
134 print(" mean rate = %f", meter.getMeanRate());
135 print(" 1-minute rate = %f", meter.getOneMinuteRate());
136 print(" 5-minute rate = %f", meter.getFiveMinuteRate());
137 print(" 15-minute rate = %f", meter.getFifteenMinuteRate());
138
139 } else if (metric instanceof Timer) {
140 heading = format("-- %s : [%s] --", name, "Timer");
141 print(heading);
142 final Timer timer = (Timer) metric;
143 final Snapshot snapshot = timer.getSnapshot();
144 print(" count = %d", timer.getCount());
Flavio Castro6baf95d2015-08-11 14:48:49 -0700145 print(" mean rate = %f per second", timer.getMeanRate());
146 print(" 1-minute rate = %f per second", timer.getOneMinuteRate());
147 print(" 5-minute rate = %f per second", timer.getFiveMinuteRate());
148 print(" 15-minute rate = %f per second", timer.getFifteenMinuteRate());
Yuta HIGUCHI6a462832014-11-23 23:56:03 -0800149 print(" min = %f ms", nanoToMs(snapshot.getMin()));
150 print(" max = %f ms", nanoToMs(snapshot.getMax()));
151 print(" mean = %f ms", nanoToMs(snapshot.getMean()));
152 print(" stddev = %f ms", nanoToMs(snapshot.getStdDev()));
153 } else {
154 heading = format("-- %s : [%s] --", name, metric.getClass().getCanonicalName());
155 print(heading);
156 print("Unknown Metric type:{}", metric.getClass().getCanonicalName());
157 }
158 print(Strings.repeat("-", heading.length()));
159 }
160
161 @SuppressWarnings("rawtypes")
162 private TreeMultimap<String, Metric> listMetrics(MetricsService metricsService, MetricFilter filter) {
163 TreeMultimap<String, Metric> metrics = TreeMultimap.create(Comparator.naturalOrder(), Ordering.arbitrary());
164
165 Map<String, Counter> counters = metricsService.getCounters(filter);
166 for (Entry<String, Counter> entry : counters.entrySet()) {
167 metrics.put(entry.getKey(), entry.getValue());
168 }
169 Map<String, Gauge> gauges = metricsService.getGauges(filter);
170 for (Entry<String, Gauge> entry : gauges.entrySet()) {
171 metrics.put(entry.getKey(), entry.getValue());
172 }
173 Map<String, Histogram> histograms = metricsService.getHistograms(filter);
174 for (Entry<String, Histogram> entry : histograms.entrySet()) {
175 metrics.put(entry.getKey(), entry.getValue());
176 }
177 Map<String, Meter> meters = metricsService.getMeters(filter);
178 for (Entry<String, Meter> entry : meters.entrySet()) {
179 metrics.put(entry.getKey(), entry.getValue());
180 }
181 Map<String, Timer> timers = metricsService.getTimers(filter);
182 for (Entry<String, Timer> entry : timers.entrySet()) {
183 metrics.put(entry.getKey(), entry.getValue());
184 }
185
186 return metrics;
187 }
188
Jian Lifb16e992016-02-22 19:03:50 +0900189 /**
190 * Creates a json object for a certain metric.
191 *
192 * @param metric metric object
193 * @return json object
194 */
195 private ObjectNode json(Metric metric) {
196 ObjectMapper mapper = new ObjectMapper();
197 ObjectNode objectNode = mapper.createObjectNode();
198 ObjectNode dataNode = mapper.createObjectNode();
199
200 if (metric instanceof Counter) {
201 dataNode.put(COUNTER, ((Counter) metric).getCount());
202 objectNode.set(COUNTER, dataNode);
203 } else if (metric instanceof Gauge) {
204 objectNode.put(VALUE, ((Gauge) metric).getValue().toString());
205 objectNode.set(GAUGE, dataNode);
206 } else if (metric instanceof Meter) {
207 dataNode.put(COUNTER, ((Meter) metric).getCount());
208 dataNode.put(MEAN_RATE, ((Meter) metric).getMeanRate());
209 dataNode.put(ONE_MIN_RATE, ((Meter) metric).getOneMinuteRate());
210 dataNode.put(FIVE_MIN_RATE, ((Meter) metric).getFiveMinuteRate());
211 dataNode.put(FIFT_MIN_RATE, ((Meter) metric).getFifteenMinuteRate());
212 objectNode.set(METER, dataNode);
213 } else if (metric instanceof Histogram) {
214 dataNode.put(COUNTER, ((Histogram) metric).getCount());
215 dataNode.put(MEAN, ((Histogram) metric).getSnapshot().getMean());
216 dataNode.put(MIN, ((Histogram) metric).getSnapshot().getMin());
217 dataNode.put(MAX, ((Histogram) metric).getSnapshot().getMax());
218 dataNode.put(STDDEV, ((Histogram) metric).getSnapshot().getStdDev());
219 objectNode.set(HISTOGRAM, dataNode);
220 } else if (metric instanceof Timer) {
221 dataNode.put(COUNTER, ((Timer) metric).getCount());
222 dataNode.put(MEAN_RATE, ((Timer) metric).getMeanRate());
223 dataNode.put(ONE_MIN_RATE, ((Timer) metric).getOneMinuteRate());
224 dataNode.put(FIVE_MIN_RATE, ((Timer) metric).getFiveMinuteRate());
225 dataNode.put(FIFT_MIN_RATE, ((Timer) metric).getFifteenMinuteRate());
226 dataNode.put(MEAN, nanoToMs(((Timer) metric).getSnapshot().getMean()));
227 dataNode.put(MIN, nanoToMs(((Timer) metric).getSnapshot().getMin()));
228 dataNode.put(MAX, nanoToMs(((Timer) metric).getSnapshot().getMax()));
229 dataNode.put(STDDEV, nanoToMs(((Timer) metric).getSnapshot().getStdDev()));
230 objectNode.set(TIMER, dataNode);
231 }
232 return objectNode;
233 }
234
Yuta HIGUCHI6a462832014-11-23 23:56:03 -0800235 private double nanoToMs(double nano) {
236 return nano / 1_000_000D;
237 }
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700238
239
Yuta HIGUCHI6a462832014-11-23 23:56:03 -0800240}