Jian Li
Committed by Gerrit Code Review

[ONOS-4127] Backup metric service to ganglia monitoring server

- Initial implementation of metric service reporter
- Unit test for metric service reporter
- Bump up metric lib from 3.1.0 to 3.1.2

Change-Id: Ia178df759d671dfc15cffaacb09d5279ad3d9ad8
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--
~ Copyright 2016 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.
-->
<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}">
<feature name="${project.artifactId}" version="${project.version}"
description="${project.description}">
<feature>onos-api</feature>
<bundle>mvn:${project.groupId}/onos-app-metrics-reporter/${project.version}</bundle>
<bundle>mvn:io.dropwizard.metrics/metrics-core/3.1.2</bundle>
<bundle>mvn:io.dropwizard.metrics/metrics-ganglia/3.1.2</bundle>
<bundle>mvn:info.ganglia.gmetric4j/gmetric4j/1.0.10</bundle>
<bundle>mvn:org.acplt.remotetea/remotetea-oncrpc/1.1.3</bundle>
</feature>
</features>
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2016 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.onosproject</groupId>
<artifactId>onos-apps</artifactId>
<version>1.6.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>onos-app-metrics-reporter</artifactId>
<packaging>bundle</packaging>
<description>Performance metric service reporter</description>
<properties>
<onos.app.name>org.onosproject.metrics.reporter</onos.app.name>
<onos.app.title>Metric Service Reporter</onos.app.title>
<onos.app.category>Monitoring</onos.app.category>
<onos.app.url>http://onosproject.org</onos.app.url>
<metrics.version>3.1.2</metrics.version>
</properties>
<dependencies>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onlab-misc</artifactId>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-cli</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.karaf.shell</groupId>
<artifactId>org.apache.karaf.shell.console</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>${metrics.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-json</artifactId>
<version>${metrics.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-ganglia</artifactId>
<version>${metrics.version}</version>
</dependency>
<dependency>
<groupId>info.ganglia.gmetric4j</groupId>
<artifactId>gmetric4j</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>org.acplt.remotetea</groupId>
<artifactId>remotetea-oncrpc</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onlab-junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-api</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
</dependencies>
</project>
/*
* Copyright 2016 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.onosproject.metrics.reporter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.ganglia.GangliaReporter;
import info.ganglia.gmetric4j.gmetric.GMetric;
import org.apache.commons.lang.StringUtils;
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.Modified;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onlab.metrics.MetricsService;
import org.onlab.util.Tools;
import org.onosproject.cfg.ComponentConfigService;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import java.io.IOException;
import java.util.Dictionary;
import java.util.concurrent.TimeUnit;
import static org.slf4j.LoggerFactory.getLogger;
/**
* A metric report that reports all metrics value to ganglia monitoring server.
*/
@Component(immediate = true)
public class GangliaMetricsReporter implements MetricsReporter {
private final Logger log = getLogger(getClass());
// we will use uni-cast mode to transfer the metrics value by default
private static final GMetric.UDPAddressingMode GANGLIA_MODE =
GMetric.UDPAddressingMode.UNICAST;
private static final int REPORT_PERIOD = 1;
private static final TimeUnit REPORT_TIME_UNIT = TimeUnit.MINUTES;
private static final String DEFAULT_ADDRESS = "localhost";
private static final int DEFAULT_PORT = 8649;
private static final int DEFAULT_TTL = 1;
private static final String DEFAULT_METRIC_NAMES = "default";
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected CoreService coreService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected MetricsService metricsService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected ComponentConfigService cfgService;
@Property(name = "monitorAll", boolValue = true,
label = "Enable to monitor all of metrics stored in metric registry default is true")
protected boolean monitorAll = true;
@Property(name = "metricNames", value = DEFAULT_METRIC_NAMES,
label = "Names of metric to be monitored; default metric names are 'default'")
protected String metricNames = DEFAULT_METRIC_NAMES;
@Property(name = "address", value = DEFAULT_ADDRESS,
label = "IP address of ganglia monitoring server; default is localhost")
protected String address = DEFAULT_ADDRESS;
@Property(name = "port", intValue = DEFAULT_PORT,
label = "Port number of ganglia monitoring server; default is 8649")
protected int port = DEFAULT_PORT;
@Property(name = "ttl", intValue = DEFAULT_TTL,
label = "TTL value of ganglia monitoring server; default is 1")
protected int ttl = DEFAULT_TTL;
private ApplicationId appId;
private GMetric ganglia;
private GangliaReporter gangliaReporter;
@Activate
public void activate() {
cfgService.registerProperties(getClass());
appId = coreService.registerApplication("org.onosproject.metrics.reporter");
startReport();
log.info("Started");
}
@Deactivate
public void deactivate() {
cfgService.unregisterProperties(getClass(), false);
stopReport();
log.info("Stopped");
}
@Modified
public void modified(ComponentContext context) {
readComponentConfiguration(context);
stopReport();
startReport();
}
@Override
public void startReport() {
try {
ganglia = new GMetric(address, port, GANGLIA_MODE, ttl);
MetricRegistry mr = metricsService.getMetricRegistry();
gangliaReporter = GangliaReporter.forRegistry(filter(mr))
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build(ganglia);
gangliaReporter.start(REPORT_PERIOD, REPORT_TIME_UNIT);
} catch (IOException e) {
log.error("Fail to connect to given ganglia server!");
}
}
@Override
public void stopReport() {
gangliaReporter.stop();
ganglia = null;
gangliaReporter = null;
}
/**
* Filters the metrics to only include a set of the given metrics.
*
* @param metricRegistry original metric registry
* @return filtered metric registry
*/
protected MetricRegistry filter(MetricRegistry metricRegistry) {
if (!monitorAll) {
final MetricRegistry filtered = new MetricRegistry();
metricRegistry.getNames().stream().filter(name ->
containsName(name, metricNames)).forEach(name ->
filtered.register(name, metricRegistry.getMetrics().get(name)));
return filtered;
} else {
return metricRegistry;
}
}
/**
* Looks up whether the metric name contains the given prefix keywords.
* Note that the keywords are separated with comma as delimiter
*
* @param full the original metric name that to be compared with
* @param prefixes the prefix keywords that are matched against with the metric name
* @return boolean value that denotes whether the metric name starts with the given prefix
*/
protected boolean containsName(String full, String prefixes) {
String[] prefixArray = StringUtils.split(prefixes, ",");
for (String prefix : prefixArray) {
if (StringUtils.startsWith(full, StringUtils.trimToEmpty(prefix))) {
return true;
}
}
return false;
}
/**
* Extracts properties from the component configuration context.
*
* @param context the component context
*/
private void readComponentConfiguration(ComponentContext context) {
Dictionary<?, ?> properties = context.getProperties();
String addressStr = Tools.get(properties, "address");
address = addressStr != null ? addressStr : DEFAULT_ADDRESS;
log.info("Configured. Ganglia server address is {}", address);
String metricNameStr = Tools.get(properties, "metricNames");
metricNames = metricNameStr != null ? metricNameStr : DEFAULT_METRIC_NAMES;
log.info("Configured. Metric name is {}", metricNames);
Integer portConfigured = Tools.getIntegerProperty(properties, "port");
if (portConfigured == null) {
port = DEFAULT_PORT;
log.info("Ganglia port is not configured, default value is {}", port);
} else {
port = portConfigured;
log.info("Configured. Ganglia port is configured to {}", port);
}
Integer ttlConfigured = Tools.getIntegerProperty(properties, "ttl");
if (ttlConfigured == null) {
ttl = DEFAULT_TTL;
log.info("Ganglia TTL is not configured, default value is {}", ttl);
} else {
ttl = ttlConfigured;
log.info("Configured. Ganglia TTL is configured to {}", ttl);
}
Boolean monitorAllEnabled = Tools.isPropertyEnabled(properties, "monitorAll");
if (monitorAllEnabled == null) {
log.info("Monitor all metrics is not configured, " +
"using current value of {}", monitorAll);
} else {
monitorAll = monitorAllEnabled;
log.info("Configured. Monitor all metrics is {}",
monitorAll ? "enabled" : "disabled");
}
}
}
/*
* Copyright 2016 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.onosproject.metrics.reporter;
/**
* A metric report that reports all metrics value to third party monitor.
*/
public interface MetricsReporter {
/**
* Starts to report a set of metrics to the monitoring server.
*/
void startReport();
/**
* Stops reporting metrics.
*/
void stopReport();
}
/*
* Copyright 2016 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.
*/
/**
* Contains various reporters that backup all of the collected metrics
* to monitoring server.
*/
package org.onosproject.metrics.reporter;
/*
* Copyright 2016 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.onosproject.metrics.reporter;
import com.codahale.metrics.MetricRegistry;
import com.google.common.collect.ImmutableSet;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Unit test for metrics reporter of ganglia.
*/
public class GangliaMetricsReporterTest {
private GangliaMetricsReporter gmr;
private static final String METRIC_NAME1 = "consistentMap.onos-app-ids.putIfAbsent";
private static final String METRIC_NAME2 = "consistentMap.onos-hosts.entrySet";
private static final String METRIC_NAME3 = "clusterCommunication.endpoint.*";
private static final String METRIC_NAME4 = "atomicCounter.onos-app-id-counter.*";
private static final String PREFIXES1 = "consistentMap";
private static final String PREFIXES2 = "topology";
private static final String PREFIXES3 = "consistentMap.onos-app-ids";
private static final String PREFIXES4 = "consistentMap, clusterCommunication, atomicCounter";
/**
* Initializes ganglia metrics reporter instance.
*/
@Before
public void setUp() {
gmr = new GangliaMetricsReporter();
}
/**
* Tests whether the containsName method can always return the correct result
* with the given metric name and a set of prefixes.
*/
@Test
public void testContainsName() {
assertTrue(gmr.containsName(METRIC_NAME1, PREFIXES1));
assertTrue(gmr.containsName(METRIC_NAME1, PREFIXES3));
assertTrue(gmr.containsName(METRIC_NAME1, PREFIXES4));
assertTrue(gmr.containsName(METRIC_NAME2, PREFIXES4));
assertTrue(gmr.containsName(METRIC_NAME3, PREFIXES4));
assertTrue(gmr.containsName(METRIC_NAME4, PREFIXES4));
assertFalse(gmr.containsName(METRIC_NAME1, PREFIXES2));
}
/**
* Tests whether the filter method can always return the correct result.
*/
@Test
public void testFilter() {
MetricRegistry filtered;
MetricRegistry full = new MetricRegistry();
full.meter(METRIC_NAME1);
full.meter(METRIC_NAME2);
full.meter(METRIC_NAME3);
full.meter(METRIC_NAME4);
gmr.monitorAll = true;
filtered = gmr.filter(full);
assertTrue(filtered.getNames()
.containsAll(ImmutableSet.of(METRIC_NAME1, METRIC_NAME2,
METRIC_NAME3, METRIC_NAME4)));
gmr.monitorAll = false;
gmr.metricNames = PREFIXES1;
filtered = gmr.filter(full);
assertTrue(filtered.getNames()
.containsAll(ImmutableSet.of(METRIC_NAME1, METRIC_NAME2)));
assertFalse(filtered.getNames()
.containsAll(ImmutableSet.of(METRIC_NAME3, METRIC_NAME4)));
gmr.metricNames = PREFIXES2;
filtered = gmr.filter(full);
assertFalse(filtered.getNames().containsAll(ImmutableSet.of(METRIC_NAME1)));
gmr.metricNames = PREFIXES3;
filtered = gmr.filter(full);
assertTrue(filtered.getNames().containsAll(ImmutableSet.of(METRIC_NAME1)));
assertFalse(filtered.getNames().containsAll(ImmutableSet.of(METRIC_NAME2)));
gmr.metricNames = PREFIXES4;
filtered = gmr.filter(full);
assertTrue(filtered.getNames()
.containsAll(ImmutableSet.of(METRIC_NAME1, METRIC_NAME2,
METRIC_NAME3, METRIC_NAME4)));
}
}
......@@ -38,6 +38,7 @@
<module>sdnip</module>
<module>optical</module>
<module>metrics</module>
<module>metricsreporter</module>
<module>routing</module>
<module>routing-api</module>
<module>reactive-routing</module>
......
......@@ -36,8 +36,8 @@
<bundle>mvn:joda-time/joda-time/2.9</bundle>
<bundle>mvn:io.dropwizard.metrics/metrics-core/3.1.0</bundle>
<bundle>mvn:io.dropwizard.metrics/metrics-json/3.1.0</bundle>
<bundle>mvn:io.dropwizard.metrics/metrics-core/3.1.2</bundle>
<bundle>mvn:io.dropwizard.metrics/metrics-json/3.1.2</bundle>
<bundle>mvn:com.eclipsesource.minimal-json/minimal-json/0.9.1</bundle>
<bundle>mvn:com.esotericsoftware/kryo/3.0.0</bundle>
......
......@@ -31,6 +31,10 @@
<description>Miscellaneous ON.Lab utilities</description>
<properties>
<metrics.version>3.1.2</metrics.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
......@@ -71,12 +75,12 @@
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.1.0</version>
<version>${metrics.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-json</artifactId>
<version>3.1.0</version>
<version>${metrics.version}</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
......
......@@ -102,6 +102,16 @@ public class MetricsManager implements MetricsService {
}
/**
* Fetches existing metric registry.
*
* @return metric registry
*/
@Override
public MetricRegistry getMetricRegistry() {
return metricsRegistry;
}
/**
* Generates a name for a Metric from its component and feature.
*
* @param component component the metric is defined in
......
......@@ -23,6 +23,7 @@ import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
/**
......@@ -39,6 +40,13 @@ public interface MetricsService {
MetricsComponent registerComponent(String name);
/**
* Fetches existing metric registry.
*
* @return metric registry
*/
MetricRegistry getMetricRegistry();
/**
* Creates a Counter metric.
*
* @param component component the Counter is defined in
......
......@@ -19,6 +19,7 @@ package org.onlab.metrics;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Metric;
......@@ -39,6 +40,11 @@ public class MetricsServiceAdapter implements MetricsService {
}
@Override
public MetricRegistry getMetricRegistry() {
return null;
}
@Override
public Counter createCounter(MetricsComponent component,
MetricsFeature feature, String metricName) {
return null;
......