Skip to content

Commit b61e4bb

Browse files
committed
Merge pull request #25721 from shakuzen
* pr/25721: Polish "Support sending metrics to InfluxDB v2" Support sending metrics to InfluxDB v2 Closes gh-25721
2 parents 91c3c72 + 8808d5e commit b61e4bb

File tree

5 files changed

+146
-5
lines changed

5 files changed

+146
-5
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxProperties.java

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.metrics.export.influx;
1818

19+
import io.micrometer.influx.InfluxApiVersion;
1920
import io.micrometer.influx.InfluxConsistency;
2021

2122
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryProperties;
@@ -33,7 +34,7 @@
3334
public class InfluxProperties extends StepRegistryProperties {
3435

3536
/**
36-
* Database to send metrics to.
37+
* Database to send metrics to. InfluxDB v1 only.
3738
*/
3839
private String db = "mydb";
3940

@@ -43,12 +44,12 @@ public class InfluxProperties extends StepRegistryProperties {
4344
private InfluxConsistency consistency = InfluxConsistency.ONE;
4445

4546
/**
46-
* Login user of the Influx server.
47+
* Login user of the Influx server. InfluxDB v1 only.
4748
*/
4849
private String userName;
4950

5051
/**
51-
* Login password of the Influx server.
52+
* Login password of the Influx server. InfluxDB v1 only.
5253
*/
5354
private String password;
5455

@@ -89,10 +90,33 @@ public class InfluxProperties extends StepRegistryProperties {
8990

9091
/**
9192
* Whether to create the Influx database if it does not exist before attempting to
92-
* publish metrics to it.
93+
* publish metrics to it. InfluxDB v1 only.
9394
*/
9495
private boolean autoCreateDb = true;
9596

97+
/**
98+
* API version of InfluxDB to use. Defaults to 'v1' unless an org is configured. If an
99+
* org is configured, defaults to 'v2'.
100+
*/
101+
private InfluxApiVersion apiVersion;
102+
103+
/**
104+
* Org to write metrics to. InfluxDB v2 only.
105+
*/
106+
private String org;
107+
108+
/**
109+
* Bucket for metrics. Use either the bucket name or ID. Defaults to the value of the
110+
* db property if not set. InfluxDB v2 only.
111+
*/
112+
private String bucket;
113+
114+
/**
115+
* Authentication token to use with calls to the InfluxDB backend. For InfluxDB v1,
116+
* the Bearer scheme is used. For v2, the Token scheme is used.
117+
*/
118+
private String token;
119+
96120
public String getDb() {
97121
return this.db;
98122
}
@@ -181,4 +205,36 @@ public void setAutoCreateDb(boolean autoCreateDb) {
181205
this.autoCreateDb = autoCreateDb;
182206
}
183207

208+
public InfluxApiVersion getApiVersion() {
209+
return this.apiVersion;
210+
}
211+
212+
public void setApiVersion(InfluxApiVersion apiVersion) {
213+
this.apiVersion = apiVersion;
214+
}
215+
216+
public String getOrg() {
217+
return this.org;
218+
}
219+
220+
public void setOrg(String org) {
221+
this.org = org;
222+
}
223+
224+
public String getBucket() {
225+
return this.bucket;
226+
}
227+
228+
public void setBucket(String bucket) {
229+
this.bucket = bucket;
230+
}
231+
232+
public String getToken() {
233+
return this.token;
234+
}
235+
236+
public void setToken(String token) {
237+
this.token = token;
238+
}
239+
184240
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxPropertiesConfigAdapter.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.metrics.export.influx;
1818

19+
import io.micrometer.influx.InfluxApiVersion;
1920
import io.micrometer.influx.InfluxConfig;
2021
import io.micrometer.influx.InfluxConsistency;
2122

@@ -94,4 +95,24 @@ public boolean autoCreateDb() {
9495
return get(InfluxProperties::isAutoCreateDb, InfluxConfig.super::autoCreateDb);
9596
}
9697

98+
@Override
99+
public InfluxApiVersion apiVersion() {
100+
return get(InfluxProperties::getApiVersion, InfluxConfig.super::apiVersion);
101+
}
102+
103+
@Override
104+
public String org() {
105+
return get(InfluxProperties::getOrg, InfluxConfig.super::org);
106+
}
107+
108+
@Override
109+
public String bucket() {
110+
return get(InfluxProperties::getBucket, InfluxConfig.super::bucket);
111+
}
112+
113+
@Override
114+
public String token() {
115+
return get(InfluxProperties::getToken, InfluxConfig.super::token);
116+
}
117+
97118
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
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+
* https://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+
package org.springframework.boot.actuate.autoconfigure.metrics.export.influx;
18+
19+
import io.micrometer.influx.InfluxApiVersion;
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
/**
25+
* Tests for {@link InfluxPropertiesConfigAdapter}.
26+
*
27+
* @author Stephane Nicoll
28+
*/
29+
class InfluxPropertiesConfigAdapterTests {
30+
31+
@Test
32+
void adaptInfluxV1BasicConfig() {
33+
InfluxProperties properties = new InfluxProperties();
34+
properties.setDb("test-db");
35+
properties.setUri("https://influx.example.com:8086");
36+
properties.setUserName("user");
37+
properties.setPassword("secret");
38+
InfluxPropertiesConfigAdapter adapter = new InfluxPropertiesConfigAdapter(properties);
39+
assertThat(adapter.apiVersion()).isEqualTo(InfluxApiVersion.V1);
40+
assertThat(adapter.db()).isEqualTo("test-db");
41+
assertThat(adapter.uri()).isEqualTo("https://influx.example.com:8086");
42+
assertThat(adapter.userName()).isEqualTo("user");
43+
assertThat(adapter.password()).isEqualTo("secret");
44+
}
45+
46+
@Test
47+
void adaptInfluxV2BasicConfig() {
48+
InfluxProperties properties = new InfluxProperties();
49+
properties.setOrg("test-org");
50+
properties.setBucket("test-bucket");
51+
properties.setUri("https://influx.example.com:8086");
52+
properties.setToken("token");
53+
InfluxPropertiesConfigAdapter adapter = new InfluxPropertiesConfigAdapter(properties);
54+
assertThat(adapter.apiVersion()).isEqualTo(InfluxApiVersion.V2);
55+
assertThat(adapter.org()).isEqualTo("test-org");
56+
assertThat(adapter.bucket()).isEqualTo("test-bucket");
57+
assertThat(adapter.uri()).isEqualTo("https://influx.example.com:8086");
58+
assertThat(adapter.token()).isEqualTo("token");
59+
}
60+
61+
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxPropertiesTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ void defaultValuesAreConsistent() {
4646
assertThat(properties.getUri()).isEqualTo(config.uri());
4747
assertThat(properties.isCompressed()).isEqualTo(config.compressed());
4848
assertThat(properties.isAutoCreateDb()).isEqualTo(config.autoCreateDb());
49+
assertThat(properties.getOrg()).isEqualTo(config.org());
50+
assertThat(properties.getToken()).isEqualTo(config.token());
4951
}
5052

5153
}

spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1833,7 +1833,8 @@ You should also configure one or more tags to identify the data source to which
18331833
18341834
[[production-ready-metrics-export-influx]]
18351835
==== Influx
1836-
By default, metrics are exported to {micrometer-registry-docs}/influx[Influx] running on your local machine.
1836+
By default, metrics are exported to an {micrometer-registry-docs}/influx[Influx] v1 instance running on your local machine with the default configuration.
1837+
To export metrics to InfluxDB v2, configure the `org`, `bucket`, and authentication `token` for writing metrics.
18371838
The location of the https://www.influxdata.com[Influx server] to use can be provided using:
18381839
18391840
[source,yaml,indent=0,configprops,configblocks]

0 commit comments

Comments
 (0)