From 8eb73bcf017983cdbe077e1a149af79abd227fa4 Mon Sep 17 00:00:00 2001 From: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com> Date: Wed, 17 Mar 2021 12:48:40 +0900 Subject: [PATCH] Support sending metrics to InfluxDB v2 See gh-25721 --- .../export/influx/InfluxProperties.java | 65 ++++++++++++++++++- .../influx/InfluxPropertiesConfigAdapter.java | 21 ++++++ .../export/influx/InfluxPropertiesTests.java | 4 ++ .../asciidoc/production-ready-features.adoc | 3 +- 4 files changed, 91 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxProperties.java index 6b909dccc1..ce7b017cca 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxProperties.java @@ -16,10 +16,12 @@ package org.springframework.boot.actuate.autoconfigure.metrics.export.influx; +import io.micrometer.influx.InfluxApiVersion; import io.micrometer.influx.InfluxConsistency; import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryProperties; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.util.StringUtils; /** * {@link ConfigurationProperties @ConfigurationProperties} for configuring Influx metrics @@ -33,7 +35,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; public class InfluxProperties extends StepRegistryProperties { /** - * Database to send metrics to. + * Database to send metrics to when using an InfluxDB 1.x database. */ private String db = "mydb"; @@ -93,6 +95,29 @@ public class InfluxProperties extends StepRegistryProperties { */ private boolean autoCreateDb = true; + /** + * API version of InfluxDB to use. Defaults to 'v1' unless an org is configured. If an + * org is configured, defaults to 'v2'. + */ + private InfluxApiVersion apiVersion; + + /** + * InfluxDB v2 org to write metrics. + */ + private String org; + + /** + * InfluxDB v2 bucket for metrics. Use either the bucket name or ID. Defaults to the + * value of the db property if not set. + */ + private String bucket; + + /** + * Authentication token to use with calls to the InfluxDB backend. For InfluxDB v1, + * the Bearer scheme is used. For v2, the Token scheme is used. + */ + private String token; + public String getDb() { return this.db; } @@ -181,4 +206,42 @@ public class InfluxProperties extends StepRegistryProperties { this.autoCreateDb = autoCreateDb; } + public InfluxApiVersion getApiVersion() { + if (this.apiVersion != null) { + return this.apiVersion; + } + return StringUtils.hasText(this.org) ? InfluxApiVersion.V2 : InfluxApiVersion.V1; + } + + public void setApiVersion(InfluxApiVersion apiVersion) { + this.apiVersion = apiVersion; + } + + public String getOrg() { + return this.org; + } + + public void setOrg(String org) { + this.org = org; + } + + public String getBucket() { + if (this.bucket != null) { + return this.bucket; + } + return this.db; + } + + public void setBucket(String bucket) { + this.bucket = bucket; + } + + public String getToken() { + return this.token; + } + + public void setToken(String token) { + this.token = token; + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxPropertiesConfigAdapter.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxPropertiesConfigAdapter.java index 782354f500..e8fdc58dd9 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxPropertiesConfigAdapter.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxPropertiesConfigAdapter.java @@ -16,6 +16,7 @@ package org.springframework.boot.actuate.autoconfigure.metrics.export.influx; +import io.micrometer.influx.InfluxApiVersion; import io.micrometer.influx.InfluxConfig; import io.micrometer.influx.InfluxConsistency; @@ -94,4 +95,24 @@ class InfluxPropertiesConfigAdapter extends StepRegistryPropertiesConfigAdapter< return get(InfluxProperties::isAutoCreateDb, InfluxConfig.super::autoCreateDb); } + @Override + public InfluxApiVersion apiVersion() { + return get(InfluxProperties::getApiVersion, InfluxConfig.super::apiVersion); + } + + @Override + public String org() { + return get(InfluxProperties::getOrg, InfluxConfig.super::org); + } + + @Override + public String bucket() { + return get(InfluxProperties::getBucket, InfluxConfig.super::bucket); + } + + @Override + public String token() { + return get(InfluxProperties::getToken, InfluxConfig.super::token); + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxPropertiesTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxPropertiesTests.java index c45eb10027..47f44b79a7 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxPropertiesTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxPropertiesTests.java @@ -46,6 +46,10 @@ class InfluxPropertiesTests extends StepRegistryPropertiesTests { assertThat(properties.getUri()).isEqualTo(config.uri()); assertThat(properties.isCompressed()).isEqualTo(config.compressed()); assertThat(properties.isAutoCreateDb()).isEqualTo(config.autoCreateDb()); + assertThat(properties.getApiVersion()).isEqualTo(config.apiVersion()); + assertThat(properties.getOrg()).isEqualTo(config.org()); + assertThat(properties.getBucket()).isEqualTo(config.bucket()); + assertThat(properties.getToken()).isEqualTo(config.token()); } } diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc index 3e393b2916..0db7777e02 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc @@ -1833,7 +1833,8 @@ You should also configure one or more tags to identify the data source to which [[production-ready-metrics-export-influx]] ==== Influx -By default, metrics are exported to {micrometer-registry-docs}/influx[Influx] running on your local machine. +By default, metrics are exported to an {micrometer-registry-docs}/influx[Influx] v1 instance running on your local machine with the default configuration. +To export metrics to InfluxDB v2, configure the `org`, `bucket`, and authentication `token` for writing metrics. The location of the https://www.influxdata.com[Influx server] to use can be provided using: [source,yaml,indent=0,configprops,configblocks]