Extract common micrometer test setup
Create `MetricsRun` that can be used to apply common micrometer test configuration to a `ApplicationContextRunner`. Closes gh-11804
This commit is contained in:
		
							parent
							
								
									d1de1cd053
								
							
						
					
					
						commit
						fd237f85cc
					
				|  | @ -19,8 +19,6 @@ package org.springframework.boot.actuate.autoconfigure.metrics; | |||
| import io.micrometer.core.instrument.MeterRegistry; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import org.springframework.boot.autoconfigure.AutoConfigurations; | ||||
| import org.springframework.boot.context.annotation.UserConfigurations; | ||||
| import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||||
| import org.springframework.context.annotation.Bean; | ||||
| 
 | ||||
|  | @ -34,13 +32,13 @@ import static org.assertj.core.api.Assertions.assertThat; | |||
|  */ | ||||
| public class MeterRegistryConfigurerTests { | ||||
| 
 | ||||
| 	private ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||||
| 			.with(MetricsRun.simple()); | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void commonTagsAreAppliedToAutoConfiguredBinders() { | ||||
| 		new ApplicationContextRunner() | ||||
| 				.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class)) | ||||
| 				.withConfiguration( | ||||
| 						UserConfigurations.of(MeterRegistryConfigurerConfiguration.class)) | ||||
| 				.withPropertyValues("management.metrics.use-global-registry=false") | ||||
| 		this.contextRunner | ||||
| 				.withUserConfiguration(MeterRegistryConfigurerConfiguration.class) | ||||
| 				.run((context) -> assertThat(context.getBean(MeterRegistry.class) | ||||
| 						.get("jvm.memory.used").tags("region", "us-east-1").gauge()) | ||||
| 								.isNotNull()); | ||||
|  | @ -48,11 +46,8 @@ public class MeterRegistryConfigurerTests { | |||
| 
 | ||||
| 	@Test | ||||
| 	public void commonTagsAreAppliedBeforeRegistryIsInjectableElsewhere() { | ||||
| 		new ApplicationContextRunner() | ||||
| 				.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class)) | ||||
| 				.withConfiguration( | ||||
| 						UserConfigurations.of(MeterRegistryConfigurerConfiguration.class)) | ||||
| 				.withPropertyValues("management.metrics.use-global-registry=false") | ||||
| 		this.contextRunner | ||||
| 				.withUserConfiguration(MeterRegistryConfigurerConfiguration.class) | ||||
| 				.run((context) -> assertThat(context.getBean(MeterRegistry.class) | ||||
| 						.get("my.thing").tags("region", "us-east-1").gauge()) | ||||
| 								.isNotNull()); | ||||
|  | @ -72,6 +67,7 @@ public class MeterRegistryConfigurerTests { | |||
| 		} | ||||
| 
 | ||||
| 		class MyThing { | ||||
| 
 | ||||
| 		} | ||||
| 
 | ||||
| 	} | ||||
|  |  | |||
|  | @ -21,7 +21,6 @@ import java.util.UUID; | |||
| import javax.sql.DataSource; | ||||
| 
 | ||||
| import io.micrometer.core.instrument.MeterRegistry; | ||||
| import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import org.springframework.boot.autoconfigure.AutoConfigurations; | ||||
|  | @ -40,10 +39,8 @@ import static org.assertj.core.api.Assertions.assertThat; | |||
|  */ | ||||
| public class MetricsAutoConfigurationTests { | ||||
| 
 | ||||
| 	private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||||
| 			.withPropertyValues("management.metrics.use-global-registry=false") | ||||
| 			.withUserConfiguration(RegistryConfiguration.class) | ||||
| 			.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class)); | ||||
| 	private ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||||
| 			.with(MetricsRun.simple()); | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void autoConfiguredDataSourceIsInstrumented() { | ||||
|  | @ -107,16 +104,6 @@ public class MetricsAutoConfigurationTests { | |||
| 				}); | ||||
| 	} | ||||
| 
 | ||||
| 	@Configuration | ||||
| 	static class RegistryConfiguration { | ||||
| 
 | ||||
| 		@Bean | ||||
| 		public MeterRegistry meterRegistry() { | ||||
| 			return new SimpleMeterRegistry(); | ||||
| 		} | ||||
| 
 | ||||
| 	} | ||||
| 
 | ||||
