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
Showing
12 changed files
with
563 additions
and
4 deletions
apps/metricsreporter/features.xml
0 → 100644
| 1 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
| 2 | +<!-- | ||
| 3 | + ~ Copyright 2016 Open Networking Laboratory | ||
| 4 | + ~ | ||
| 5 | + ~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 6 | + ~ you may not use this file except in compliance with the License. | ||
| 7 | + ~ You may obtain a copy of the License at | ||
| 8 | + ~ | ||
| 9 | + ~ http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | + ~ | ||
| 11 | + ~ Unless required by applicable law or agreed to in writing, software | ||
| 12 | + ~ distributed under the License is distributed on an "AS IS" BASIS, | ||
| 13 | + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 14 | + ~ See the License for the specific language governing permissions and | ||
| 15 | + ~ limitations under the License. | ||
| 16 | + --> | ||
| 17 | +<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}"> | ||
| 18 | + <feature name="${project.artifactId}" version="${project.version}" | ||
| 19 | + description="${project.description}"> | ||
| 20 | + <feature>onos-api</feature> | ||
| 21 | + <bundle>mvn:${project.groupId}/onos-app-metrics-reporter/${project.version}</bundle> | ||
| 22 | + <bundle>mvn:io.dropwizard.metrics/metrics-core/3.1.2</bundle> | ||
| 23 | + <bundle>mvn:io.dropwizard.metrics/metrics-ganglia/3.1.2</bundle> | ||
| 24 | + <bundle>mvn:info.ganglia.gmetric4j/gmetric4j/1.0.10</bundle> | ||
| 25 | + <bundle>mvn:org.acplt.remotetea/remotetea-oncrpc/1.1.3</bundle> | ||
| 26 | + </feature> | ||
| 27 | +</features> |
apps/metricsreporter/pom.xml
0 → 100644
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | +<!-- | ||
| 3 | + ~ Copyright 2016 Open Networking Laboratory | ||
| 4 | + ~ | ||
| 5 | + ~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 6 | + ~ you may not use this file except in compliance with the License. | ||
| 7 | + ~ You may obtain a copy of the License at | ||
| 8 | + ~ | ||
| 9 | + ~ http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | + ~ | ||
| 11 | + ~ Unless required by applicable law or agreed to in writing, software | ||
| 12 | + ~ distributed under the License is distributed on an "AS IS" BASIS, | ||
| 13 | + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 14 | + ~ See the License for the specific language governing permissions and | ||
| 15 | + ~ limitations under the License. | ||
| 16 | + --> | ||
| 17 | +<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| 18 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 19 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
| 20 | + <modelVersion>4.0.0</modelVersion> | ||
| 21 | + | ||
| 22 | + <parent> | ||
| 23 | + <groupId>org.onosproject</groupId> | ||
| 24 | + <artifactId>onos-apps</artifactId> | ||
| 25 | + <version>1.6.0-SNAPSHOT</version> | ||
| 26 | + <relativePath>../pom.xml</relativePath> | ||
| 27 | + </parent> | ||
| 28 | + | ||
| 29 | + <artifactId>onos-app-metrics-reporter</artifactId> | ||
| 30 | + <packaging>bundle</packaging> | ||
| 31 | + | ||
| 32 | + <description>Performance metric service reporter</description> | ||
| 33 | + | ||
| 34 | + <properties> | ||
| 35 | + <onos.app.name>org.onosproject.metrics.reporter</onos.app.name> | ||
| 36 | + <onos.app.title>Metric Service Reporter</onos.app.title> | ||
| 37 | + <onos.app.category>Monitoring</onos.app.category> | ||
| 38 | + <onos.app.url>http://onosproject.org</onos.app.url> | ||
| 39 | + <metrics.version>3.1.2</metrics.version> | ||
| 40 | + </properties> | ||
| 41 | + | ||
| 42 | + <dependencies> | ||
| 43 | + <dependency> | ||
| 44 | + <groupId>org.onosproject</groupId> | ||
| 45 | + <artifactId>onlab-misc</artifactId> | ||
| 46 | + </dependency> | ||
| 47 | + <dependency> | ||
| 48 | + <groupId>org.onosproject</groupId> | ||
| 49 | + <artifactId>onos-cli</artifactId> | ||
| 50 | + <version>${project.version}</version> | ||
| 51 | + </dependency> | ||
| 52 | + <dependency> | ||
| 53 | + <groupId>org.osgi</groupId> | ||
| 54 | + <artifactId>org.osgi.core</artifactId> | ||
| 55 | + </dependency> | ||
| 56 | + <dependency> | ||
| 57 | + <groupId>org.apache.karaf.shell</groupId> | ||
| 58 | + <artifactId>org.apache.karaf.shell.console</artifactId> | ||
| 59 | + </dependency> | ||
| 60 | + <dependency> | ||
| 61 | + <groupId>com.fasterxml.jackson.core</groupId> | ||
| 62 | + <artifactId>jackson-databind</artifactId> | ||
| 63 | + </dependency> | ||
| 64 | + <dependency> | ||
| 65 | + <groupId>com.fasterxml.jackson.core</groupId> | ||
| 66 | + <artifactId>jackson-annotations</artifactId> | ||
| 67 | + </dependency> | ||
| 68 | + <dependency> | ||
| 69 | + <groupId>org.osgi</groupId> | ||
| 70 | + <artifactId>org.osgi.compendium</artifactId> | ||
| 71 | + </dependency> | ||
| 72 | + <dependency> | ||
| 73 | + <groupId>io.dropwizard.metrics</groupId> | ||
| 74 | + <artifactId>metrics-core</artifactId> | ||
| 75 | + <version>${metrics.version}</version> | ||
| 76 | + </dependency> | ||
| 77 | + <dependency> | ||
| 78 | + <groupId>io.dropwizard.metrics</groupId> | ||
| 79 | + <artifactId>metrics-json</artifactId> | ||
| 80 | + <version>${metrics.version}</version> | ||
| 81 | + </dependency> | ||
| 82 | + <dependency> | ||
| 83 | + <groupId>io.dropwizard.metrics</groupId> | ||
| 84 | + <artifactId>metrics-ganglia</artifactId> | ||
| 85 | + <version>${metrics.version}</version> | ||
| 86 | + </dependency> | ||
| 87 | + <dependency> | ||
| 88 | + <groupId>info.ganglia.gmetric4j</groupId> | ||
| 89 | + <artifactId>gmetric4j</artifactId> | ||
| 90 | + <version>1.0.10</version> | ||
| 91 | + </dependency> | ||
| 92 | + <dependency> | ||
| 93 | + <groupId>org.acplt.remotetea</groupId> | ||
| 94 | + <artifactId>remotetea-oncrpc</artifactId> | ||
| 95 | + <version>1.1.3</version> | ||
| 96 | + </dependency> | ||
| 97 | + <dependency> | ||
| 98 | + <groupId>org.onosproject</groupId> | ||
| 99 | + <artifactId>onlab-junit</artifactId> | ||
| 100 | + <scope>test</scope> | ||
| 101 | + </dependency> | ||
| 102 | + <dependency> | ||
| 103 | + <groupId>org.easymock</groupId> | ||
| 104 | + <artifactId>easymock</artifactId> | ||
| 105 | + <scope>test</scope> | ||
| 106 | + </dependency> | ||
| 107 | + <dependency> | ||
| 108 | + <groupId>org.onosproject</groupId> | ||
| 109 | + <artifactId>onos-api</artifactId> | ||
| 110 | + <version>${project.version}</version> | ||
| 111 | + <classifier>tests</classifier> | ||
| 112 | + <scope>test</scope> | ||
| 113 | + </dependency> | ||
| 114 | + </dependencies> | ||
| 115 | + | ||
| 116 | +</project> |
apps/metricsreporter/src/main/java/org/onosproject/metrics/reporter/GangliaMetricsReporter.java
0 → 100644
| 1 | +/* | ||
| 2 | + * Copyright 2016 Open Networking Laboratory | ||
| 3 | + * | ||
| 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 | +package org.onosproject.metrics.reporter; | ||
| 17 | + | ||
| 18 | +import com.codahale.metrics.MetricRegistry; | ||
| 19 | +import com.codahale.metrics.ganglia.GangliaReporter; | ||
| 20 | +import info.ganglia.gmetric4j.gmetric.GMetric; | ||
| 21 | +import org.apache.commons.lang.StringUtils; | ||
| 22 | +import org.apache.felix.scr.annotations.Activate; | ||
| 23 | +import org.apache.felix.scr.annotations.Component; | ||
| 24 | +import org.apache.felix.scr.annotations.Deactivate; | ||
| 25 | +import org.apache.felix.scr.annotations.Modified; | ||
| 26 | +import org.apache.felix.scr.annotations.Property; | ||
| 27 | +import org.apache.felix.scr.annotations.Reference; | ||
| 28 | +import org.apache.felix.scr.annotations.ReferenceCardinality; | ||
| 29 | +import org.onlab.metrics.MetricsService; | ||
| 30 | +import org.onlab.util.Tools; | ||
| 31 | +import org.onosproject.cfg.ComponentConfigService; | ||
| 32 | +import org.onosproject.core.ApplicationId; | ||
| 33 | +import org.onosproject.core.CoreService; | ||
| 34 | +import org.osgi.service.component.ComponentContext; | ||
| 35 | +import org.slf4j.Logger; | ||
| 36 | + | ||
| 37 | +import java.io.IOException; | ||
| 38 | +import java.util.Dictionary; | ||
| 39 | +import java.util.concurrent.TimeUnit; | ||
| 40 | + | ||
| 41 | +import static org.slf4j.LoggerFactory.getLogger; | ||
| 42 | + | ||
| 43 | +/** | ||
| 44 | + * A metric report that reports all metrics value to ganglia monitoring server. | ||
| 45 | + */ | ||
| 46 | +@Component(immediate = true) | ||
| 47 | +public class GangliaMetricsReporter implements MetricsReporter { | ||
| 48 | + private final Logger log = getLogger(getClass()); | ||
| 49 | + | ||
| 50 | + // we will use uni-cast mode to transfer the metrics value by default | ||
| 51 | + private static final GMetric.UDPAddressingMode GANGLIA_MODE = | ||
| 52 | + GMetric.UDPAddressingMode.UNICAST; | ||
| 53 | + private static final int REPORT_PERIOD = 1; | ||
| 54 | + private static final TimeUnit REPORT_TIME_UNIT = TimeUnit.MINUTES; | ||
| 55 | + | ||
| 56 | + private static final String DEFAULT_ADDRESS = "localhost"; | ||
| 57 | + private static final int DEFAULT_PORT = 8649; | ||
| 58 | + private static final int DEFAULT_TTL = 1; | ||
| 59 | + private static final String DEFAULT_METRIC_NAMES = "default"; | ||
| 60 | + | ||
| 61 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
| 62 | + protected CoreService coreService; | ||
| 63 | + | ||
| 64 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
| 65 | + protected MetricsService metricsService; | ||
| 66 | + | ||
| 67 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
| 68 | + protected ComponentConfigService cfgService; | ||
| 69 | + | ||
| 70 | + @Property(name = "monitorAll", boolValue = true, | ||
| 71 | + label = "Enable to monitor all of metrics stored in metric registry default is true") | ||
| 72 | + protected boolean monitorAll = true; | ||
| 73 | + | ||
| 74 | + @Property(name = "metricNames", value = DEFAULT_METRIC_NAMES, | ||
| 75 | + label = "Names of metric to be monitored; default metric names are 'default'") | ||
| 76 | + protected String metricNames = DEFAULT_METRIC_NAMES; | ||
| 77 | + | ||
| 78 | + @Property(name = "address", value = DEFAULT_ADDRESS, | ||
| 79 | + label = "IP address of ganglia monitoring server; default is localhost") | ||
| 80 | + protected String address = DEFAULT_ADDRESS; | ||
| 81 | + | ||
| 82 | + @Property(name = "port", intValue = DEFAULT_PORT, | ||
| 83 | + label = "Port number of ganglia monitoring server; default is 8649") | ||
| 84 | + protected int port = DEFAULT_PORT; | ||
| 85 | + | ||
| 86 | + @Property(name = "ttl", intValue = DEFAULT_TTL, | ||
| 87 | + label = "TTL value of ganglia monitoring server; default is 1") | ||
| 88 | + protected int ttl = DEFAULT_TTL; | ||
| 89 | + | ||
| 90 | + private ApplicationId appId; | ||
| 91 | + private GMetric ganglia; | ||
| 92 | + private GangliaReporter gangliaReporter; | ||
| 93 | + | ||
| 94 | + @Activate | ||
| 95 | + public void activate() { | ||
| 96 | + cfgService.registerProperties(getClass()); | ||
| 97 | + appId = coreService.registerApplication("org.onosproject.metrics.reporter"); | ||
| 98 | + | ||
| 99 | + startReport(); | ||
| 100 | + log.info("Started"); | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + @Deactivate | ||
| 104 | + public void deactivate() { | ||
| 105 | + cfgService.unregisterProperties(getClass(), false); | ||
| 106 | + | ||
| 107 | + stopReport(); | ||
| 108 | + log.info("Stopped"); | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + @Modified | ||
| 112 | + public void modified(ComponentContext context) { | ||
| 113 | + readComponentConfiguration(context); | ||
| 114 | + stopReport(); | ||
| 115 | + startReport(); | ||
| 116 | + } | ||
| 117 | + | ||
| 118 | + @Override | ||
| 119 | + public void startReport() { | ||
| 120 | + try { | ||
| 121 | + ganglia = new GMetric(address, port, GANGLIA_MODE, ttl); | ||
| 122 | + | ||
| 123 | + MetricRegistry mr = metricsService.getMetricRegistry(); | ||
| 124 | + | ||
| 125 | + gangliaReporter = GangliaReporter.forRegistry(filter(mr)) | ||
| 126 | + .convertRatesTo(TimeUnit.SECONDS) | ||
| 127 | + .convertDurationsTo(TimeUnit.MILLISECONDS) | ||
| 128 | + .build(ganglia); | ||
| 129 | + gangliaReporter.start(REPORT_PERIOD, REPORT_TIME_UNIT); | ||
| 130 | + } catch (IOException e) { | ||
| 131 | + log.error("Fail to connect to given ganglia server!"); | ||
| 132 | + } | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + @Override | ||
| 136 | + public void stopReport() { | ||
| 137 | + gangliaReporter.stop(); | ||
| 138 | + ganglia = null; | ||
| 139 | + gangliaReporter = null; | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + /** | ||
| 143 | + * Filters the metrics to only include a set of the given metrics. | ||
| 144 | + * | ||
| 145 | + * @param metricRegistry original metric registry | ||
| 146 | + * @return filtered metric registry | ||
| 147 | + */ | ||
| 148 | + protected MetricRegistry filter(MetricRegistry metricRegistry) { | ||
| 149 | + if (!monitorAll) { | ||
| 150 | + final MetricRegistry filtered = new MetricRegistry(); | ||
| 151 | + metricRegistry.getNames().stream().filter(name -> | ||
| 152 | + containsName(name, metricNames)).forEach(name -> | ||
| 153 | + filtered.register(name, metricRegistry.getMetrics().get(name))); | ||
| 154 | + return filtered; | ||
| 155 | + } else { | ||
| 156 | + return metricRegistry; | ||
| 157 | + } | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + /** | ||
| 161 | + * Looks up whether the metric name contains the given prefix keywords. | ||
| 162 | + * Note that the keywords are separated with comma as delimiter | ||
| 163 | + * | ||
| 164 | + * @param full the original metric name that to be compared with | ||
| 165 | + * @param prefixes the prefix keywords that are matched against with the metric name | ||
| 166 | + * @return boolean value that denotes whether the metric name starts with the given prefix | ||
| 167 | + */ | ||
| 168 | + protected boolean containsName(String full, String prefixes) { | ||
| 169 | + String[] prefixArray = StringUtils.split(prefixes, ","); | ||
| 170 | + for (String prefix : prefixArray) { | ||
| 171 | + if (StringUtils.startsWith(full, StringUtils.trimToEmpty(prefix))) { | ||
| 172 | + return true; | ||
| 173 | + } | ||
| 174 | + } | ||
| 175 | + return false; | ||
| 176 | + } | ||
| 177 | + | ||
| 178 | + /** | ||
| 179 | + * Extracts properties from the component configuration context. | ||
| 180 | + * | ||
| 181 | + * @param context the component context | ||
| 182 | + */ | ||
| 183 | + private void readComponentConfiguration(ComponentContext context) { | ||
| 184 | + Dictionary<?, ?> properties = context.getProperties(); | ||
| 185 | + | ||
| 186 | + String addressStr = Tools.get(properties, "address"); | ||
| 187 | + address = addressStr != null ? addressStr : DEFAULT_ADDRESS; | ||
| 188 | + log.info("Configured. Ganglia server address is {}", address); | ||
| 189 | + | ||
| 190 | + String metricNameStr = Tools.get(properties, "metricNames"); | ||
| 191 | + metricNames = metricNameStr != null ? metricNameStr : DEFAULT_METRIC_NAMES; | ||
| 192 | + log.info("Configured. Metric name is {}", metricNames); | ||
| 193 | + | ||
| 194 | + Integer portConfigured = Tools.getIntegerProperty(properties, "port"); | ||
| 195 | + if (portConfigured == null) { | ||
| 196 | + port = DEFAULT_PORT; | ||
| 197 | + log.info("Ganglia port is not configured, default value is {}", port); | ||
| 198 | + } else { | ||
| 199 | + port = portConfigured; | ||
| 200 | + log.info("Configured. Ganglia port is configured to {}", port); | ||
| 201 | + } | ||
| 202 | + | ||
| 203 | + Integer ttlConfigured = Tools.getIntegerProperty(properties, "ttl"); | ||
| 204 | + if (ttlConfigured == null) { | ||
| 205 | + ttl = DEFAULT_TTL; | ||
| 206 | + log.info("Ganglia TTL is not configured, default value is {}", ttl); | ||
| 207 | + } else { | ||
| 208 | + ttl = ttlConfigured; | ||
| 209 | + log.info("Configured. Ganglia TTL is configured to {}", ttl); | ||
| 210 | + } | ||
| 211 | + | ||
| 212 | + Boolean monitorAllEnabled = Tools.isPropertyEnabled(properties, "monitorAll"); | ||
| 213 | + if (monitorAllEnabled == null) { | ||
| 214 | + log.info("Monitor all metrics is not configured, " + | ||
| 215 | + "using current value of {}", monitorAll); | ||
| 216 | + } else { | ||
| 217 | + monitorAll = monitorAllEnabled; | ||
| 218 | + log.info("Configured. Monitor all metrics is {}", | ||
| 219 | + monitorAll ? "enabled" : "disabled"); | ||
| 220 | + } | ||
| 221 | + } | ||
| 222 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2016 Open Networking Laboratory | ||
| 3 | + * | ||
| 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 | +package org.onosproject.metrics.reporter; | ||
| 17 | + | ||
| 18 | +/** | ||
| 19 | + * A metric report that reports all metrics value to third party monitor. | ||
| 20 | + */ | ||
| 21 | +public interface MetricsReporter { | ||
| 22 | + | ||
| 23 | + /** | ||
| 24 | + * Starts to report a set of metrics to the monitoring server. | ||
| 25 | + */ | ||
| 26 | + void startReport(); | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * Stops reporting metrics. | ||
| 30 | + */ | ||
| 31 | + void stopReport(); | ||
| 32 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2016 Open Networking Laboratory | ||
| 3 | + * | ||
| 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 | + | ||
| 17 | +/** | ||
| 18 | + * Contains various reporters that backup all of the collected metrics | ||
| 19 | + * to monitoring server. | ||
| 20 | + */ | ||
| 21 | +package org.onosproject.metrics.reporter; |
apps/metricsreporter/src/test/java/org/onosproject/metrics/reporter/GangliaMetricsReporterTest.java
0 → 100644
| 1 | +/* | ||
| 2 | + * Copyright 2016 Open Networking Laboratory | ||
| 3 | + * | ||
| 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 | +package org.onosproject.metrics.reporter; | ||
| 17 | + | ||
| 18 | +import com.codahale.metrics.MetricRegistry; | ||
| 19 | +import com.google.common.collect.ImmutableSet; | ||
| 20 | +import org.junit.Before; | ||
| 21 | +import org.junit.Test; | ||
| 22 | + | ||
| 23 | +import static org.junit.Assert.assertFalse; | ||
| 24 | +import static org.junit.Assert.assertTrue; | ||
| 25 | + | ||
| 26 | +/** | ||
| 27 | + * Unit test for metrics reporter of ganglia. | ||
| 28 | + */ | ||
| 29 | +public class GangliaMetricsReporterTest { | ||
| 30 | + | ||
| 31 | + private GangliaMetricsReporter gmr; | ||
| 32 | + | ||
| 33 | + private static final String METRIC_NAME1 = "consistentMap.onos-app-ids.putIfAbsent"; | ||
| 34 | + private static final String METRIC_NAME2 = "consistentMap.onos-hosts.entrySet"; | ||
| 35 | + private static final String METRIC_NAME3 = "clusterCommunication.endpoint.*"; | ||
| 36 | + private static final String METRIC_NAME4 = "atomicCounter.onos-app-id-counter.*"; | ||
| 37 | + | ||
| 38 | + private static final String PREFIXES1 = "consistentMap"; | ||
| 39 | + private static final String PREFIXES2 = "topology"; | ||
| 40 | + private static final String PREFIXES3 = "consistentMap.onos-app-ids"; | ||
| 41 | + private static final String PREFIXES4 = "consistentMap, clusterCommunication, atomicCounter"; | ||
| 42 | + | ||
| 43 | + /** | ||
| 44 | + * Initializes ganglia metrics reporter instance. | ||
| 45 | + */ | ||
| 46 | + @Before | ||
| 47 | + public void setUp() { | ||
| 48 | + gmr = new GangliaMetricsReporter(); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + /** | ||
| 52 | + * Tests whether the containsName method can always return the correct result | ||
| 53 | + * with the given metric name and a set of prefixes. | ||
| 54 | + */ | ||
| 55 | + @Test | ||
| 56 | + public void testContainsName() { | ||
| 57 | + assertTrue(gmr.containsName(METRIC_NAME1, PREFIXES1)); | ||
| 58 | + assertTrue(gmr.containsName(METRIC_NAME1, PREFIXES3)); | ||
| 59 | + assertTrue(gmr.containsName(METRIC_NAME1, PREFIXES4)); | ||
| 60 | + assertTrue(gmr.containsName(METRIC_NAME2, PREFIXES4)); | ||
| 61 | + assertTrue(gmr.containsName(METRIC_NAME3, PREFIXES4)); | ||
| 62 | + assertTrue(gmr.containsName(METRIC_NAME4, PREFIXES4)); | ||
| 63 | + assertFalse(gmr.containsName(METRIC_NAME1, PREFIXES2)); | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + /** | ||
| 67 | + * Tests whether the filter method can always return the correct result. | ||
| 68 | + */ | ||
| 69 | + @Test | ||
| 70 | + public void testFilter() { | ||
| 71 | + MetricRegistry filtered; | ||
| 72 | + MetricRegistry full = new MetricRegistry(); | ||
| 73 | + full.meter(METRIC_NAME1); | ||
| 74 | + full.meter(METRIC_NAME2); | ||
| 75 | + full.meter(METRIC_NAME3); | ||
| 76 | + full.meter(METRIC_NAME4); | ||
| 77 | + | ||
| 78 | + gmr.monitorAll = true; | ||
| 79 | + filtered = gmr.filter(full); | ||
| 80 | + | ||
| 81 | + assertTrue(filtered.getNames() | ||
| 82 | + .containsAll(ImmutableSet.of(METRIC_NAME1, METRIC_NAME2, | ||
| 83 | + METRIC_NAME3, METRIC_NAME4))); | ||
| 84 | + | ||
| 85 | + gmr.monitorAll = false; | ||
| 86 | + gmr.metricNames = PREFIXES1; | ||
| 87 | + filtered = gmr.filter(full); | ||
| 88 | + | ||
| 89 | + assertTrue(filtered.getNames() | ||
| 90 | + .containsAll(ImmutableSet.of(METRIC_NAME1, METRIC_NAME2))); | ||
| 91 | + assertFalse(filtered.getNames() | ||
| 92 | + .containsAll(ImmutableSet.of(METRIC_NAME3, METRIC_NAME4))); | ||
| 93 | + | ||
| 94 | + gmr.metricNames = PREFIXES2; | ||
| 95 | + filtered = gmr.filter(full); | ||
| 96 | + | ||
| 97 | + assertFalse(filtered.getNames().containsAll(ImmutableSet.of(METRIC_NAME1))); | ||
| 98 | + | ||
| 99 | + gmr.metricNames = PREFIXES3; | ||
| 100 | + filtered = gmr.filter(full); | ||
| 101 | + | ||
| 102 | + assertTrue(filtered.getNames().containsAll(ImmutableSet.of(METRIC_NAME1))); | ||
| 103 | + assertFalse(filtered.getNames().containsAll(ImmutableSet.of(METRIC_NAME2))); | ||
| 104 | + | ||
| 105 | + gmr.metricNames = PREFIXES4; | ||
| 106 | + filtered = gmr.filter(full); | ||
| 107 | + | ||
| 108 | + assertTrue(filtered.getNames() | ||
| 109 | + .containsAll(ImmutableSet.of(METRIC_NAME1, METRIC_NAME2, | ||
| 110 | + METRIC_NAME3, METRIC_NAME4))); | ||
| 111 | + } | ||
| 112 | +} |
| ... | @@ -38,6 +38,7 @@ | ... | @@ -38,6 +38,7 @@ |
| 38 | <module>sdnip</module> | 38 | <module>sdnip</module> |
| 39 | <module>optical</module> | 39 | <module>optical</module> |
| 40 | <module>metrics</module> | 40 | <module>metrics</module> |
| 41 | + <module>metricsreporter</module> | ||
| 41 | <module>routing</module> | 42 | <module>routing</module> |
| 42 | <module>routing-api</module> | 43 | <module>routing-api</module> |
| 43 | <module>reactive-routing</module> | 44 | <module>reactive-routing</module> | ... | ... |
| ... | @@ -36,8 +36,8 @@ | ... | @@ -36,8 +36,8 @@ |
| 36 | 36 | ||
| 37 | <bundle>mvn:joda-time/joda-time/2.9</bundle> | 37 | <bundle>mvn:joda-time/joda-time/2.9</bundle> |
| 38 | 38 | ||
| 39 | - <bundle>mvn:io.dropwizard.metrics/metrics-core/3.1.0</bundle> | 39 | + <bundle>mvn:io.dropwizard.metrics/metrics-core/3.1.2</bundle> |
| 40 | - <bundle>mvn:io.dropwizard.metrics/metrics-json/3.1.0</bundle> | 40 | + <bundle>mvn:io.dropwizard.metrics/metrics-json/3.1.2</bundle> |
| 41 | <bundle>mvn:com.eclipsesource.minimal-json/minimal-json/0.9.1</bundle> | 41 | <bundle>mvn:com.eclipsesource.minimal-json/minimal-json/0.9.1</bundle> |
| 42 | 42 | ||
| 43 | <bundle>mvn:com.esotericsoftware/kryo/3.0.0</bundle> | 43 | <bundle>mvn:com.esotericsoftware/kryo/3.0.0</bundle> | ... | ... |
| ... | @@ -31,6 +31,10 @@ | ... | @@ -31,6 +31,10 @@ |
| 31 | 31 | ||
| 32 | <description>Miscellaneous ON.Lab utilities</description> | 32 | <description>Miscellaneous ON.Lab utilities</description> |
| 33 | 33 | ||
| 34 | + <properties> | ||
| 35 | + <metrics.version>3.1.2</metrics.version> | ||
| 36 | + </properties> | ||
| 37 | + | ||
| 34 | <dependencies> | 38 | <dependencies> |
| 35 | <dependency> | 39 | <dependency> |
| 36 | <groupId>com.google.guava</groupId> | 40 | <groupId>com.google.guava</groupId> |
| ... | @@ -71,12 +75,12 @@ | ... | @@ -71,12 +75,12 @@ |
| 71 | <dependency> | 75 | <dependency> |
| 72 | <groupId>io.dropwizard.metrics</groupId> | 76 | <groupId>io.dropwizard.metrics</groupId> |
| 73 | <artifactId>metrics-core</artifactId> | 77 | <artifactId>metrics-core</artifactId> |
| 74 | - <version>3.1.0</version> | 78 | + <version>${metrics.version}</version> |
| 75 | </dependency> | 79 | </dependency> |
| 76 | <dependency> | 80 | <dependency> |
| 77 | <groupId>io.dropwizard.metrics</groupId> | 81 | <groupId>io.dropwizard.metrics</groupId> |
| 78 | <artifactId>metrics-json</artifactId> | 82 | <artifactId>metrics-json</artifactId> |
| 79 | - <version>3.1.0</version> | 83 | + <version>${metrics.version}</version> |
| 80 | </dependency> | 84 | </dependency> |
| 81 | <dependency> | 85 | <dependency> |
| 82 | <groupId>org.apache.felix</groupId> | 86 | <groupId>org.apache.felix</groupId> | ... | ... |
| ... | @@ -102,6 +102,16 @@ public class MetricsManager implements MetricsService { | ... | @@ -102,6 +102,16 @@ public class MetricsManager implements MetricsService { |
| 102 | } | 102 | } |
| 103 | 103 | ||
| 104 | /** | 104 | /** |
| 105 | + * Fetches existing metric registry. | ||
| 106 | + * | ||
| 107 | + * @return metric registry | ||
| 108 | + */ | ||
| 109 | + @Override | ||
| 110 | + public MetricRegistry getMetricRegistry() { | ||
| 111 | + return metricsRegistry; | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + /** | ||
| 105 | * Generates a name for a Metric from its component and feature. | 115 | * Generates a name for a Metric from its component and feature. |
| 106 | * | 116 | * |
| 107 | * @param component component the metric is defined in | 117 | * @param component component the metric is defined in | ... | ... |
| ... | @@ -23,6 +23,7 @@ import com.codahale.metrics.Histogram; | ... | @@ -23,6 +23,7 @@ import com.codahale.metrics.Histogram; |
| 23 | import com.codahale.metrics.Meter; | 23 | import com.codahale.metrics.Meter; |
| 24 | import com.codahale.metrics.Metric; | 24 | import com.codahale.metrics.Metric; |
| 25 | import com.codahale.metrics.MetricFilter; | 25 | import com.codahale.metrics.MetricFilter; |
| 26 | +import com.codahale.metrics.MetricRegistry; | ||
| 26 | import com.codahale.metrics.Timer; | 27 | import com.codahale.metrics.Timer; |
| 27 | 28 | ||
| 28 | /** | 29 | /** |
| ... | @@ -39,6 +40,13 @@ public interface MetricsService { | ... | @@ -39,6 +40,13 @@ public interface MetricsService { |
| 39 | MetricsComponent registerComponent(String name); | 40 | MetricsComponent registerComponent(String name); |
| 40 | 41 | ||
| 41 | /** | 42 | /** |
| 43 | + * Fetches existing metric registry. | ||
| 44 | + * | ||
| 45 | + * @return metric registry | ||
| 46 | + */ | ||
| 47 | + MetricRegistry getMetricRegistry(); | ||
| 48 | + | ||
| 49 | + /** | ||
| 42 | * Creates a Counter metric. | 50 | * Creates a Counter metric. |
| 43 | * | 51 | * |
| 44 | * @param component component the Counter is defined in | 52 | * @param component component the Counter is defined in | ... | ... |
| ... | @@ -19,6 +19,7 @@ package org.onlab.metrics; | ... | @@ -19,6 +19,7 @@ package org.onlab.metrics; |
| 19 | import com.codahale.metrics.Counter; | 19 | import com.codahale.metrics.Counter; |
| 20 | import com.codahale.metrics.Histogram; | 20 | import com.codahale.metrics.Histogram; |
| 21 | import com.codahale.metrics.Meter; | 21 | import com.codahale.metrics.Meter; |
| 22 | +import com.codahale.metrics.MetricRegistry; | ||
| 22 | import com.codahale.metrics.Timer; | 23 | import com.codahale.metrics.Timer; |
| 23 | import com.codahale.metrics.Gauge; | 24 | import com.codahale.metrics.Gauge; |
| 24 | import com.codahale.metrics.Metric; | 25 | import com.codahale.metrics.Metric; |
| ... | @@ -39,6 +40,11 @@ public class MetricsServiceAdapter implements MetricsService { | ... | @@ -39,6 +40,11 @@ public class MetricsServiceAdapter implements MetricsService { |
| 39 | } | 40 | } |
| 40 | 41 | ||
| 41 | @Override | 42 | @Override |
| 43 | + public MetricRegistry getMetricRegistry() { | ||
| 44 | + return null; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + @Override | ||
| 42 | public Counter createCounter(MetricsComponent component, | 48 | public Counter createCounter(MetricsComponent component, |
| 43 | MetricsFeature feature, String metricName) { | 49 | MetricsFeature feature, String metricName) { |
| 44 | return null; | 50 | return null; | ... | ... |
-
Please register or login to post a comment