Merge pull request #11319 from @alex859

* pr/11319:
  Apply configurers on MeterRegistry before injection
pull/11330/merge
Brian Clozel 7 years ago
commit 9cb64de53b

@ -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