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 io.micrometer.core.instrument.MeterRegistry; | ||||||
| import org.junit.Test; | 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.boot.test.context.runner.ApplicationContextRunner; | ||||||
| import org.springframework.context.annotation.Bean; | import org.springframework.context.annotation.Bean; | ||||||
| 
 | 
 | ||||||
|  | @ -34,13 +32,13 @@ import static org.assertj.core.api.Assertions.assertThat; | ||||||
|  */ |  */ | ||||||
| public class MeterRegistryConfigurerTests { | public class MeterRegistryConfigurerTests { | ||||||
| 
 | 
 | ||||||
|  | 	private ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||||||
|  | 			.with(MetricsRun.simple()); | ||||||
|  | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void commonTagsAreAppliedToAutoConfiguredBinders() { | 	public void commonTagsAreAppliedToAutoConfiguredBinders() { | ||||||
| 		new ApplicationContextRunner() | 		this.contextRunner | ||||||
| 				.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class)) | 				.withUserConfiguration(MeterRegistryConfigurerConfiguration.class) | ||||||
| 				.withConfiguration( |  | ||||||
| 						UserConfigurations.of(MeterRegistryConfigurerConfiguration.class)) |  | ||||||
| 				.withPropertyValues("management.metrics.use-global-registry=false") |  | ||||||
| 				.run((context) -> assertThat(context.getBean(MeterRegistry.class) | 				.run((context) -> assertThat(context.getBean(MeterRegistry.class) | ||||||
| 						.get("jvm.memory.used").tags("region", "us-east-1").gauge()) | 						.get("jvm.memory.used").tags("region", "us-east-1").gauge()) | ||||||
| 								.isNotNull()); | 								.isNotNull()); | ||||||
|  | @ -48,11 +46,8 @@ public class MeterRegistryConfigurerTests { | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void commonTagsAreAppliedBeforeRegistryIsInjectableElsewhere() { | 	public void commonTagsAreAppliedBeforeRegistryIsInjectableElsewhere() { | ||||||
| 		new ApplicationContextRunner() | 		this.contextRunner | ||||||
| 				.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class)) | 				.withUserConfiguration(MeterRegistryConfigurerConfiguration.class) | ||||||
| 				.withConfiguration( |  | ||||||
| 						UserConfigurations.of(MeterRegistryConfigurerConfiguration.class)) |  | ||||||
| 				.withPropertyValues("management.metrics.use-global-registry=false") |  | ||||||
| 				.run((context) -> assertThat(context.getBean(MeterRegistry.class) | 				.run((context) -> assertThat(context.getBean(MeterRegistry.class) | ||||||
| 						.get("my.thing").tags("region", "us-east-1").gauge()) | 						.get("my.thing").tags("region", "us-east-1").gauge()) | ||||||
| 								.isNotNull()); | 								.isNotNull()); | ||||||
|  | @ -72,6 +67,7 @@ public class MeterRegistryConfigurerTests { | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		class MyThing { | 		class MyThing { | ||||||
|  | 
 | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -21,7 +21,6 @@ import java.util.UUID; | ||||||
| import javax.sql.DataSource; | import javax.sql.DataSource; | ||||||
| 
 | 
 | ||||||
| import io.micrometer.core.instrument.MeterRegistry; | import io.micrometer.core.instrument.MeterRegistry; | ||||||
| import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |  | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| 
 | 
 | ||||||
| import org.springframework.boot.autoconfigure.AutoConfigurations; | import org.springframework.boot.autoconfigure.AutoConfigurations; | ||||||
|  | @ -40,10 +39,8 @@ import static org.assertj.core.api.Assertions.assertThat; | ||||||
|  */ |  */ | ||||||
| public class MetricsAutoConfigurationTests { | public class MetricsAutoConfigurationTests { | ||||||
| 
 | 
 | ||||||
| 	private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | 	private ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||||||
| 			.withPropertyValues("management.metrics.use-global-registry=false") | 			.with(MetricsRun.simple()); | ||||||
| 			.withUserConfiguration(RegistryConfiguration.class) |  | ||||||
| 			.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class)); |  | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void autoConfiguredDataSourceIsInstrumented() { | 	public void autoConfiguredDataSourceIsInstrumented() { | ||||||
|  | @ -107,16 +104,6 @@ public class MetricsAutoConfigurationTests { | ||||||
| 				}); | 				}); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	@Configuration |  | ||||||
| 	static class RegistryConfiguration { |  | ||||||
| 
 |  | ||||||
| 		@Bean |  | ||||||
| 		public MeterRegistry meterRegistry() { |  | ||||||
| 			return new SimpleMeterRegistry(); |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	@Configuration | 	@Configuration | ||||||
| 	static class TwoDataSourcesConfiguration { | 	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; | package org.springframework.boot.actuate.autoconfigure.metrics.amqp; | ||||||
| 
 | 
 | ||||||
| import io.micrometer.core.instrument.MeterRegistry; | import io.micrometer.core.instrument.MeterRegistry; | ||||||
| import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |  | ||||||
| import org.junit.Test; | 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.AutoConfigurations; | ||||||
| import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration; | import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration; | ||||||
| import org.springframework.boot.test.context.runner.ApplicationContextRunner; | 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; | import static org.assertj.core.api.Assertions.assertThat; | ||||||
| 
 | 
 | ||||||
|  | @ -37,10 +34,8 @@ import static org.assertj.core.api.Assertions.assertThat; | ||||||
| public class RabbitMetricsConfigurationTests { | public class RabbitMetricsConfigurationTests { | ||||||
| 
 | 
 | ||||||
| 	private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | 	private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||||||
| 			.withUserConfiguration(RegistryConfiguration.class) | 			.with(MetricsRun.simple()) | ||||||
| 			.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class, | 			.withConfiguration(AutoConfigurations.of(RabbitAutoConfiguration.class)); | ||||||
| 					RabbitAutoConfiguration.class)) |  | ||||||
| 			.withPropertyValues("management.metrics.use-global-registry=false"); |  | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void autoConfiguredConnectionFactoryIsInstrumented() { | 	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; | package org.springframework.boot.actuate.autoconfigure.metrics.cache; | ||||||
| 
 | 
 | ||||||
| import io.micrometer.core.instrument.MeterRegistry; | import io.micrometer.core.instrument.MeterRegistry; | ||||||
| import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |  | ||||||
| import org.junit.Test; | 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.AutoConfigurations; | ||||||
| import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration; | import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration; | ||||||
| import org.springframework.boot.test.context.runner.ApplicationContextRunner; | import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||||||
| import org.springframework.cache.annotation.EnableCaching; | import org.springframework.cache.annotation.EnableCaching; | ||||||
| import org.springframework.context.annotation.Bean; |  | ||||||
| import org.springframework.context.annotation.Configuration; | import org.springframework.context.annotation.Configuration; | ||||||
| 
 | 
 | ||||||
| import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||||
|  | @ -38,10 +36,8 @@ import static org.assertj.core.api.Assertions.assertThat; | ||||||
| public class CacheMetricsConfigurationTests { | public class CacheMetricsConfigurationTests { | ||||||
| 
 | 
 | ||||||
| 	private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | 	private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||||||
| 			.withUserConfiguration(RegistryConfiguration.class) | 			.with(MetricsRun.simple()).withUserConfiguration(CachingConfiguration.class) | ||||||
| 			.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class, | 			.withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class)); | ||||||
| 					CacheAutoConfiguration.class)) |  | ||||||
| 			.withPropertyValues("management.metrics.use-global-registry=false"); |  | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void autoConfiguredCacheManagerIsInstrumented() { | 	public void autoConfiguredCacheManagerIsInstrumented() { | ||||||
|  | @ -93,12 +89,7 @@ public class CacheMetricsConfigurationTests { | ||||||
| 
 | 
 | ||||||
| 	@Configuration | 	@Configuration | ||||||
| 	@EnableCaching | 	@EnableCaching | ||||||
| 	static class RegistryConfiguration { | 	static class CachingConfiguration { | ||||||
| 
 |  | ||||||
| 		@Bean |  | ||||||
| 		public MeterRegistry meterRegistry() { |  | ||||||
| 			return new SimpleMeterRegistry(); |  | ||||||
| 		} |  | ||||||
| 
 | 
 | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -17,17 +17,14 @@ | ||||||
| package org.springframework.boot.actuate.autoconfigure.metrics.web.client; | package org.springframework.boot.actuate.autoconfigure.metrics.web.client; | ||||||
| 
 | 
 | ||||||
| import io.micrometer.core.instrument.MeterRegistry; | import io.micrometer.core.instrument.MeterRegistry; | ||||||
| import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |  | ||||||
| import org.junit.Test; | 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.actuate.metrics.web.client.MetricsRestTemplateCustomizer; | ||||||
| import org.springframework.boot.autoconfigure.AutoConfigurations; | import org.springframework.boot.autoconfigure.AutoConfigurations; | ||||||
| import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration; | import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration; | ||||||
| import org.springframework.boot.test.context.runner.ApplicationContextRunner; | import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||||||
| import org.springframework.boot.web.client.RestTemplateBuilder; | 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.http.HttpStatus; | ||||||
| import org.springframework.test.web.client.MockRestServiceServer; | import org.springframework.test.web.client.MockRestServiceServer; | ||||||
| import org.springframework.web.client.RestTemplate; | import org.springframework.web.client.RestTemplate; | ||||||
|  | @ -44,10 +41,8 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat | ||||||
| public class RestTemplateMetricsConfigurationTests { | public class RestTemplateMetricsConfigurationTests { | ||||||
| 
 | 
 | ||||||
| 	private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | 	private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||||||
| 			.withPropertyValues("management.metrics.use-global-registry=false") | 			.with(MetricsRun.simple()).withConfiguration( | ||||||
| 			.withConfiguration(AutoConfigurations.of(RestTemplateAutoConfiguration.class, | 					AutoConfigurations.of(RestTemplateAutoConfiguration.class)); | ||||||
| 					MetricsAutoConfiguration.class)) |  | ||||||
| 			.withUserConfiguration(RegistryConfiguration.class); |  | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void restTemplateCreatedWithBuilderIsInstrumented() { | 	public void restTemplateCreatedWithBuilderIsInstrumented() { | ||||||
|  | @ -80,14 +75,4 @@ public class RestTemplateMetricsConfigurationTests { | ||||||
| 		registry.get("http.client.requests").meter(); | 		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"); |  * 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. | ||||||
|  | @ -19,6 +19,7 @@ package org.springframework.boot.test.context.runner; | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.Collections; | import java.util.Collections; | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  | import java.util.function.Function; | ||||||
| import java.util.function.Supplier; | import java.util.function.Supplier; | ||||||
| 
 | 
 | ||||||
| import org.springframework.boot.context.annotation.Configurations; | import org.springframework.boot.context.annotation.Configurations; | ||||||
|  | @ -220,6 +221,16 @@ abstract class AbstractApplicationContextRunner<SELF extends AbstractApplication | ||||||
| 				add(this.configurations, configurations)); | 				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) { | 	private <T> List<T> add(List<T> list, T element) { | ||||||
| 		List<T> result = new ArrayList<>(list); | 		List<T> result = new ArrayList<>(list); | ||||||
| 		result.add(element); | 		result.add(element); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue