Skip to content

Commit 8eb73bc

Browse files
shakuzensnicoll
authored andcommitted
Support sending metrics to InfluxDB v2
See gh-25721
1 parent 91c3c72 commit 8eb73bc

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
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;
2223
import org.springframework.boot.context.properties.ConfigurationProperties;
24+
import org.springframework.util.StringUtils;
2325

2426
/**
2527
* {@link ConfigurationProperties @ConfigurationProperties} for configuring Influx metrics
@@ -33,7 +35,7 @@
3335
public class InfluxProperties extends StepRegistryProperties {
3436

3537
/**
36-
* Database to send metrics to.
38+
* Database to send metrics to when using an InfluxDB 1.x database.
3739
*/
3840
private String db = "mydb";
3941

@@ -93,6 +95,29 @@ public class InfluxProperties extends StepRegistryProperties {
9395
*/
9496
private boolean autoCreateDb = true;
9597

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

209+
public InfluxApiVersion getApiVersion() {
210+
if (this.apiVersion != null) {
211+
return this.apiVersion;
212+
}
213+
return StringUtils.hasText(this.org) ? InfluxApiVersion.V2 : InfluxApiVersion.V1;
214+
}
215+
216+
public void setApiVersion(InfluxApiVersion apiVersion) {
217+
this.apiVersion = apiVersion;
218+
}
219+
220+
public String getOrg() {
221+
return this.org;
222+
}
223+
224+
public void setOrg(String org) {
225+
this.org = org;
226+
}
227+
228+
public String getBucket() {
229+
if (this.bucket != null) {
230+
return this.bucket;
231+
}
232+
return this.db;
233+
}
234+
235+
public void setBucket(String bucket) {
236+
this.bucket = bucket;
237+
}
238+
239+
public String getToken() {
240+
return this.token;
241+
}
242+
243+
public void setToken(String token) {
244+
this.token = token;
245+
}
246+
184247
}

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
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ 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.getApiVersion()).isEqualTo(config.apiVersion());
50+
assertThat(properties.getOrg()).isEqualTo(config.org());
51+
assertThat(properties.getBucket()).isEqualTo(config.bucket());
52+
assertThat(properties.getToken()).isEqualTo(config.token());
4953
}
5054

5155
}

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)