Harmonize Metrics test

This commit harmonizes metrics test to rely on `MetricRun.simple()`
rather than configuring a simple `MeterRegistry` manually. Rather than
applying related auto-configurations automatically, `MetricsRun` only
enable the absolute minimum.

See gh-14255
pull/14297/head
Stephane Nicoll 6 years ago
parent c0e45b7333
commit 1ceb076035

@ -34,8 +34,8 @@ import static org.assertj.core.api.Assertions.assertThat;
public class RabbitMetricsAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.with(MetricsRun.simple())
.withConfiguration(AutoConfigurations.of(RabbitAutoConfiguration.class));
.with(MetricsRun.simple()).withConfiguration(AutoConfigurations.of(
RabbitAutoConfiguration.class, RabbitMetricsAutoConfiguration.class));
@Test
public void autoConfiguredConnectionFactoryIsInstrumented() {

@ -37,7 +37,8 @@ public class CacheMetricsAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.with(MetricsRun.simple()).withUserConfiguration(CachingConfiguration.class)
.withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class));
.withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class,
CacheMetricsAutoConfiguration.class));
@Test
public void autoConfiguredCacheManagerIsInstrumented() {

@ -23,8 +23,6 @@ import java.util.function.Function;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.amqp.RabbitMetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.atlas.AtlasMetricsExportAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.datadog.DatadogMetricsExportAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.ganglia.GangliaMetricsExportAutoConfiguration;
@ -36,12 +34,8 @@ import org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.
import org.springframework.boot.actuate.autoconfigure.metrics.export.signalfx.SignalFxMetricsExportAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.statsd.StatsdMetricsExportAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.jdbc.DataSourcePoolMetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.orm.jpa.HibernateMetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.web.client.HttpClientMetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.web.reactive.WebFluxMetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.web.servlet.WebMvcMetricsAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.AbstractApplicationContextRunner;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.util.Assert;
@ -73,12 +67,8 @@ public final class MetricsRun {
}
private static final AutoConfigurations AUTO_CONFIGURATIONS = AutoConfigurations.of(
MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class,
RabbitMetricsAutoConfiguration.class, CacheMetricsAutoConfiguration.class,
DataSourcePoolMetricsAutoConfiguration.class,
HibernateMetricsAutoConfiguration.class,
HttpClientMetricsAutoConfiguration.class,
WebFluxMetricsAutoConfiguration.class, WebMvcMetricsAutoConfiguration.class);
MetricsAutoConfiguration.class,
CompositeMeterRegistryAutoConfiguration.class);
private MetricsRun() {
}
@ -88,7 +78,7 @@ public final class MetricsRun {
* implementation.
* @return the function to apply
*/
public static Function<ApplicationContextRunner, ApplicationContextRunner> simple() {
public static <T extends AbstractApplicationContextRunner<?, ?, ?>> Function<T, T> simple() {
return limitedTo(SimpleMetricsExportAutoConfiguration.class);
}
@ -98,18 +88,18 @@ public final class MetricsRun {
* @param exportAutoConfigurations the export auto-configurations to include
* @return the function to apply
*/
public static Function<ApplicationContextRunner, ApplicationContextRunner> limitedTo(
public static <T extends AbstractApplicationContextRunner<?, ?, ?>> Function<T, T> limitedTo(
Class<?>... exportAutoConfigurations) {
return (contextRunner) -> apply(contextRunner, exportAutoConfigurations);
}
private static ApplicationContextRunner apply(ApplicationContextRunner contextRunner,
Class<?>[] exportAutoConfigurations) {
private static <T extends AbstractApplicationContextRunner<?, ?, ?>> T apply(
T contextRunner, Class<?>[] exportAutoConfigurations) {
for (Class<?> configuration : exportAutoConfigurations) {
Assert.state(EXPORT_AUTO_CONFIGURATIONS.contains(configuration),
() -> "Unknown export auto-configuration " + configuration.getName());
}
return contextRunner
return (T) contextRunner
.withPropertyValues("management.metrics.use-global-registry=false")
.withConfiguration(AUTO_CONFIGURATIONS)
.withConfiguration(AutoConfigurations.of(exportAutoConfigurations));

@ -45,8 +45,9 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
public class RestTemplateMetricsConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.with(MetricsRun.simple()).withConfiguration(
AutoConfigurations.of(RestTemplateAutoConfiguration.class));
.with(MetricsRun.simple())
.withConfiguration(AutoConfigurations.of(RestTemplateAutoConfiguration.class,
HttpClientMetricsAutoConfiguration.class));
@Rule
public OutputCapture out = new OutputCapture();

@ -50,7 +50,8 @@ public class WebClientMetricsConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.with(MetricsRun.simple())
.withConfiguration(AutoConfigurations.of(WebClientAutoConfiguration.class));
.withConfiguration(AutoConfigurations.of(WebClientAutoConfiguration.class,
HttpClientMetricsAutoConfiguration.class));
@Rule
public OutputCapture out = new OutputCapture();

@ -20,8 +20,7 @@ import io.micrometer.core.instrument.MeterRegistry;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
import org.springframework.boot.actuate.autoconfigure.metrics.web.TestController;
import org.springframework.boot.actuate.metrics.web.reactive.server.DefaultWebFluxTagsProvider;
import org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter;
@ -47,9 +46,8 @@ import static org.mockito.Mockito.mock;
public class WebFluxMetricsAutoConfigurationTests {
private ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class,
SimpleMetricsExportAutoConfiguration.class,
WebFluxMetricsAutoConfiguration.class));
.with(MetricsRun.simple()).withConfiguration(
AutoConfigurations.of(WebFluxMetricsAutoConfiguration.class));
@Rule
public OutputCapture output = new OutputCapture();

@ -26,11 +26,11 @@ import javax.servlet.http.HttpServletResponse;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
import org.springframework.boot.actuate.autoconfigure.metrics.web.TestController;
import org.springframework.boot.actuate.metrics.web.servlet.DefaultWebMvcTagsProvider;
import org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter;
@ -60,7 +60,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
public class WebMvcMetricsAutoConfigurationTests {
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(
.with(MetricsRun.simple()).withConfiguration(
AutoConfigurations.of(WebMvcMetricsAutoConfiguration.class));
@Rule
@ -68,25 +68,27 @@ public class WebMvcMetricsAutoConfigurationTests {
@Test
public void backsOffWhenMeterRegistryIsMissing() {
this.contextRunner.run((context) -> assertThat(context)
.doesNotHaveBean(WebMvcMetricsAutoConfiguration.class));
new WebApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(WebMvcMetricsAutoConfiguration.class))
.run((context) -> assertThat(context)
.doesNotHaveBean(WebMvcTagsProvider.class));
}
@Test
public void definesTagsProviderAndFilterWhenMeterRegistryIsPresent() {
this.contextRunner.withUserConfiguration(MeterRegistryConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(DefaultWebMvcTagsProvider.class);
assertThat(context).hasSingleBean(FilterRegistrationBean.class);
assertThat(context.getBean(FilterRegistrationBean.class).getFilter())
.isInstanceOf(WebMvcMetricsFilter.class);
});
this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(DefaultWebMvcTagsProvider.class);
assertThat(context).hasSingleBean(FilterRegistrationBean.class);
assertThat(context.getBean(FilterRegistrationBean.class).getFilter())
.isInstanceOf(WebMvcMetricsFilter.class);
});
}
@Test
public void tagsProviderBacksOff() {
this.contextRunner.withUserConfiguration(MeterRegistryConfiguration.class,
TagsProviderConfiguration.class).run((context) -> {
this.contextRunner.withUserConfiguration(TagsProviderConfiguration.class)
.run((context) -> {
assertThat(context).doesNotHaveBean(DefaultWebMvcTagsProvider.class);
assertThat(context).hasSingleBean(TestWebMvcTagsProvider.class);
});
@ -94,23 +96,18 @@ public class WebMvcMetricsAutoConfigurationTests {
@Test
public void filterRegistrationHasExpectedDispatcherTypesAndOrder() {
this.contextRunner.withUserConfiguration(MeterRegistryConfiguration.class)
.run((context) -> {
FilterRegistrationBean<?> registration = context
.getBean(FilterRegistrationBean.class);
assertThat(registration).hasFieldOrPropertyWithValue(
"dispatcherTypes",
EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
assertThat(registration.getOrder())
.isEqualTo(Ordered.HIGHEST_PRECEDENCE + 1);
});
this.contextRunner.run((context) -> {
FilterRegistrationBean<?> registration = context
.getBean(FilterRegistrationBean.class);
assertThat(registration).hasFieldOrPropertyWithValue("dispatcherTypes",
EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
assertThat(registration.getOrder()).isEqualTo(Ordered.HIGHEST_PRECEDENCE + 1);
});
}
@Test
public void afterMaxUrisReachedFurtherUrisAreDenied() {
this.contextRunner
.withUserConfiguration(TestController.class,
MeterRegistryConfiguration.class)
this.contextRunner.withUserConfiguration(TestController.class)
.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class,
WebMvcAutoConfiguration.class))
.withPropertyValues("management.metrics.web.server.max-uri-tags=2")
@ -125,9 +122,7 @@ public class WebMvcMetricsAutoConfigurationTests {
@Test
public void shouldNotDenyNorLogIfMaxUrisIsNotReached() {
this.contextRunner
.withUserConfiguration(TestController.class,
MeterRegistryConfiguration.class)
this.contextRunner.withUserConfiguration(TestController.class)
.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class,
WebMvcAutoConfiguration.class))
.withPropertyValues("management.metrics.web.server.max-uri-tags=5")
@ -154,16 +149,6 @@ public class WebMvcMetricsAutoConfigurationTests {
return context.getBean(MeterRegistry.class);
}
@Configuration
static class MeterRegistryConfiguration {
@Bean
public MeterRegistry meterRegistry() {
return new SimpleMeterRegistry();
}
}
@Configuration
static class TagsProviderConfiguration {

Loading…
Cancel
Save