Apply MeterRegistryCustomizer to composites
Update `MeterRegistryConfigurer` to also apply customizers to composite meter registries. Prior to this commit composites were skipped due to the incorrect assumption that did not contain their own state. Closes gh-12762
This commit is contained in:
		
							parent
							
								
									d49a1024bd
								
							
						
					
					
						commit
						1fce462944
					
				|  | @ -59,9 +59,6 @@ class MeterRegistryConfigurer { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	void configure(MeterRegistry registry) { | 	void configure(MeterRegistry registry) { | ||||||
| 		if (registry instanceof CompositeMeterRegistry) { |  | ||||||
| 			return; |  | ||||||
| 		} |  | ||||||
| 		// Customizers must be applied before binders, as they may add custom | 		// Customizers must be applied before binders, as they may add custom | ||||||
| 		// tags or alter timer or summary configuration. | 		// tags or alter timer or summary configuration. | ||||||
| 		customize(registry); | 		customize(registry); | ||||||
|  |  | ||||||
|  | @ -0,0 +1,45 @@ | ||||||
|  | /* | ||||||
|  |  * 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 io.micrometer.core.instrument.MeterRegistry; | ||||||
|  | import io.micrometer.core.instrument.composite.CompositeMeterRegistry; | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | import org.springframework.boot.actuate.autoconfigure.metrics.export.atlas.AtlasMetricsExportAutoConfiguration; | ||||||
|  | import org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusMetricsExportAutoConfiguration; | ||||||
|  | import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun; | ||||||
|  | import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||||||
|  | 
 | ||||||
|  | public class MeterRegistryConfigurerIntegrationTests { | ||||||
|  | 	private ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||||||
|  | 			.with(MetricsRun.limitedTo(AtlasMetricsExportAutoConfiguration.class, | ||||||
|  | 					PrometheusMetricsExportAutoConfiguration.class)); | ||||||
|  | 
 | ||||||
|  | 	@Test | ||||||
|  | 	public void binderMetricsAreSearchableFromTheComposite() { | ||||||
|  | 		this.contextRunner | ||||||
|  | 				.run((context) -> { | ||||||
|  | 					CompositeMeterRegistry composite = context.getBean(CompositeMeterRegistry.class); | ||||||
|  | 					composite.get("jvm.memory.used").gauge(); | ||||||
|  | 
 | ||||||
|  | 					for (MeterRegistry registry : context.getBeansOfType(MeterRegistry.class).values()) { | ||||||
|  | 						registry.get("jvm.memory.used").gauge(); | ||||||
|  | 					} | ||||||
|  | 				}); | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | @ -35,7 +35,6 @@ import static org.assertj.core.api.Assertions.assertThat; | ||||||
| import static org.mockito.BDDMockito.given; | import static org.mockito.BDDMockito.given; | ||||||
| import static org.mockito.Mockito.inOrder; | import static org.mockito.Mockito.inOrder; | ||||||
| import static org.mockito.Mockito.verify; | import static org.mockito.Mockito.verify; | ||||||
| import static org.mockito.Mockito.verifyZeroInteractions; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Tests for {@link MeterRegistryConfigurer}. |  * Tests for {@link MeterRegistryConfigurer}. | ||||||
|  | @ -73,13 +72,13 @@ public class MeterRegistryConfigurerTests { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void configureWhenCompositeShouldSkip() { | 	public void configureWhenCompositeShouldApplyCustomizer() { | ||||||
| 		this.binders.add(this.mockBinder); |  | ||||||
| 		this.customizers.add(this.mockCustomizer); | 		this.customizers.add(this.mockCustomizer); | ||||||
| 		MeterRegistryConfigurer configurer = new MeterRegistryConfigurer(this.binders, | 		MeterRegistryConfigurer configurer = new MeterRegistryConfigurer(this.binders, | ||||||
| 				this.filters, this.customizers, false); | 				this.filters, this.customizers, false); | ||||||
| 		configurer.configure(new CompositeMeterRegistry()); | 		CompositeMeterRegistry composite = new CompositeMeterRegistry(); | ||||||
| 		verifyZeroInteractions(this.mockBinder, this.mockCustomizer); | 		configurer.configure(composite); | ||||||
|  | 		verify(this.mockCustomizer).customize(composite); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
|  |  | ||||||
|  | @ -16,10 +16,13 @@ | ||||||
| 
 | 
 | ||||||
| package org.springframework.boot.actuate.autoconfigure.metrics; | package org.springframework.boot.actuate.autoconfigure.metrics; | ||||||
| 
 | 
 | ||||||
|  | import io.micrometer.atlas.AtlasMeterRegistry; | ||||||
| import io.micrometer.core.instrument.MeterRegistry; | import io.micrometer.core.instrument.MeterRegistry; | ||||||
| import io.micrometer.core.instrument.MeterRegistry.Config; | import io.micrometer.prometheus.PrometheusMeterRegistry; | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| 
 | 
 | ||||||
|  | import org.springframework.boot.actuate.autoconfigure.metrics.export.atlas.AtlasMetricsExportAutoConfiguration; | ||||||
|  | import org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusMetricsExportAutoConfiguration; | ||||||
| import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun; | import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun; | ||||||
| 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; | ||||||
|  | @ -36,7 +39,8 @@ import static org.assertj.core.api.Assertions.assertThat; | ||||||
| public class MeterRegistryCustomizerTests { | public class MeterRegistryCustomizerTests { | ||||||
| 
 | 
 | ||||||
| 	private ApplicationContextRunner contextRunner = new ApplicationContextRunner() | 	private ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||||||
| 			.with(MetricsRun.simple()); | 			.with(MetricsRun.limitedTo(AtlasMetricsExportAutoConfiguration.class, | ||||||
|  | 					PrometheusMetricsExportAutoConfiguration.class)); | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void commonTagsAreAppliedToAutoConfiguredBinders() { | 	public void commonTagsAreAppliedToAutoConfiguredBinders() { | ||||||
|  | @ -44,8 +48,7 @@ public class MeterRegistryCustomizerTests { | ||||||
| 				.withUserConfiguration(MeterRegistryCustomizerConfiguration.class) | 				.withUserConfiguration(MeterRegistryCustomizerConfiguration.class) | ||||||
| 				.run((context) -> { | 				.run((context) -> { | ||||||
| 					MeterRegistry registry = context.getBean(MeterRegistry.class); | 					MeterRegistry registry = context.getBean(MeterRegistry.class); | ||||||
| 					assertThat(registry.get("jvm.memory.used").tags("region", "us-east-1") | 					registry.get("jvm.memory.used").tags("region", "us-east-1").gauge(); | ||||||
| 							.gauge()).isNotNull(); |  | ||||||
| 				}); | 				}); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -55,9 +58,21 @@ public class MeterRegistryCustomizerTests { | ||||||
| 				.withUserConfiguration(MeterRegistryCustomizerConfiguration.class) | 				.withUserConfiguration(MeterRegistryCustomizerConfiguration.class) | ||||||
| 				.run((context) -> { | 				.run((context) -> { | ||||||
| 					MeterRegistry registry = context.getBean(MeterRegistry.class); | 					MeterRegistry registry = context.getBean(MeterRegistry.class); | ||||||
| 					assertThat( | 					registry.get("my.thing").tags("region", "us-east-1").gauge(); | ||||||
| 							registry.get("my.thing").tags("region", "us-east-1").gauge()) | 				}); | ||||||
| 									.isNotNull(); | 	} | ||||||
|  | 
 | ||||||
|  | 	@Test | ||||||
|  | 	public void customizersCanBeAppliedToSpecificRegistryTypes() { | ||||||
|  | 		this.contextRunner | ||||||
|  | 				.withUserConfiguration(MeterRegistryCustomizerConfiguration.class) | ||||||
|  | 				.run((context) -> { | ||||||
|  | 					MeterRegistry prometheus = context.getBean(PrometheusMeterRegistry.class); | ||||||
|  | 					prometheus.get("jvm.memory.used").tags("job", "myjob").gauge(); | ||||||
|  | 
 | ||||||
|  | 					MeterRegistry atlas = context.getBean(AtlasMeterRegistry.class); | ||||||
|  | 					assertThat(atlas.find("jvm.memory.used").tags("job", "myjob") | ||||||
|  | 							.gauge()).isNull(); | ||||||
| 				}); | 				}); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -66,10 +81,12 @@ public class MeterRegistryCustomizerTests { | ||||||
| 
 | 
 | ||||||
| 		@Bean | 		@Bean | ||||||
| 		public MeterRegistryCustomizer<MeterRegistry> commonTags() { | 		public MeterRegistryCustomizer<MeterRegistry> commonTags() { | ||||||
| 			return (registry) -> { | 			return (registry) -> registry.config().commonTags("region", "us-east-1"); | ||||||
| 				Config config = registry.config(); | 		} | ||||||
| 				config.commonTags("region", "us-east-1"); | 
 | ||||||
| 			}; | 		@Bean | ||||||
|  | 		public MeterRegistryCustomizer<PrometheusMeterRegistry> prometehusOnlyCommonTags() { | ||||||
|  | 			return (registry) -> registry.config().commonTags("job", "myjob"); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		@Bean | 		@Bean | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue