Commit a034c9a1 authored by Andy Wilkinson's avatar Andy Wilkinson

Merge pull request #21067 from jkschneider

* gh-21067:
  Polish "Update validation of Micrometer configuration"
  Update validation of Micrometer configuration

Closes gh-21067
parents e26f3e2a 60a76ce6
/* /*
* 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,29 +16,29 @@ ...@@ -16,29 +16,29 @@
package org.springframework.boot.actuate.autoconfigure.metrics; package org.springframework.boot.actuate.autoconfigure.metrics;
import io.micrometer.core.instrument.config.MissingRequiredConfigurationException; import io.micrometer.core.instrument.config.validate.Validated.Invalid;
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;
/** /**
* An {@link AbstractFailureAnalyzer} that performs analysis of failures caused by a * An {@link AbstractFailureAnalyzer} that performs analysis of failures caused by a
* {@link MissingRequiredConfigurationException}. * {@link ValidationException}.
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
class MissingRequiredConfigurationFailureAnalyzer class ValidationFailureAnalyzer extends AbstractFailureAnalyzer<ValidationException> {
extends AbstractFailureAnalyzer<MissingRequiredConfigurationException> {
@Override @Override
protected FailureAnalysis analyze(Throwable rootFailure, MissingRequiredConfigurationException cause) { protected FailureAnalysis analyze(Throwable rootFailure, ValidationException cause) {
StringBuilder description = new StringBuilder(); StringBuilder description = new StringBuilder(String.format("Invalid Micrometer configuration detected:%n"));
description.append(cause.getMessage()); for (Invalid<?> failure : cause.getValidation().failures()) {
if (!cause.getMessage().endsWith(".")) { description.append(String.format("%n - management.metrics.export.%s was '%s' but it %s",
description.append("."); failure.getProperty(), failure.getValue(), failure.getMessage()));
} }
return new FailureAnalysis(description.toString(), return new FailureAnalysis(description.toString(),
"Update your application to provide the missing configuration.", cause); "Update your application to correct the invalid configuration.", cause);
} }
} }
...@@ -62,17 +62,17 @@ public class ElasticProperties extends StepRegistryProperties { ...@@ -62,17 +62,17 @@ public class ElasticProperties extends StepRegistryProperties {
/** /**
* Login user of the Elastic server. * Login user of the Elastic server.
*/ */
private String userName = ""; private String userName;
/** /**
* Login password of the Elastic server. * Login password of the Elastic server.
*/ */
private String password = ""; private String password;
/** /**
* Ingest pipeline name. By default, events are not pre-processed. * Ingest pipeline name. By default, events are not pre-processed.
*/ */
private String pipeline = ""; private String pipeline;
public String getHost() { public String getHost() {
return this.host; return this.host;
......
/* /*
* 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; ...@@ -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
...@@ -47,7 +48,7 @@ public class GangliaProperties { ...@@ -47,7 +48,7 @@ public class GangliaProperties {
/** /**
* Base time unit used to report rates. * Base time unit used to report rates.
*/ */
private TimeUnit rateUnits = TimeUnit.SECONDS; private TimeUnit rateUnits;
/** /**
* Base time unit used to report durations. * Base time unit used to report durations.
...@@ -57,7 +58,7 @@ public class GangliaProperties { ...@@ -57,7 +58,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.
*/ */
private String protocolVersion = "3.1"; private String protocolVersion;
/** /**
* UDP addressing mode, either unicast or multicast. * UDP addressing mode, either unicast or multicast.
...@@ -96,10 +97,13 @@ public class GangliaProperties { ...@@ -96,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;
} }
...@@ -112,10 +116,13 @@ public class GangliaProperties { ...@@ -112,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;
} }
......
...@@ -36,11 +36,6 @@ public abstract class PushRegistryPropertiesConfigAdapter<T extends PushRegistry ...@@ -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;
......
...@@ -105,4 +105,4 @@ org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementChil ...@@ -105,4 +105,4 @@ org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementChil
org.springframework.boot.actuate.autoconfigure.web.servlet.WebMvcEndpointChildContextConfiguration org.springframework.boot.actuate.autoconfigure.web.servlet.WebMvcEndpointChildContextConfiguration
org.springframework.boot.diagnostics.FailureAnalyzer=\ org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.actuate.autoconfigure.metrics.MissingRequiredConfigurationFailureAnalyzer org.springframework.boot.actuate.autoconfigure.metrics.ValidationFailureAnalyzer
/* /*
* 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.
...@@ -30,19 +30,20 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -30,19 +30,20 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
/** /**
* Tests for {@link MissingRequiredConfigurationFailureAnalyzer}. * Tests for {@link ValidationFailureAnalyzer}.
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
class MissingRequiredConfigurationFailureAnalyzerTests { class ValidationFailureAnalyzerTests {
@Test @Test
void analyzesMissingRequiredConfiguration() { void analyzesMissingRequiredConfiguration() {
FailureAnalysis analysis = new MissingRequiredConfigurationFailureAnalyzer() FailureAnalysis analysis = new ValidationFailureAnalyzer()
.analyze(createFailure(MissingAccountIdConfiguration.class)); .analyze(createFailure(MissingAccountIdAndApiKeyConfiguration.class));
assertThat(analysis).isNotNull(); assertThat(analysis).isNotNull();
assertThat(analysis.getDescription()).isEqualTo("accountId must be set to report metrics to New Relic."); assertThat(analysis.getDescription()).isEqualTo(String.format("Invalid Micrometer configuration detected:%n%n"
assertThat(analysis.getAction()).isEqualTo("Update your application to provide the missing configuration."); + " - 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"));
} }
private Exception createFailure(Class<?> configuration) { private Exception createFailure(Class<?> configuration) {
...@@ -56,7 +57,7 @@ class MissingRequiredConfigurationFailureAnalyzerTests { ...@@ -56,7 +57,7 @@ class MissingRequiredConfigurationFailureAnalyzerTests {
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
static class MissingAccountIdConfiguration { static class MissingAccountIdAndApiKeyConfiguration {
@Bean @Bean
NewRelicMeterRegistry meterRegistry() { NewRelicMeterRegistry meterRegistry() {
......
/* /*
* 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; ...@@ -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;
......
/* /*
* 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.
...@@ -36,8 +36,6 @@ class WavefrontPropertiesTests extends PushRegistryPropertiesTests { ...@@ -36,8 +36,6 @@ class WavefrontPropertiesTests extends PushRegistryPropertiesTests {
WavefrontConfig config = WavefrontConfig.DEFAULT_DIRECT; WavefrontConfig config = WavefrontConfig.DEFAULT_DIRECT;
assertStepRegistryDefaultValues(properties, config); assertStepRegistryDefaultValues(properties, config);
assertThat(properties.getUri().toString()).isEqualTo(config.uri()); assertThat(properties.getUri().toString()).isEqualTo(config.uri());
// source has no static default value
assertThat(properties.getApiToken()).isEqualTo(config.apiToken());
assertThat(properties.getGlobalPrefix()).isEqualTo(config.globalPrefix()); assertThat(properties.getGlobalPrefix()).isEqualTo(config.globalPrefix());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment