Yuta HIGUCHI

metrics command to dump all the Metric in the system.

- Add probe to measure the time spent querying for Mastership.

Change-Id: I48fe37568a3261ee5b6229a3884e42a3a741b407
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onlab.onos.cli;
import static java.lang.String.format;
import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.karaf.shell.commands.Command;
import org.joda.time.LocalDateTime;
import org.onlab.metrics.MetricsService;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.Snapshot;
import com.codahale.metrics.Timer;
import com.google.common.base.Strings;
import com.google.common.collect.Ordering;
import com.google.common.collect.TreeMultimap;
/**
* Prints metrics in the system.
*/
@Command(scope = "onos", name = "metrics",
description = "Prints metrics in the system")
public class MetricsListCommand extends AbstractShellCommand {
@Override
protected void execute() {
MetricsService metricsService = get(MetricsService.class);
// TODO support filter condition
MetricFilter filter = MetricFilter.ALL;
TreeMultimap<String, Metric> matched = listMetrics(metricsService, filter);
matched.asMap().forEach((name, metrics) -> {
for (Metric metric : metrics) {
// TODO JSON version
printMetric(name, metric);
}
});
}
/**
* Print metric object.
*
* @param name metric name
* @param metric metric object
*/
private void printMetric(String name, Metric metric) {
final String heading;
if (metric instanceof Counter) {
heading = format("-- %s : [%s] --", name, "Counter");
print(heading);
Counter counter = (Counter) metric;
print(" count = %d", counter.getCount());
} else if (metric instanceof Gauge) {
heading = format("-- %s : [%s] --", name, "Gauge");
print(heading);
@SuppressWarnings("rawtypes")
Gauge gauge = (Gauge) metric;
final Object value = gauge.getValue();
if (name.endsWith("EpochMs") && value instanceof Long) {
print(" value = %s (%s)", value, new LocalDateTime(value));
} else {
print(" value = %s", value);
}
} else if (metric instanceof Histogram) {
heading = format("-- %s : [%s] --", name, "Histogram");
print(heading);
final Histogram histogram = (Histogram) metric;
final Snapshot snapshot = histogram.getSnapshot();
print(" count = %d", histogram.getCount());
print(" min = %d", snapshot.getMin());
print(" max = %d", snapshot.getMax());
print(" mean = %f", snapshot.getMean());
print(" stddev = %f", snapshot.getStdDev());
} else if (metric instanceof Meter) {
heading = format("-- %s : [%s] --", name, "Meter");
print(heading);
final Meter meter = (Meter) metric;
print(" count = %d", meter.getCount());
print(" mean rate = %f", meter.getMeanRate());
print(" 1-minute rate = %f", meter.getOneMinuteRate());
print(" 5-minute rate = %f", meter.getFiveMinuteRate());
print(" 15-minute rate = %f", meter.getFifteenMinuteRate());
} else if (metric instanceof Timer) {
heading = format("-- %s : [%s] --", name, "Timer");
print(heading);
final Timer timer = (Timer) metric;
final Snapshot snapshot = timer.getSnapshot();
print(" count = %d", timer.getCount());
print(" mean rate = %f", timer.getMeanRate());
print(" 1-minute rate = %f", timer.getOneMinuteRate());
print(" 5-minute rate = %f", timer.getFiveMinuteRate());
print(" 15-minute rate = %f", timer.getFifteenMinuteRate());
print(" min = %f ms", nanoToMs(snapshot.getMin()));
print(" max = %f ms", nanoToMs(snapshot.getMax()));
print(" mean = %f ms", nanoToMs(snapshot.getMean()));
print(" stddev = %f ms", nanoToMs(snapshot.getStdDev()));
} else {
heading = format("-- %s : [%s] --", name, metric.getClass().getCanonicalName());
print(heading);
print("Unknown Metric type:{}", metric.getClass().getCanonicalName());
}
print(Strings.repeat("-", heading.length()));
}
@SuppressWarnings("rawtypes")
private TreeMultimap<String, Metric> listMetrics(MetricsService metricsService, MetricFilter filter) {
TreeMultimap<String, Metric> metrics = TreeMultimap.create(Comparator.naturalOrder(), Ordering.arbitrary());
Map<String, Counter> counters = metricsService.getCounters(filter);
for (Entry<String, Counter> entry : counters.entrySet()) {
metrics.put(entry.getKey(), entry.getValue());
}
Map<String, Gauge> gauges = metricsService.getGauges(filter);
for (Entry<String, Gauge> entry : gauges.entrySet()) {
metrics.put(entry.getKey(), entry.getValue());
}
Map<String, Histogram> histograms = metricsService.getHistograms(filter);
for (Entry<String, Histogram> entry : histograms.entrySet()) {
metrics.put(entry.getKey(), entry.getValue());
}
Map<String, Meter> meters = metricsService.getMeters(filter);
for (Entry<String, Meter> entry : meters.entrySet()) {
metrics.put(entry.getKey(), entry.getValue());
}
Map<String, Timer> timers = metricsService.getTimers(filter);
for (Entry<String, Timer> entry : timers.entrySet()) {
metrics.put(entry.getKey(), entry.getValue());
}
return metrics;
}
private double nanoToMs(double nano) {
return nano / 1_000_000D;
}
}
......@@ -19,6 +19,11 @@
<command>
<action class="org.onlab.onos.cli.SummaryCommand"/>
</command>
<command>
<action class="org.onlab.onos.cli.MetricsListCommand"/>
</command>
<command>
<action class="org.onlab.onos.cli.TabletMemberCommand"/>
</command>
......
......@@ -27,6 +27,9 @@ import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onlab.metrics.MetricsComponent;
import org.onlab.metrics.MetricsFeature;
import org.onlab.metrics.MetricsService;
import org.onlab.onos.cluster.ClusterEvent;
import org.onlab.onos.cluster.ClusterEventListener;
import org.onlab.onos.cluster.ClusterService;
......@@ -47,6 +50,9 @@ import org.onlab.onos.net.DeviceId;
import org.onlab.onos.net.MastershipRole;
import org.slf4j.Logger;
import com.codahale.metrics.Timer;
import com.codahale.metrics.Timer.Context;
@Component(immediate = true)
@Service
public class MastershipManager
......@@ -72,10 +78,16 @@ public class MastershipManager
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected ClusterService clusterService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected MetricsService metricsService;
private ClusterEventListener clusterListener = new InternalClusterEventListener();
private Timer requestRoleTimer;
@Activate
public void activate() {
requestRoleTimer = createTimer("Mastership", "requestRole", "responseTime");
eventDispatcher.addSink(MastershipEvent.class, listenerRegistry);
clusterService.addListener(clusterListener);
store.setDelegate(delegate);
......@@ -137,7 +149,12 @@ public class MastershipManager
@Override
public MastershipRole requestRoleFor(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_ID_NULL);
return store.requestRole(deviceId);
final Context timer = startTimer(requestRoleTimer);
try {
return store.requestRole(deviceId);
} finally {
stopTimer(timer);
}
}
@Override
......@@ -186,6 +203,28 @@ public class MastershipManager
private Timer createTimer(String component, String feature, String name) {
if (metricsService != null) {
MetricsComponent c = metricsService.registerComponent(component);
MetricsFeature f = c.registerFeature(feature);
return metricsService.createTimer(c, f, name);
}
return null;
}
private static final Context startTimer(Timer timer) {
if (timer != null) {
return timer.time();
}
return null;
}
private static final void stopTimer(Context context) {
if (context != null) {
context.stop();
}
}
//callback for reacting to cluster events
private class InternalClusterEventListener implements ClusterEventListener {
......
......@@ -154,7 +154,7 @@ public class MetricsManager implements MetricsService {
* Creates a Timer metric.
*
* @param component component the Timer is defined in
* @param feature feature the Timeer is defined in
* @param feature feature the Timer is defined in
* @param metricName local name of the metric
* @return the created Timer Metric
*/
......