Apply configurers on MeterRegistry before injection

MeterRegistryConfigurers are now applied within the Bean method
creating the CompositeMeterRegistry, instead of applying them
later in its lifecycle, when the bean itself could have been
injected somewhere.

Fixes gh-11319
pull/11330/merge
Alessandro Ciccimarra 7 years ago committed by Brian Clozel
parent 04068ee792
commit e1def637ae

@ -79,8 +79,11 @@ public class MetricsAutoConfiguration {
@Bean
@ConditionalOnMissingBean(MeterRegistry.class)
public CompositeMeterRegistry compositeMeterRegistry(
ObjectProvider<Collection<MetricsExporter>> exporters) {
ObjectProvider<Collection<MetricsExporter>> exporters,
ObjectProvider<Collection<MeterRegistryConfigurer>> configurers) {
CompositeMeterRegistry composite = new CompositeMeterRegistry();
configurers.getIfAvailable(Collections::emptyList)
.forEach((configurer) -> configurer.configureRegistry(composite));
exporters.getIfAvailable(Collections::emptyList).stream()
.map(MetricsExporter::registry).forEach(composite::add);
return composite;
@ -119,11 +122,8 @@ public class MetricsAutoConfiguration {
static class MeterRegistryConfigurationSupport {
MeterRegistryConfigurationSupport(MeterRegistry registry,
ObjectProvider<Collection<MeterRegistryConfigurer>> configurers,
MetricsProperties config,
ObjectProvider<Collection<MeterBinder>> binders) {
configurers.getIfAvailable(Collections::emptyList)
.forEach((configurer) -> configurer.configureRegistry(registry));
binders.getIfAvailable(Collections::emptyList)
.forEach((binder) -> binder.bindTo(registry));
if (config.isUseGlobalRegistry()) {

@ -46,6 +46,18 @@ public class MeterRegistryConfigurerTests {
.isPresent());
}
@Test
public void commonTagsAreAppliedBeforeRegistryIsInjectableElsewhere() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class))
.withConfiguration(
UserConfigurations.of(MeterRegistryConfigurerConfiguration.class))
.withPropertyValues("metrics.use-global-registry=false")
.run((context) -> assertThat(context.getBean(MeterRegistry.class)
.find("my.thing").tags("region", "us-east-1").gauge())
.isPresent());
}
static class MeterRegistryConfigurerConfiguration {
@Bean
@ -53,6 +65,14 @@ public class MeterRegistryConfigurerTests {
return (registry) -> registry.config().commonTags("region", "us-east-1");
}
private class MyThing {}
@Bean
public MyThing myThing(MeterRegistry registry) {
registry.gauge("my.thing", 0);
return new MyThing();
}
}
}

Loading…
Cancel
Save