Jian Li
Committed by Gerrit Code Review

[ONOS-4176] Implement influxdb reporter for backing up metrics

Change-Id: If899955c06cc5a5619a529178be8eb1bffa5b498
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-influxdb/${project.version}</bundle>
22 + <bundle>wrap:mvn:com.izettle/metrics-influxdb/1.1.1$Bundle-SymbolicName=metrics-influxdb&amp;Bundle-Version=1.1.1</bundle>
23 + <bundle>mvn:commons-codec/commons-codec/1.10</bundle>
24 + </feature>
25 +</features>
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-influxdb</artifactId>
30 + <packaging>bundle</packaging>
31 +
32 + <description>Performance metric service reporter and retriever for influxDB</description>
33 +
34 + <properties>
35 + <onos.app.name>org.onosproject.influxdbmetrics</onos.app.name>
36 + <onos.app.title>InfluxDB Report and Query App</onos.app.title>
37 + <onos.app.category>Monitoring</onos.app.category>
38 + <onos.app.url>http://onosproject.org</onos.app.url>
39 + </properties>
40 +
41 + <dependencies>
42 + <dependency>
43 + <groupId>org.osgi</groupId>
44 + <artifactId>org.osgi.compendium</artifactId>
45 + </dependency>
46 + <dependency>
47 + <groupId>org.onosproject</groupId>
48 + <artifactId>onlab-misc</artifactId>
49 + </dependency>
50 + <dependency>
51 + <groupId>org.onosproject</groupId>
52 + <artifactId>onos-cli</artifactId>
53 + <version>${project.version}</version>
54 + </dependency>
55 + <dependency>
56 + <groupId>org.apache.karaf.shell</groupId>
57 + <artifactId>org.apache.karaf.shell.console</artifactId>
58 + </dependency>
59 + <dependency>
60 + <groupId>org.apache.felix</groupId>
61 + <artifactId>org.apache.felix.scr.annotations</artifactId>
62 + </dependency>
63 + <dependency>
64 + <groupId>io.dropwizard.metrics</groupId>
65 + <artifactId>metrics-core</artifactId>
66 + <version>3.1.2</version>
67 + </dependency>
68 + <dependency>
69 + <groupId>com.izettle</groupId>
70 + <artifactId>metrics-influxdb</artifactId>
71 + <version>1.1.1</version>
72 + </dependency>
73 + <dependency>
74 + <groupId>org.onosproject</groupId>
75 + <artifactId>onos-api</artifactId>
76 + <version>${project.version}</version>
77 + <classifier>tests</classifier>
78 + <scope>test</scope>
79 + </dependency>
80 + <dependency>
81 + <groupId>org.onosproject</groupId>
82 + <artifactId>onlab-junit</artifactId>
83 + <scope>test</scope>
84 + </dependency>
85 + <dependency>
86 + <groupId>org.easymock</groupId>
87 + <artifactId>easymock</artifactId>
88 + <scope>test</scope>
89 + </dependency>
90 + </dependencies>
91 +
92 +</project>
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.influxdbmetrics;
17 +
18 +import com.codahale.metrics.MetricRegistry;
19 +import com.izettle.metrics.influxdb.InfluxDbHttpSender;
20 +import com.izettle.metrics.influxdb.InfluxDbReporter;
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.cluster.ClusterService;
33 +import org.onosproject.cluster.ControllerNode;
34 +import org.onosproject.core.ApplicationId;
35 +import org.onosproject.core.CoreService;
36 +import org.osgi.service.component.ComponentContext;
37 +import org.slf4j.Logger;
38 +
39 +import java.util.Dictionary;
40 +import java.util.concurrent.TimeUnit;
41 +
42 +import static org.slf4j.LoggerFactory.getLogger;
43 +
44 +/**
45 + * A Metric reporter that reports all metrics value to influxDB server.
46 + */
47 +@Component(immediate = true)
48 +public class InfluxDbMetricsReporter {
49 + private final Logger log = getLogger(getClass());
50 +
51 + private static final int REPORT_PERIOD = 1;
52 + private static final TimeUnit REPORT_TIME_UNIT = TimeUnit.MINUTES;
53 +
54 + private static final String DEFAULT_PROTOCOL = "http";
55 + private static final String DEFAULT_ADDRESS = "localhost";
56 + private static final int DEFAULT_PORT = 8086;
57 + private static final String DEFAULT_METRIC_NAMES = "default";
58 + private static final String DEFAULT_DATABASE = "onos";
59 + private static final String DEFAULT_USERNAME = "onos";
60 + private static final String DEFAULT_PASSWORD = "onos.password";
61 + private static final String SEPARATOR = ":";
62 + private static final int DEFAULT_CONN_TIMEOUT = 1000;
63 + private static final int DEFAULT_READ_TIMEOUT = 1000;
64 +
65 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 + protected CoreService coreService;
67 +
68 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 + protected MetricsService metricsService;
70 +
71 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 + protected ComponentConfigService cfgService;
73 +
74 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 + protected ClusterService clusterService;
76 +
77 + @Property(name = "monitorAll", boolValue = true,
78 + label = "Enable to monitor all of metrics stored in metric registry " +
79 + "default is true")
80 + protected boolean monitorAll = true;
81 +
82 + @Property(name = "metricNames", value = DEFAULT_METRIC_NAMES,
83 + label = "Names of metric to be monitored in third party monitoring " +
84 + "server; default metric names are 'default'")
85 + protected String metricNames = DEFAULT_METRIC_NAMES;
86 +
87 + @Property(name = "address", value = DEFAULT_ADDRESS,
88 + label = "IP address of influxDB server; " +
89 + "default is localhost")
90 + protected String address = DEFAULT_ADDRESS;
91 +
92 + @Property(name = "port", intValue = DEFAULT_PORT,
93 + label = "Port number of influxDB server; " +
94 + "default is 8086")
95 + protected int port = DEFAULT_PORT;
96 +
97 + @Property(name = "database", value = DEFAULT_DATABASE,
98 + label = "Database name of influxDB server; " +
99 + "default is onos")
100 + protected String database = DEFAULT_DATABASE;
101 +
102 + @Property(name = "username", value = DEFAULT_USERNAME,
103 + label = "Username of influxDB server; default is onos")
104 + protected String username = DEFAULT_USERNAME;
105 +
106 + @Property(name = "password", value = DEFAULT_PASSWORD,
107 + label = "Password of influxDB server; default is onos.password")
108 + protected String password = DEFAULT_PASSWORD;
109 +
110 + private ApplicationId appId;
111 + private InfluxDbReporter influxDbReporter;
112 + private InfluxDbHttpSender influxDbHttpSender;
113 +
114 + @Activate
115 + public void activate() {
116 + cfgService.registerProperties(getClass());
117 + appId = coreService.registerApplication("org.onosproject.influxdbmetrics");
118 +
119 + startReport();
120 + log.info("Started");
121 + }
122 +
123 + @Deactivate
124 + public void deactivate() {
125 + cfgService.unregisterProperties(getClass(), false);
126 +
127 + stopReport();
128 + log.info("Stopped");
129 + }
130 +
131 + @Modified
132 + public void modified(ComponentContext context) {
133 + readComponentConfiguration(context);
134 + restartReport();
135 + }
136 +
137 + protected void startReport() {
138 + try {
139 + influxDbHttpSender = new InfluxDbHttpSender(DEFAULT_PROTOCOL, address,
140 + port, database, username + SEPARATOR + password, REPORT_TIME_UNIT,
141 + DEFAULT_CONN_TIMEOUT, DEFAULT_READ_TIMEOUT);
142 + MetricRegistry mr = metricsService.getMetricRegistry();
143 + influxDbReporter = InfluxDbReporter.forRegistry(addHostPrefix(filter(mr)))
144 + .convertRatesTo(TimeUnit.SECONDS)
145 + .convertDurationsTo(TimeUnit.MILLISECONDS)
146 + .build(influxDbHttpSender);
147 + influxDbReporter.start(REPORT_PERIOD, REPORT_TIME_UNIT);
148 + log.info("Start to report metrics to influxDB.");
149 + } catch (Exception e) {
150 + log.error("Fail to connect to given influxDB server!");
151 + }
152 + }
153 +
154 + protected void stopReport() {
155 + influxDbReporter.stop();
156 + influxDbHttpSender = null;
157 + influxDbReporter = null;
158 + log.info("Stop reporting metrics to influxDB.");
159 + }
160 +
161 + protected void restartReport() {
162 + stopReport();
163 + startReport();
164 + }
165 +
166 + /**
167 + * Filters the metrics to only include a set of the given metrics.
168 + *
169 + * @param metricRegistry original metric registry
170 + * @return filtered metric registry
171 + */
172 + protected MetricRegistry filter(MetricRegistry metricRegistry) {
173 + if (!monitorAll) {
174 + final MetricRegistry filtered = new MetricRegistry();
175 + metricRegistry.getNames().stream().filter(name ->
176 + containsName(name, metricNames)).forEach(name ->
177 + filtered.register(name, metricRegistry.getMetrics().get(name)));
178 + return filtered;
179 + } else {
180 + return metricRegistry;
181 + }
182 + }
183 +
184 + /**
185 + * Appends node IP prefix to all performance metrics.
186 + *
187 + * @param metricRegistry original metric registry
188 + * @return prefix added metric registry
189 + */
190 + protected MetricRegistry addHostPrefix(MetricRegistry metricRegistry) {
191 + MetricRegistry moddedRegistry = new MetricRegistry();
192 + ControllerNode node = clusterService.getLocalNode();
193 + String prefix = node.id().id() + ".";
194 + metricRegistry.getNames().stream().forEach(name ->
195 + moddedRegistry.register(prefix + name, metricRegistry.getMetrics().get(name)));
196 +
197 + return moddedRegistry;
198 + }
199 +
200 + /**
201 + * Looks up whether the metric name contains the given prefix keywords.
202 + * Note that the keywords are separated with comma as delimiter.
203 + *
204 + * @param full the original metric name that to be compared with
205 + * @param prefixes the prefix keywords that are matched against with the metric name
206 + * @return boolean value that denotes whether the metric name starts with the given prefix
207 + */
208 + protected boolean containsName(String full, String prefixes) {
209 + String[] prefixArray = StringUtils.split(prefixes, ",");
210 + for (String prefix : prefixArray) {
211 + if (StringUtils.startsWith(full, StringUtils.trimToEmpty(prefix))) {
212 + return true;
213 + }
214 + }
215 + return false;
216 + }
217 +
218 + /**
219 + * Extracts properties from the component configuration context.
220 + *
221 + * @param context the component context
222 + */
223 + private void readComponentConfiguration(ComponentContext context) {
224 + Dictionary<?, ?> properties = context.getProperties();
225 +
226 + String addressStr = Tools.get(properties, "address");
227 + address = addressStr != null ? addressStr : DEFAULT_ADDRESS;
228 + log.info("Configured. InfluxDB server address is {}", address);
229 +
230 + String metricNameStr = Tools.get(properties, "metricNames");
231 + metricNames = metricNameStr != null ? metricNameStr : DEFAULT_METRIC_NAMES;
232 + log.info("Configured. Metric name is {}", metricNames);
233 +
234 + String databaseStr = Tools.get(properties, "database");
235 + database = databaseStr != null ? databaseStr : DEFAULT_DATABASE;
236 + log.info("Configured. InfluxDB server database is {}", database);
237 +
238 + String usernameStr = Tools.get(properties, "username");
239 + username = usernameStr != null ? usernameStr : DEFAULT_USERNAME;
240 + log.info("Configured. InfluxDB server username is {}", username);
241 +
242 + String passwordStr = Tools.get(properties, "password");
243 + password = passwordStr != null ? passwordStr : DEFAULT_PASSWORD;
244 + log.info("Configured. InfluxDB server password is {}", password);
245 +
246 + Integer portConfigured = Tools.getIntegerProperty(properties, "port");
247 + if (portConfigured == null) {
248 + port = DEFAULT_PORT;
249 + log.info("InfluxDB port is not configured, default value is {}", port);
250 + } else {
251 + port = portConfigured;
252 + log.info("Configured. InfluxDB port is configured to {}", port);
253 + }
254 +
255 + Boolean monitorAllEnabled = Tools.isPropertyEnabled(properties, "monitorAll");
256 + if (monitorAllEnabled == null) {
257 + log.info("Monitor all metrics is not configured, " +
258 + "using current value of {}", monitorAll);
259 + } else {
260 + monitorAll = monitorAllEnabled;
261 + log.info("Configured. Monitor all metrics is {}",
262 + monitorAll ? "enabled" : "disabled");
263 + }
264 + }
265 +}
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 + * Application that provides capability to report and retrieve metrics
19 + * to and from influx database.
20 + */
21 +package org.onosproject.influxdbmetrics;
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.influxdbmetrics;
17 +
18 +import com.codahale.metrics.Counter;
19 +import com.codahale.metrics.Gauge;
20 +import com.codahale.metrics.Histogram;
21 +import com.codahale.metrics.Meter;
22 +import com.codahale.metrics.Metric;
23 +import com.codahale.metrics.MetricFilter;
24 +import com.codahale.metrics.MetricRegistry;
25 +import com.codahale.metrics.Timer;
26 +import org.junit.Before;
27 +import org.onlab.metrics.MetricsComponent;
28 +import org.onlab.metrics.MetricsFeature;
29 +import org.onlab.metrics.MetricsService;
30 +import org.onosproject.cfg.ComponentConfigAdapter;
31 +import org.onosproject.cluster.ClusterServiceAdapter;
32 +import org.onosproject.core.CoreServiceAdapter;
33 +
34 +import java.util.Map;
35 +
36 +/**
37 + * Unit tests for influxDB metrics reporter.
38 + */
39 +public class InfluxDbMetricsReporterTest {
40 +
41 + private InfluxDbMetricsReporter influxReporter;
42 +
43 + /**
44 + * Sets up the services required by influxDB metrics reporter.
45 + */
46 + @Before
47 + public void setUp() {
48 + influxReporter = new InfluxDbMetricsReporter();
49 + influxReporter.coreService = new CoreServiceAdapter();
50 + influxReporter.cfgService = new ComponentConfigAdapter();
51 + influxReporter.clusterService = new ClusterServiceAdapter();
52 + influxReporter.metricsService = new TestMetricsService();
53 + influxReporter.activate();
54 + }
55 +
56 + /**
57 + * Tears down influxDB metrics reporter.
58 + */
59 + public void tearDown() {
60 + influxReporter.deactivate();
61 + }
62 +
63 + private class TestMetricsService implements MetricsService {
64 +
65 + @Override
66 + public MetricsComponent registerComponent(String name) {
67 + return null;
68 + }
69 +
70 + @Override
71 + public MetricRegistry getMetricRegistry() {
72 + return null;
73 + }
74 +
75 + @Override
76 + public Counter createCounter(MetricsComponent component,
77 + MetricsFeature feature, String metricName) {
78 + return null;
79 + }
80 +
81 + @Override
82 + public Histogram createHistogram(MetricsComponent component,
83 + MetricsFeature feature, String metricName) {
84 + return null;
85 + }
86 +
87 + @Override
88 + public Timer createTimer(MetricsComponent component,
89 + MetricsFeature feature, String metricName) {
90 + return null;
91 + }
92 +
93 + @Override
94 + public Meter createMeter(MetricsComponent component,
95 + MetricsFeature feature, String metricName) {
96 + return null;
97 + }
98 +
99 + @Override
100 + public <T extends Metric> T registerMetric(MetricsComponent component,
101 + MetricsFeature feature,
102 + String metricName, T metric) {
103 + return null;
104 + }
105 +
106 + @Override
107 + public boolean removeMetric(MetricsComponent component,
108 + MetricsFeature feature, String metricName) {
109 + return false;
110 + }
111 +
112 + @Override
113 + public Map<String, Timer> getTimers(MetricFilter filter) {
114 + return null;
115 + }
116 +
117 + @Override
118 + public Map<String, Gauge> getGauges(MetricFilter filter) {
119 + return null;
120 + }
121 +
122 + @Override
123 + public Map<String, Counter> getCounters(MetricFilter filter) {
124 + return null;
125 + }
126 +
127 + @Override
128 + public Map<String, Meter> getMeters(MetricFilter filter) {
129 + return null;
130 + }
131 +
132 + @Override
133 + public Map<String, Histogram> getHistograms(MetricFilter filter) {
134 + return null;
135 + }
136 +
137 + @Override
138 + public Map<String, Metric> getMetrics() {
139 + return null;
140 + }
141 +
142 + @Override
143 + public void removeMatching(MetricFilter filter) {
144 + }
145 + }
146 +}
...@@ -70,6 +70,7 @@ ...@@ -70,6 +70,7 @@
70 <module>openstacknode</module> 70 <module>openstacknode</module>
71 <module>openstacknetworking</module> 71 <module>openstacknetworking</module>
72 <module>openstackinterface</module> 72 <module>openstackinterface</module>
73 + <module>influxdbmetrics</module>
73 </modules> 74 </modules>
74 75
75 <properties> 76 <properties>
......