| 	@Configuration | ||||
| 	static class TwoDataSourcesConfiguration { | ||||
| 
 | ||||
|  |  | |||
|  | @ -0,0 +1,87 @@ | |||
| /* | ||||
|  * Copyright 2012-2018 the original author or authors. | ||||
|  * | ||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * | ||||
|  *      http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  * | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| 
 | ||||
| package org.springframework.boot.actuate.autoconfigure.metrics; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.Arrays; | ||||
| import java.util.Collections; | ||||
| import java.util.LinkedHashSet; | ||||
| import java.util.List; | ||||
| import java.util.Set; | ||||
| import java.util.function.Function; | ||||
| 
 | ||||
| import org.springframework.boot.autoconfigure.AutoConfigurations; | ||||
| import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||||
| 
 | ||||
| /** | ||||
|  * Additional metrics configuration and settings that can be applied to a | ||||
|  * {@link ApplicationContextRunner} when running a metrics test. | ||||
|  * | ||||
|  * @author Jon Schneider | ||||
|  * @author Phillip Webb | ||||
|  */ | ||||
| public final class MetricsRun { | ||||
| 
 | ||||
| 	private static final Set<String> IMPLEMENTATIONS = Collections | ||||
| 			.unmodifiableSet(new LinkedHashSet<>(Arrays.asList("atlas", "datadog", | ||||
| 					"ganglia", "graphite", "influx", "jmx", "prometheus", "statsd", | ||||
| 					"newrelic", "signalfx", "wavefront", "simple"))); | ||||
| 
 | ||||
| 	private MetricsRun() { | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Return a function that configures the run to be limited to the {@code simple} | ||||
| 	 * implementation. | ||||
| 	 * @return the function to apply | ||||
| 	 */ | ||||
| 	public static Function<ApplicationContextRunner, ApplicationContextRunner> simple() { | ||||
| 		return limitedTo("simple"); | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Return a function that configures the run to be limited to the specified | ||||
| 	 * implementations. | ||||
| 	 * @param implementations the implementations to include | ||||
| 	 * @return the function to apply | ||||
| 	 */ | ||||
| 	public static Function<ApplicationContextRunner, ApplicationContextRunner> limitedTo( | ||||
| 			String... implementations) { | ||||
| 		return (contextRunner) -> apply(contextRunner, implementations); | ||||
| 	} | ||||
| 
 | ||||
| 	private static ApplicationContextRunner apply(ApplicationContextRunner contextRunner, | ||||
| 			String[] implementations) { | ||||
| 		return contextRunner.withPropertyValues(getPropertyValues(implementations)) | ||||
| 				.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class)); | ||||
| 	} | ||||
| 
 | ||||
| 	private static String[] getPropertyValues(String[] implementations) { | ||||
| 		List<String> propertyValues = new ArrayList<>(); | ||||
| 		propertyValues.add("management.metrics.use-global-registry=false"); | ||||
| 		List<String> keep = Arrays.asList(implementations); | ||||
| 		IMPLEMENTATIONS.stream() | ||||
| 				.filter((implementation) -> !keep.contains(implementations)) | ||||
| 				.map(MetricsRun::disableExport).forEach(propertyValues::add); | ||||
| 		return propertyValues.toArray(new String[0]); | ||||
| 	} | ||||
| 
 | ||||
| 	private static String disableExport(String implementation) { | ||||
| 		return "management.metrics.export." + implementation + ".enabled=false"; | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  | @ -17,15 +17,12 @@ | |||
| package org.springframework.boot.actuate.autoconfigure.metrics.amqp; | ||||
| 
 | ||||
| import io.micrometer.core.instrument.MeterRegistry; | ||||
| import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; | ||||
| import org.springframework.boot.actuate.autoconfigure.metrics.MetricsRun; | ||||
| import org.springframework.boot.autoconfigure.AutoConfigurations; | ||||
| import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration; | ||||
| import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||||
| import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.context.annotation.Configuration; | ||||
| 
 | ||||
| import static org.assertj.core.api.Assertions.assertThat; | ||||
| 
 | ||||
|  | @ -37,10 +34,8 @@ import static org.assertj.core.api.Assertions.assertThat; | |||
| public class RabbitMetricsConfigurationTests { | ||||
| 
 | ||||
| 	private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||||
| 			.withUserConfiguration(RegistryConfiguration.class) | ||||
| 			.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class, | ||||
| 					RabbitAutoConfiguration.class)) | ||||
| 			.withPropertyValues("management.metrics.use-global-registry=false"); | ||||
| 			.with(MetricsRun.simple()) | ||||
| 			.withConfiguration(AutoConfigurations.of(RabbitAutoConfiguration.class)); | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void autoConfiguredConnectionFactoryIsInstrumented() { | ||||
|  | @ -71,14 +66,4 @@ public class RabbitMetricsConfigurationTests { | |||
| 				}); | ||||
| 	} | ||||
| 
 | ||||
| 	@Configuration | ||||
| 	static class RegistryConfiguration { | ||||
| 
 | ||||
| 		@Bean | ||||
| 		public MeterRegistry meterRegistry() { | ||||
| 			return new SimpleMeterRegistry(); | ||||
| 		} | ||||
| 
 | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  |  | |||
|  | @ -17,15 +17,13 @@ | |||
| package org.springframework.boot.actuate.autoconfigure.metrics.cache; | ||||
| 
 | ||||
| import io.micrometer.core.instrument.MeterRegistry; | ||||
| import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; | ||||
| import org.springframework.boot.actuate.autoconfigure.metrics.MetricsRun; | ||||
| import org.springframework.boot.autoconfigure.AutoConfigurations; | ||||
| import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration; | ||||
| import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||||
| import org.springframework.cache.annotation.EnableCaching; | ||||
| import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.context.annotation.Configuration; | ||||
| 
 | ||||
| import static org.assertj.core.api.Assertions.assertThat; | ||||
|  | @ -38,10 +36,8 @@ import static org.assertj.core.api.Assertions.assertThat; | |||
| public class CacheMetricsConfigurationTests { | ||||
| 
 | ||||
| 	private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||||
| 			.withUserConfiguration(RegistryConfiguration.class) | ||||
| 			.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class, | ||||
| 					CacheAutoConfiguration.class)) | ||||
| 			.withPropertyValues("management.metrics.use-global-registry=false"); | ||||
| 			.with(MetricsRun.simple()).withUserConfiguration(CachingConfiguration.class) | ||||
| 			.withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class)); | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void autoConfiguredCacheManagerIsInstrumented() { | ||||
|  | @ -93,12 +89,7 @@ public class CacheMetricsConfigurationTests { | |||
| 
 | ||||
| 	@Configuration | ||||
| 	@EnableCaching | ||||
| 	static class RegistryConfiguration { | ||||
| 
 | ||||
| 		@Bean | ||||
| 		public MeterRegistry meterRegistry() { | ||||
| 			return new SimpleMeterRegistry(); | ||||
| 		} | ||||
| 	static class CachingConfiguration { | ||||
| 
 | ||||
| 	} | ||||
| 
 | ||||
|  |  | |||
|  | @ -17,17 +17,14 @@ | |||
| package org.springframework.boot.actuate.autoconfigure.metrics.web.client; | ||||
| 
 | ||||
| import io.micrometer.core.instrument.MeterRegistry; | ||||
| import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; | ||||
| import org.springframework.boot.actuate.autoconfigure.metrics.MetricsRun; | ||||
| import org.springframework.boot.actuate.metrics.web.client.MetricsRestTemplateCustomizer; | ||||
| import org.springframework.boot.autoconfigure.AutoConfigurations; | ||||
| import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration; | ||||
| import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||||
| import org.springframework.boot.web.client.RestTemplateBuilder; | ||||
| import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.context.annotation.Configuration; | ||||
| import org.springframework.http.HttpStatus; | ||||
| import org.springframework.test.web.client.MockRestServiceServer; | ||||
| import org.springframework.web.client.RestTemplate; | ||||
|  | @ -44,10 +41,8 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat | |||
| public class RestTemplateMetricsConfigurationTests { | ||||
| 
 | ||||
| 	private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||||
| 			.withPropertyValues("management.metrics.use-global-registry=false") | ||||
| 			.withConfiguration(AutoConfigurations.of(RestTemplateAutoConfiguration.class, | ||||
| 					MetricsAutoConfiguration.class)) | ||||
| 			.withUserConfiguration(RegistryConfiguration.class); | ||||
| 			.with(MetricsRun.simple()).withConfiguration( | ||||
| 					AutoConfigurations.of(RestTemplateAutoConfiguration.class)); | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void restTemplateCreatedWithBuilderIsInstrumented() { | ||||
|  | @ -80,14 +75,4 @@ public class RestTemplateMetricsConfigurationTests { | |||
| 		registry.get("http.client.requests").meter(); | ||||
| 	} | ||||
| 
 | ||||
| 	@Configuration | ||||
| 	static class RegistryConfiguration { | ||||
| 
 | ||||
| 		@Bean | ||||
| 		public MeterRegistry registry() { | ||||
| 			return new SimpleMeterRegistry(); | ||||
| 		} | ||||
| 
 | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  |  | |||
|  | @ -1,5 +1,5 @@ | |||
| /* | ||||
|  * Copyright 2012-2017 the original author or authors. | ||||
|  * Copyright 2012-2018 the original author or authors. | ||||
|  * | ||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  | @ -19,6 +19,7 @@ package org.springframework.boot.test.context.runner; | |||
| import java.util.ArrayList; | ||||
| import java.util.Collections; | ||||
| import java.util.List; | ||||
| import java.util.function.Function; | ||||
| import java.util.function.Supplier; | ||||
| 
 | ||||
| import org.springframework.boot.context.annotation.Configurations; | ||||
|  | @ -220,6 +221,16 @@ abstract class AbstractApplicationContextRunner<SELF extends AbstractApplication | |||
| 				add(this.configurations, configurations)); | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Apply customization to this runner. | ||||
| 	 * @param customizer the customizer to call | ||||
| 	 * @return a new instance with the customizations applied | ||||
| 	 */ | ||||
| 	@SuppressWarnings("unchecked") | ||||
| 	public SELF with(Function<SELF, SELF> customizer) { | ||||
| 		return customizer.apply((SELF) this); | ||||
| 	} | ||||
| 
 | ||||
| 	private <T> List<T> add(List<T> list, T element) { | ||||
| 		List<T> result = new ArrayList<>(list); | ||||
| 		result.add(element); | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue