Skip to content

Commit a034c9a

Browse files
committed
Merge pull request #21067 from jkschneider
* gh-21067: Polish "Update validation of Micrometer configuration" Update validation of Micrometer configuration Closes gh-21067
2 parents e26f3e2 + 60a76ce commit a034c9a

File tree

8 files changed

+37
-35
lines changed

8 files changed

+37
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,29 +16,29 @@
1616

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

19-
import io.micrometer.core.instrument.config.MissingRequiredConfigurationException;
19+
import io.micrometer.core.instrument.config.validate.Validated.Invalid;
20+
import io.micrometer.core.instrument.config.validate.ValidationException;
2021

2122
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
2223
import org.springframework.boot.diagnostics.FailureAnalysis;
2324

2425
/**
2526
* An {@link AbstractFailureAnalyzer} that performs analysis of failures caused by a
26-
* {@link MissingRequiredConfigurationException}.
27+
* {@link ValidationException}.
2728
*
2829
* @author Andy Wilkinson
2930
*/
30-
class MissingRequiredConfigurationFailureAnalyzer
31-
extends AbstractFailureAnalyzer<MissingRequiredConfigurationException> {
31+
class ValidationFailureAnalyzer extends AbstractFailureAnalyzer<ValidationException> {
3232

3333
@Override
34-
protected FailureAnalysis analyze(Throwable rootFailure, MissingRequiredConfigurationException cause) {
35-
StringBuilder description = new StringBuilder();
36-
description.append(cause.getMessage());
37-
if (!cause.getMessage().endsWith(".")) {
38-
description.append(".");
34+
protected FailureAnalysis analyze(Throwable rootFailure, ValidationException cause) {
35+
StringBuilder description = new StringBuilder(String.format("Invalid Micrometer configuration detected:%n"));
36+
for (Invalid<?> failure : cause.getValidation().failures()) {
37+
description.append(String.format("%n - management.metrics.export.%s was '%s' but it %s",
38+
failure.getProperty(), failure.getValue(), failure.getMessage()));
3939
}
4040
return new FailureAnalysis(description.toString(),
41-
"Update your application to provide the missing configuration.", cause);
41+
"Update your application to correct the invalid configuration.", cause);
4242
}
4343

4444
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ public class ElasticProperties extends StepRegistryProperties {
6262
/**
6363
* Login user of the Elastic server.
6464
*/
65-
private String userName = "";
65+
private String userName;
6666

6767
/**
6868
* Login password of the Elastic server.
6969
*/
70-
private String password = "";
70+
private String password;
7171

7272
/**
7373
* Ingest pipeline name. By default, events are not pre-processed.
7474
*/
75-
private String pipeline = "";
75+
private String pipeline;
7676

7777
public String getHost() {
7878
return this.host;

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import info.ganglia.gmetric4j.gmetric.GMetric;
2323

2424
import org.springframework.boot.context.properties.ConfigurationProperties;
25+
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
2526

2627
/**
2728
* {@link ConfigurationProperties @ConfigurationProperties} for configuring Ganglia
@@ -47,7 +48,7 @@ public class GangliaProperties {
4748
/**
4849
* Base time unit used to report rates.
4950
*/
50-
private TimeUnit rateUnits = TimeUnit.SECONDS;
51+
private TimeUnit rateUnits;
5152

5253
/**
5354
* Base time unit used to report durations.
@@ -57,7 +58,7 @@ public class GangliaProperties {
5758
/**
5859
* Ganglia protocol version. Must be either 3.1 or 3.0.
5960
*/
60-
private String protocolVersion = "3.1";
61+
private String protocolVersion;
6162

6263
/**
6364
* UDP addressing mode, either unicast or multicast.
@@ -96,10 +97,13 @@ public void setStep(Duration step) {
9697
this.step = step;
9798
}
9899

100+
@Deprecated
101+
@DeprecatedConfigurationProperty(reason = "No longer used by Micormeter")
99102
public TimeUnit getRateUnits() {
100103
return this.rateUnits;
101104
}
102105

106+
@Deprecated
103107
public void setRateUnits(TimeUnit rateUnits) {
104108
this.rateUnits = rateUnits;
105109
}
@@ -112,10 +116,13 @@ public void setDurationUnits(TimeUnit durationUnits) {
112116
this.durationUnits = durationUnits;
113117
}
114118

119+
@Deprecated
120+
@DeprecatedConfigurationProperty(reason = "No longer used by Micormeter")
115121
public String getProtocolVersion() {
116122
return this.protocolVersion;
117123
}
118124

125+
@Deprecated
119126
public void setProtocolVersion(String protocolVersion) {
120127
this.protocolVersion = protocolVersion;
121128
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ public PushRegistryPropertiesConfigAdapter(T properties) {
3636
super(properties);
3737
}
3838

39-
@Override
40-
public String prefix() {
41-
return null;
42-
}
43-
4439
@Override
4540
public String get(String k) {
4641
return null;

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@ org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementChil
105105
org.springframework.boot.actuate.autoconfigure.web.servlet.WebMvcEndpointChildContextConfiguration
106106

107107
org.springframework.boot.diagnostics.FailureAnalyzer=\
108-
org.springframework.boot.actuate.autoconfigure.metrics.MissingRequiredConfigurationFailureAnalyzer
108+
org.springframework.boot.actuate.autoconfigure.metrics.ValidationFailureAnalyzer
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,19 +30,20 @@
3030
import static org.junit.jupiter.api.Assertions.fail;
3131

3232
/**
33-
* Tests for {@link MissingRequiredConfigurationFailureAnalyzer}.
33+
* Tests for {@link ValidationFailureAnalyzer}.
3434
*
3535
* @author Andy Wilkinson
3636
*/
37-
class MissingRequiredConfigurationFailureAnalyzerTests {
37+
class ValidationFailureAnalyzerTests {
3838

3939
@Test
4040
void analyzesMissingRequiredConfiguration() {
41-
FailureAnalysis analysis = new MissingRequiredConfigurationFailureAnalyzer()
42-
.analyze(createFailure(MissingAccountIdConfiguration.class));
41+
FailureAnalysis analysis = new ValidationFailureAnalyzer()
42+
.analyze(createFailure(MissingAccountIdAndApiKeyConfiguration.class));
4343
assertThat(analysis).isNotNull();
44-
assertThat(analysis.getDescription()).isEqualTo("accountId must be set to report metrics to New Relic.");
45-
assertThat(analysis.getAction()).isEqualTo("Update your application to provide the missing configuration.");
44+
assertThat(analysis.getDescription()).isEqualTo(String.format("Invalid Micrometer configuration detected:%n%n"
45+
+ " - management.metrics.export.newrelic.apiKey was 'null' but it is required when publishing to Insights API%n"
46+
+ " - management.metrics.export.newrelic.accountId was 'null' but it is required when publishing to Insights API"));
4647
}
4748

4849
private Exception createFailure(Class<?> configuration) {
@@ -56,7 +57,7 @@ private Exception createFailure(Class<?> configuration) {
5657
}
5758

5859
@Configuration(proxyBeanMethods = false)
59-
static class MissingAccountIdConfiguration {
60+
static class MissingAccountIdAndApiKeyConfiguration {
6061

6162
@Bean
6263
NewRelicMeterRegistry meterRegistry() {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
class GangliaPropertiesTests {
3030

3131
@Test
32+
@SuppressWarnings("deprecation")
3233
void defaultValuesAreConsistent() {
3334
GangliaProperties properties = new GangliaProperties();
3435
GangliaConfig config = GangliaConfig.DEFAULT;

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,8 +36,6 @@ void defaultValuesAreConsistent() {
3636
WavefrontConfig config = WavefrontConfig.DEFAULT_DIRECT;
3737
assertStepRegistryDefaultValues(properties, config);
3838
assertThat(properties.getUri().toString()).isEqualTo(config.uri());
39-
// source has no static default value
40-
assertThat(properties.getApiToken()).isEqualTo(config.apiToken());
4139
assertThat(properties.getGlobalPrefix()).isEqualTo(config.globalPrefix());
4240
}
4341

0 commit comments

Comments
 (0)