Skip to content

Commit ff99ec8

Browse files
authored
Add Support for InfluxDB Cloud 2 (#4)
* add support for influxdb cloud * version to 4.1.0
1 parent 6797672 commit ff99ec8

File tree

8 files changed

+278
-50
lines changed

8 files changed

+278
-50
lines changed

README.adoc

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
*Extension Type:* Monitoring
1010

11-
*Version:* 4.0.0
11+
*Version:* 4.1.0
1212

1313
*License:* Apache License 2.0
1414

@@ -33,16 +33,18 @@ The InfluxDB Monitoring extension uses its own configuration file `influxdb.prop
3333
|===
3434
| Config name | Required | Description | Default
3535

36-
| mode | no | The mode configured for the InfluxDB sender. Possibilities are: http, tcp, udp | http
36+
| mode | no | The mode configured for the InfluxDB sender. Possibilities are: http, tcp, udp, cloud | http
3737
| host | yes | The host name of the InfluxDB instance. | -
3838
| port | yes | The port number the InfluxDB instance is listening. | 8086
3939
| protocol | no | The protocol the InfluxDB sender uses in http mode. | http
40-
| auth | no | The authorization string to be used to connect to InfluxDB, of format username:password. | -
40+
| auth | no | The authorization string to be used to connect to InfluxDB, of format username:password. If mode "cloud" is used, the token must be passed here| -
4141
| prefix | no | The measurement prefix. | -
4242
| database | no | The database name. | hivemq
4343
| reportingInterval | no | The reporting interval in seconds. | 1
4444
| connectTimeout | no | The connect and read timeout in seconds. | 5000
4545
| tags | no | The tags for each metric. Listed as a semicolon ( `;` ) separated list. | -
46+
| organization | only for mode: "cloud" | The organization to push data to | -
47+
| bucket | only for mode: "cloud" | The bucket to push data to | -
4648

4749
|===
4850

pom.xml

+79-45
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@
2323
<artifactId>hivemq-influxdb-extension</artifactId>
2424
<properties>
2525
<extension.name>InfluxDB Monitoring Extension</extension.name>
26+
<extension.author>HiveMQ</extension.author>
27+
28+
<asciidoctor.version>1.5.2.1</asciidoctor.version>
29+
<output.dir>${basedir}</output.dir>
30+
<source.document.name>README.adoc</source.document.name>
2631
</properties>
27-
<version>4.0.0</version>
32+
<version>4.1.0</version>
2833

2934
<dependencies>
3035
<dependency>
@@ -65,23 +70,29 @@
6570
<artifactId>slf4j-api</artifactId>
6671
<version>1.7.25</version>
6772
</dependency>
73+
<dependency>
74+
<groupId>com.github.tomakehurst</groupId>
75+
<artifactId>wiremock-jre8-standalone</artifactId>
76+
<version>2.25.1</version>
77+
<scope>test</scope>
78+
</dependency>
6879
</dependencies>
6980

7081
<build>
7182
<plugins>
7283
<plugin>
7384
<groupId>org.apache.maven.plugins</groupId>
7485
<artifactId>maven-compiler-plugin</artifactId>
75-
<version>3.1</version>
86+
<version>3.8.0</version>
7687
<configuration>
77-
<source>10</source>
78-
<target>10</target>
88+
<source>11</source>
89+
<target>11</target>
7990
</configuration>
8091
</plugin>
8192
<plugin>
8293
<groupId>org.apache.maven.plugins</groupId>
8394
<artifactId>maven-shade-plugin</artifactId>
84-
<version>2.1</version>
95+
<version>3.1.1</version>
8596
<executions>
8697
<execution>
8798
<phase>package</phase>
@@ -91,67 +102,90 @@
91102
<configuration>
92103
<artifactSet>
93104
<excludes>
94-
<exclude>com.google.guava:*</exclude>
105+
<exclude>com.hivemq:hivemq-extension-sdk</exclude>
95106
<exclude>org.slf4j:*</exclude>
96107
<exclude>ch.qos.logback:*</exclude>
97-
<exclude>javax.ws.rs:*</exclude>
98108
<exclude>javax.servlet:*</exclude>
99109
</excludes>
100110
</artifactSet>
111+
<filters>
112+
<filter>
113+
<artifact>*:*</artifact>
114+
<excludes>
115+
<exclude>META-INF/*.SF</exclude>
116+
<exclude>META-INF/*.DSA</exclude>
117+
<exclude>META-INF/*.RSA</exclude>
118+
</excludes>
119+
</filter>
120+
</filters>
121+
</configuration>
122+
</execution>
123+
</executions>
124+
</plugin>
125+
<plugin>
126+
<artifactId>maven-assembly-plugin</artifactId>
127+
<executions>
128+
<execution>
129+
<id>assembly</id>
130+
<phase>package</phase>
131+
<goals>
132+
<goal>single</goal>
133+
</goals>
134+
<configuration>
135+
<descriptors>
136+
<descriptor>assembly.xml</descriptor>
137+
</descriptors>
138+
</configuration>
139+
</execution>
140+
</executions>
141+
</plugin>
142+
<plugin>
143+
<groupId>org.asciidoctor</groupId>
144+
<artifactId>asciidoctor-maven-plugin</artifactId>
145+
<version>${asciidoctor.version}</version>
146+
<executions>
147+
<execution>
148+
<id>output-html</id>
149+
<phase>generate-resources</phase>
150+
<goals>
151+
<goal>process-asciidoc</goal>
152+
</goals>
153+
<configuration>
154+
<backend>html</backend>
155+
<doctype>xhtml</doctype>
156+
<headerFooter>true</headerFooter>
157+
<sourceDirectory>${basedir}</sourceDirectory>
158+
<outputDirectory>${output.dir}/target</outputDirectory>
159+
<sourceDocumentName>${source.document.name}</sourceDocumentName>
160+
<attributes>
161+
<icons>font</icons>
162+
<toc2/>
163+
</attributes>
101164
</configuration>
102165
</execution>
103166
</executions>
104167
</plugin>
105168
</plugins>
106169
</build>
107-
108170
<profiles>
109171
<profile>
110-
<activation>
111-
<activeByDefault>true</activeByDefault>
112-
</activation>
113-
<id>Packaging</id>
172+
<id>RunWithHiveMQ</id>
114173
<build>
115174
<plugins>
116175
<plugin>
117-
<artifactId>maven-assembly-plugin</artifactId>
176+
<groupId>com.hivemq</groupId>
177+
<artifactId>hivemq-maven-plugin</artifactId>
178+
<version>4.0.2</version>
118179
<executions>
119180
<execution>
120-
<id>assembly</id>
181+
<id>hivemq</id>
121182
<phase>package</phase>
122183
<goals>
123-
<goal>single</goal>
124-
</goals>
125-
<configuration>
126-
<descriptors>
127-
<descriptor>assembly.xml</descriptor>
128-
</descriptors>
129-
</configuration>
130-
</execution>
131-
</executions>
132-
</plugin>
133-
<plugin>
134-
<groupId>org.asciidoctor</groupId>
135-
<artifactId>asciidoctor-maven-plugin</artifactId>
136-
<version>1.5.2.1</version>
137-
<executions>
138-
<execution>
139-
<id>output-html</id>
140-
<phase>generate-resources</phase>
141-
<goals>
142-
<goal>process-asciidoc</goal>
184+
<goal>hivemq</goal>
143185
</goals>
144186
<configuration>
145-
<backend>html</backend>
146-
<doctype>xhtml</doctype>
147-
<headerFooter>true</headerFooter>
148-
<sourceDirectory>${basedir}</sourceDirectory>
149-
<outputDirectory>${basedir}/target</outputDirectory>
150-
<sourceDocumentName>README.adoc</sourceDocumentName>
151-
<attributes>
152-
<icons>font</icons>
153-
<toc2/>
154-
</attributes>
187+
<hiveMQDir><!-- YOUR_HIVEMQ_HOME_FOLDER --></hiveMQDir>
188+
<debugMode>SERVER</debugMode>
155189
</configuration>
156190
</execution>
157191
</executions>
@@ -161,4 +195,4 @@
161195
</profile>
162196
</profiles>
163197

164-
</project>
198+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2018 dc-square GmbH
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 com.hivemq.extensions;
17+
18+
import com.izettle.metrics.influxdb.InfluxDbHttpSender;
19+
import com.izettle.metrics.influxdb.utils.TimeUtils;
20+
21+
import java.io.IOException;
22+
import java.io.OutputStream;
23+
import java.net.HttpURLConnection;
24+
import java.net.URL;
25+
import java.net.URLEncoder;
26+
import java.nio.charset.StandardCharsets;
27+
import java.util.concurrent.TimeUnit;
28+
import java.util.zip.GZIPOutputStream;
29+
30+
/**
31+
* Sender for InfluxDB Cloud.
32+
*
33+
* @author Simon Baier
34+
*/
35+
public class InfluxDbCloudSender extends InfluxDbHttpSender {
36+
private final String authToken;
37+
private final int connectTimeout;
38+
private final int readTimeout;
39+
private final URL url;
40+
41+
public InfluxDbCloudSender(String protocol, String host, int port,
42+
String authToken, TimeUnit timePrecision,
43+
int connectTimeout, int readTimeout, String measurementPrefix,
44+
final String organization, final String bucket) throws Exception {
45+
super(protocol, host, port, "", authToken, timePrecision, connectTimeout, readTimeout, measurementPrefix);
46+
this.authToken = authToken;
47+
this.connectTimeout = connectTimeout;
48+
this.readTimeout = readTimeout;
49+
50+
final String endpoint = new URL(protocol, host, port, "/api/v2/write").toString();
51+
final String queryPrecision = String.format("precision=%s", TimeUtils.toTimePrecision(timePrecision));
52+
final String orgParameter = String.format("org=%s", URLEncoder.encode(organization, StandardCharsets.UTF_8));
53+
final String bucketParameter = String.format("bucket=%s", URLEncoder.encode(bucket, StandardCharsets.UTF_8));
54+
this.url = new URL(endpoint + "?" + queryPrecision + "&" + orgParameter + "&" + bucketParameter);
55+
}
56+
57+
@Override
58+
protected int writeData(byte[] line) throws Exception {
59+
final HttpURLConnection con = (HttpURLConnection) url.openConnection();
60+
con.setRequestMethod("POST");
61+
con.setRequestProperty("Authorization", "Token " + authToken);
62+
con.setDoOutput(true);
63+
con.setConnectTimeout(connectTimeout);
64+
con.setReadTimeout(readTimeout);
65+
con.setRequestProperty("Content-Encoding", "gzip");
66+
67+
try (OutputStream out = con.getOutputStream(); final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(out)) {
68+
gzipOutputStream.write(line);
69+
gzipOutputStream.flush();
70+
out.flush();
71+
}
72+
73+
int responseCode = con.getResponseCode();
74+
75+
// Check if non 2XX response code.
76+
if (responseCode / 100 != 2) {
77+
throw new IOException(
78+
"Server returned HTTP response code: " + responseCode + " for URL: " + url + " with content :'"
79+
+ con.getResponseMessage() + "'");
80+
}
81+
return responseCode;
82+
}
83+
}

src/main/java/com/hivemq/extensions/InfluxDbExtensionMain.java

+10
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ private InfluxDbSender setupSender(@NotNull final InfluxDbConfiguration configur
122122
final int connectTimeout = configuration.getConnectTimeout();
123123
final String prefix = configuration.getPrefix();
124124

125+
// cloud
126+
final String bucket = configuration.getBucket();
127+
final String organization = configuration.getOrganization();
128+
125129
InfluxDbSender sender = null;
126130

127131
try {
@@ -138,6 +142,12 @@ private InfluxDbSender setupSender(@NotNull final InfluxDbConfiguration configur
138142
log.info("Creating InfluxDB UDP sender for server {}:{} and database {}", host, port, database);
139143
sender = new InfluxDbUdpSender(host, port, connectTimeout, database, prefix);
140144
break;
145+
case "cloud":
146+
log.info("Creating InfluxDB Cloud sender for endpoint {}, bucket {}, organization {}", host, bucket, organization);
147+
checkNotNull(bucket, "Bucket name must be defined in cloud mode");
148+
checkNotNull(organization, "Organization must be defined in cloud mode");
149+
sender = new InfluxDbCloudSender("https", host, port, auth, TimeUnit.SECONDS, connectTimeout, connectTimeout, prefix, organization, bucket);
150+
break;
141151

142152
}
143153
} catch (Exception ex) {

src/main/java/com/hivemq/extensions/configuration/InfluxDbConfiguration.java

+14
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public class InfluxDbConfiguration extends PropertiesReader {
5757
private static final String TAGS = "tags";
5858
private static final HashMap<String, String> TAGS_DEFAULT = new HashMap<>();
5959

60+
//InfluxDB Cloud
61+
private static final String BUCKET = "bucket";
62+
private static final String ORGANIZATION = "organization";
63+
6064

6165
public InfluxDbConfiguration(@NotNull final File configFilePath) {
6266
super(configFilePath);
@@ -206,6 +210,16 @@ public Map<String, String> getTags() {
206210
return tagMap;
207211
}
208212

213+
@Nullable
214+
public String getBucket() {
215+
return getProperty(BUCKET);
216+
}
217+
218+
@Nullable
219+
public String getOrganization() {
220+
return getProperty(ORGANIZATION);
221+
}
222+
209223
@Override
210224
public String getFilename() {
211225
return "influxdb.properties";

src/main/resources/influxdb.properties

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ database:hivemq
1010
reportingInterval:1
1111
connectTimeout:5000
1212

13-
tags:host=--NODE-NAME--
13+
tags:host=--NODE-NAME--
14+
15+
# InfluxDB cloud options
16+
bucket:
17+
organization:

0 commit comments

Comments
 (0)