Fix MeterRegistry eager load

If there is a `MeterRegistry` bean (any) in the application context,
we add a `MicrometerMetricsCaptor` bean which populates meters further
from the components.
In some case we may load a `MicrometerMetricsCaptor` bean too early
so, not all the stuff around `MeterRegistry` maybe ready.
See Spring Boot and its `MetricsAutoConfiguration`

* Fix the `MicrometerMetricsCaptorRegistrar` the way to rely on the
`ObjectProvider<MeterRegistry>` instead
* Add package protected ctor to the `MicrometerMetricsCaptor` to
provide a target `MeterRegistry` on demand

All of that will ensure that we use an already post-processed `MeterRegistry`
including Spring Boot auto-configuration

**Cherry-pick to 5.3.x & 5.2.x**
This commit is contained in:
Artem Bilan
2020-10-15 14:50:38 -04:00
committed by Gary Russell
parent cab16c41f4
commit d60f23549c
3 changed files with 40 additions and 26 deletions

View File

@@ -23,9 +23,10 @@ import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -41,6 +42,7 @@ import org.springframework.messaging.MessageChannel;
/**
* @author Gary Russell
* @author Artem Bilan
*
* @since 4.2
*
@@ -69,7 +71,7 @@ public class IntegrationManagementConfigurerTests {
channel.setCountsEnabled(true);
channel.setStatsEnabled(true);
ApplicationContext ctx = mock(ApplicationContext.class);
Map<String, IntegrationManagement> beans = new HashMap<String, IntegrationManagement>();
Map<String, IntegrationManagement> beans = new HashMap<>();
beans.put("foo", channel);
beans.put("bar", handler);
beans.put("baz", source);
@@ -88,14 +90,14 @@ public class IntegrationManagementConfigurerTests {
@Test
public void testEmptyAnnotation() {
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(ConfigEmptyAnnotation.class);
AbstractMessageChannel channel = ctx.getBean("channel", AbstractMessageChannel.class);
assertThat(channel.isCountsEnabled()).isTrue();
assertThat(channel.isStatsEnabled()).isTrue();
channel = ctx.getBean("loggingOffChannel", AbstractMessageChannel.class);
assertThat(channel.isLoggingEnabled()).isFalse();
ctx.close();
try (ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigEmptyAnnotation.class)) {
AbstractMessageChannel channel = ctx.getBean("channel", AbstractMessageChannel.class);
assertThat(channel.isCountsEnabled()).isTrue();
assertThat(channel.isStatsEnabled()).isTrue();
assertThat(channel.isLoggingEnabled()).isTrue();
channel = ctx.getBean("loggingOffChannel", AbstractMessageChannel.class);
assertThat(channel.isLoggingEnabled()).isFalse();
}
}
@Configuration
@@ -114,6 +116,7 @@ public class IntegrationManagementConfigurerTests {
directChannel.setLoggingEnabled(false);
return directChannel;
}
}
}