Polish "Update validation of Micrometer configuration"

See gh-21067
This commit is contained in:
Andy Wilkinson 2020-04-21 17:39:21 +01:00
parent 95798265b6
commit 60a76ce6e7
6 changed files with 28 additions and 36 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,31 +16,29 @@
package org.springframework.boot.actuate.autoconfigure.metrics; package org.springframework.boot.actuate.autoconfigure.metrics;
import io.micrometer.core.instrument.config.validate.Validated.Invalid;
import io.micrometer.core.instrument.config.validate.ValidationException; import io.micrometer.core.instrument.config.validate.ValidationException;
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
import org.springframework.boot.diagnostics.FailureAnalysis; import org.springframework.boot.diagnostics.FailureAnalysis;
import java.util.stream.Collectors;
/** /**
* An {@link AbstractFailureAnalyzer} that performs analysis of failures caused by a * An {@link AbstractFailureAnalyzer} that performs analysis of failures caused by a
* {@link ValidationException}. * {@link ValidationException}.
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
class ValidationFailureAnalyzer class ValidationFailureAnalyzer extends AbstractFailureAnalyzer<ValidationException> {
extends AbstractFailureAnalyzer<ValidationException> {
@Override @Override
protected FailureAnalysis analyze(Throwable rootFailure, ValidationException cause) { protected FailureAnalysis analyze(Throwable rootFailure, ValidationException cause) {
String description = cause.getValidation().failures().stream() StringBuilder description = new StringBuilder(String.format("Invalid Micrometer configuration detected:%n"));
.map(failure -> "management.metrics.export." + failure.getProperty() + " was '" + for (Invalid<?> failure : cause.getValidation().failures()) {
(failure.getValue() == null ? "null" : failure.getValue().toString()) + description.append(String.format("%n - management.metrics.export.%s was '%s' but it %s",
"' but it " + failure.getMessage() + ".") failure.getProperty(), failure.getValue(), failure.getMessage()));
.collect(Collectors.joining("\n")); }
return new FailureAnalysis(description.toString(),
return new FailureAnalysis(description, "Update your application to correct the invalid configuration.", cause);
"Update your application to provide the missing configuration.", cause);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import java.util.concurrent.TimeUnit;
import info.ganglia.gmetric4j.gmetric.GMetric; import info.ganglia.gmetric4j.gmetric.GMetric;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
/** /**
* {@link ConfigurationProperties @ConfigurationProperties} for configuring Ganglia * {@link ConfigurationProperties @ConfigurationProperties} for configuring Ganglia
@ -46,10 +47,7 @@ public class GangliaProperties {
/** /**
* Base time unit used to report rates. * Base time unit used to report rates.
*
* @deprecated No longer used by Micrometer.
*/ */
@Deprecated
private TimeUnit rateUnits; private TimeUnit rateUnits;
/** /**
@ -59,10 +57,7 @@ public class GangliaProperties {
/** /**
* Ganglia protocol version. Must be either 3.1 or 3.0. * Ganglia protocol version. Must be either 3.1 or 3.0.
*
* @deprecated No longer used by Micrometer.
*/ */
@Deprecated
private String protocolVersion; private String protocolVersion;
/** /**
@ -102,10 +97,13 @@ public class GangliaProperties {
this.step = step; this.step = step;
} }
@Deprecated
@DeprecatedConfigurationProperty(reason = "No longer used by Micormeter")
public TimeUnit getRateUnits() { public TimeUnit getRateUnits() {
return this.rateUnits; return this.rateUnits;
} }
@Deprecated
public void setRateUnits(TimeUnit rateUnits) { public void setRateUnits(TimeUnit rateUnits) {
this.rateUnits = rateUnits; this.rateUnits = rateUnits;
} }
@ -118,10 +116,13 @@ public class GangliaProperties {
this.durationUnits = durationUnits; this.durationUnits = durationUnits;
} }
@Deprecated
@DeprecatedConfigurationProperty(reason = "No longer used by Micormeter")
public String getProtocolVersion() { public String getProtocolVersion() {
return this.protocolVersion; return this.protocolVersion;
} }
@Deprecated
public void setProtocolVersion(String protocolVersion) { public void setProtocolVersion(String protocolVersion) {
this.protocolVersion = protocolVersion; this.protocolVersion = protocolVersion;
} }

View File

@ -36,11 +36,6 @@ public abstract class PushRegistryPropertiesConfigAdapter<T extends PushRegistry
super(properties); super(properties);
} }
@Override
public String prefix() {
return null;
}
@Override @Override
public String get(String k) { public String get(String k) {
return null; return null;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -39,12 +39,11 @@ class ValidationFailureAnalyzerTests {
@Test @Test
void analyzesMissingRequiredConfiguration() { void analyzesMissingRequiredConfiguration() {
FailureAnalysis analysis = new ValidationFailureAnalyzer() FailureAnalysis analysis = new ValidationFailureAnalyzer()
.analyze(createFailure(MissingAccountIdConfiguration.class)); .analyze(createFailure(MissingAccountIdAndApiKeyConfiguration.class));
assertThat(analysis).isNotNull(); assertThat(analysis).isNotNull();
assertThat(analysis.getDescription()).isEqualTo( assertThat(analysis.getDescription()).isEqualTo(String.format("Invalid Micrometer configuration detected:%n%n"
"management.metrics.export.newrelic.apiKey was 'null' but it is required when publishing to Insights API.\n" + + " - management.metrics.export.newrelic.apiKey was 'null' but it is required when publishing to Insights API%n"
"management.metrics.export.newrelic.accountId was 'null' but it is required when publishing to Insights API."); + " - management.metrics.export.newrelic.accountId was 'null' but it is required when publishing to Insights API"));
assertThat(analysis.getAction()).isEqualTo("Update your application to provide the missing configuration.");
} }
private Exception createFailure(Class<?> configuration) { private Exception createFailure(Class<?> configuration) {
@ -58,7 +57,7 @@ class ValidationFailureAnalyzerTests {
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
static class MissingAccountIdConfiguration { static class MissingAccountIdAndApiKeyConfiguration {
@Bean @Bean
NewRelicMeterRegistry meterRegistry() { NewRelicMeterRegistry meterRegistry() {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -29,6 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat;
class GangliaPropertiesTests { class GangliaPropertiesTests {
@Test @Test
@SuppressWarnings("deprecation")
void defaultValuesAreConsistent() { void defaultValuesAreConsistent() {
GangliaProperties properties = new GangliaProperties(); GangliaProperties properties = new GangliaProperties();
GangliaConfig config = GangliaConfig.DEFAULT; GangliaConfig config = GangliaConfig.DEFAULT;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,14 +16,12 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront; package org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront;
import io.micrometer.core.instrument.config.validate.ValidationException;
import io.micrometer.wavefront.WavefrontConfig; import io.micrometer.wavefront.WavefrontConfig;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.PushRegistryPropertiesTests; import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.PushRegistryPropertiesTests;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** /**
* Tests for {@link WavefrontProperties}. * Tests for {@link WavefrontProperties}.