Pavlin Radoslavov

Refactoring and cleanup in the Metrics module:

 * Removed logging related code inside class MetricsManager
 * Removed @Component related code, because it is not suppose to be used
   as an component.
 * Added a new class-wrapper so the Metrics can be used as a loadable service:
   MetricsManagerComponent. The name and the location of this class
   will be refactored in the future.
 * Added new method MetricsManager.removeMetric()
 * Line formatting
 * Changed "interface MetricsService" to public
......@@ -68,7 +68,6 @@ private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class);
metrics = new MetricsManager();
Endpoint endpoint = new Endpoint(host, port);
messaging.activate();
metrics.activate();
MetricsFeature feature = new MetricsFeature("latency");
MetricsComponent component = metrics.registerComponent("NettyMessaging");
log.info("connecting " + host + ":" + port + " warmup:" + warmup + " iterations:" + iterations);
......@@ -117,7 +116,6 @@ private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class);
public static void stop() {
try {
messaging.deactivate();
metrics.deactivate();
} catch (Exception e) {
log.info("Unable to stop client %s", e);
}
......
package org.onlab.onos.impl;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Service;
import org.onlab.metrics.MetricsManager;
/**
* Metrics service implementation.
*/
@Component(immediate = true)
@Service
public class MetricsManagerComponent extends MetricsManager {
@Activate
protected void activate() {
super.clear();
}
@Deactivate
protected void deactivate() {
super.clear();
}
}
......@@ -3,15 +3,7 @@ package org.onlab.metrics;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
......@@ -53,48 +45,25 @@ import com.codahale.metrics.Timer;
* </code>
* </pre>
*/
@Component(immediate = true)
public final class MetricsManager implements MetricsService {
public class MetricsManager implements MetricsService {
private final Logger log = LoggerFactory.getLogger(getClass());
/**
* Registry to hold the Components defined in the system.
*/
private ConcurrentMap<String, MetricsComponent> componentsRegistry;
private ConcurrentMap<String, MetricsComponent> componentsRegistry =
new ConcurrentHashMap<>();
/**
* Registry for the Metrics objects created in the system.
*/
private final MetricRegistry metricsRegistry;
private MetricRegistry metricsRegistry = new MetricRegistry();
/**
* Default Reporter for this metrics manager.
* Clears the internal state.
*/
//private final Slf4jReporter reporter;
private final ConsoleReporter reporter;
public MetricsManager() {
this.metricsRegistry = new MetricRegistry();
// this.reporter = Slf4jReporter.forRegistry(this.metricsRegistry)
// .outputTo(log)
// .convertRatesTo(TimeUnit.SECONDS)
// .convertDurationsTo(TimeUnit.MICROSECONDS)
// .build();
this.reporter = ConsoleReporter.forRegistry(this.metricsRegistry)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MICROSECONDS)
.build();
}
@Activate
public void activate() {
protected void clear() {
this.componentsRegistry = new ConcurrentHashMap<>();
reporter.start(10, TimeUnit.SECONDS);
}
@Deactivate
public void deactivate() {
reporter.stop();
this.metricsRegistry = new MetricRegistry();
}
/**
......@@ -107,7 +76,8 @@ public final class MetricsManager implements MetricsService {
public MetricsComponent registerComponent(final String name) {
MetricsComponent component = componentsRegistry.get(name);
if (component == null) {
final MetricsComponent createdComponent = new MetricsComponent(name);
final MetricsComponent createdComponent =
new MetricsComponent(name);
component = componentsRegistry.putIfAbsent(name, createdComponent);
if (component == null) {
component = createdComponent;
......@@ -221,6 +191,22 @@ public final class MetricsManager implements MetricsService {
}
/**
* Removes the metric with the given name.
*
* @param component component the Metric is defined in
* @param feature feature the Metric is defined in
* @param metricName local name of the metric
* @return true if the metric existed and was removed, otherwise false
*/
@Override
public boolean removeMetric(final MetricsComponent component,
final MetricsFeature feature,
final String metricName) {
final String name = generateName(component, feature, metricName);
return metricsRegistry.remove(name);
}
/**
* Fetches the existing Timers.
*
* @param filter filter to use to select Timers
......@@ -272,8 +258,8 @@ public final class MetricsManager implements MetricsService {
* Fetches the existing Histograms.
*
* @param filter filter to use to select Histograms
* @return a map of the Histograms that match the filter, with the key as the
* name String to the Histogram.
* @return a map of the Histograms that match the filter, with the key as
* the name String to the Histogram.
*/
@Override
public Map<String, Histogram> getHistograms(final MetricFilter filter) {
......@@ -290,4 +276,3 @@ public final class MetricsManager implements MetricsService {
metricsRegistry.removeMatching(filter);
}
}
......
......@@ -13,7 +13,7 @@ import com.codahale.metrics.Timer;
/**
* Metrics Service to collect metrics.
*/
interface MetricsService {
public interface MetricsService {
/**
* Registers a component.
......@@ -90,6 +90,18 @@ interface MetricsService {
T metric);
/**
* Removes the metric with the given name.
*
* @param component component the Metric is defined in
* @param feature feature the Metric is defined in
* @param metricName local name of the metric
* @return true if the metric existed and was removed, otherwise false
*/
boolean removeMetric(MetricsComponent component,
MetricsFeature feature,
String metricName);
/**
* Fetches the existing Timers.
*
* @param filter filter to use to select Timers
......@@ -129,15 +141,15 @@ interface MetricsService {
* Fetches the existing Histograms.
*
* @param filter filter to use to select Histograms
* @return a map of the Histograms that match the filter, with the key as the
* name String to the Histogram.
* @return a map of the Histograms that match the filter, with the key as
* the name String to the Histogram.
*/
Map<String, Histogram> getHistograms(MetricFilter filter);
/**
* Removes all Metrics that match a given filter.
*
* @param filter filter to use to select the Metrics to remove.
*/
void removeMatching(MetricFilter filter);
}
......