Skip to content

Commit 8808d5e

Browse files
committed
Polish "Support sending metrics to InfluxDB v2"
See gh-25721
1 parent 8eb73bc commit 8808d5e

File tree

3 files changed

+70
-18
lines changed

3 files changed

+70
-18
lines changed

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

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

2222
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryProperties;
2323
import org.springframework.boot.context.properties.ConfigurationProperties;
24-
import org.springframework.util.StringUtils;
2524

2625
/**
2726
* {@link ConfigurationProperties @ConfigurationProperties} for configuring Influx metrics
@@ -35,7 +34,7 @@
3534
public class InfluxProperties extends StepRegistryProperties {
3635

3736
/**
38-
* Database to send metrics to when using an InfluxDB 1.x database.
37+
* Database to send metrics to. InfluxDB v1 only.
3938
*/
4039
private String db = "mydb";
4140

@@ -45,12 +44,12 @@ public class InfluxProperties extends StepRegistryProperties {
4544
private InfluxConsistency consistency = InfluxConsistency.ONE;
4645

4746
/**
48-
* Login user of the Influx server.
47+
* Login user of the Influx server. InfluxDB v1 only.
4948
*/
5049
private String userName;
5150

5251
/**
53-
* Login password of the Influx server.
52+
* Login password of the Influx server. InfluxDB v1 only.
5453
*/
5554
private String password;
5655

@@ -91,7 +90,7 @@ public class InfluxProperties extends StepRegistryProperties {
9190

9291
/**
9392
* Whether to create the Influx database if it does not exist before attempting to
94-
* publish metrics to it.
93+
* publish metrics to it. InfluxDB v1 only.
9594
*/
9695
private boolean autoCreateDb = true;
9796

@@ -102,13 +101,13 @@ public class InfluxProperties extends StepRegistryProperties {
102101
private InfluxApiVersion apiVersion;
103102

104103
/**
105-
* InfluxDB v2 org to write metrics.
104+
* Org to write metrics to. InfluxDB v2 only.
106105
*/
107106
private String org;
108107

109108
/**
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.
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.
112111
*/
113112
private String bucket;
114113

@@ -207,10 +206,7 @@ public void setAutoCreateDb(boolean autoCreateDb) {
207206
}
208207

209208
public InfluxApiVersion getApiVersion() {
210-
if (this.apiVersion != null) {
211-
return this.apiVersion;
212-
}
213-
return StringUtils.hasText(this.org) ? InfluxApiVersion.V2 : InfluxApiVersion.V1;
209+
return this.apiVersion;
214210
}
215211

216212
public void setApiVersion(InfluxApiVersion apiVersion) {
@@ -226,10 +222,7 @@ public void setOrg(String org) {
226222
}
227223

228224
public String getBucket() {
229-
if (this.bucket != null) {
230-
return this.bucket;
231-
}
232-
return this.db;
225+
return this.bucket;
233226
}
234227

235228
public void setBucket(String bucket) {
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: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ 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());
5049
assertThat(properties.getOrg()).isEqualTo(config.org());
51-
assertThat(properties.getBucket()).isEqualTo(config.bucket());
5250
assertThat(properties.getToken()).isEqualTo(config.token());
5351
}
5452

0 commit comments

Comments
 (0)