Spring-boot: RabbitMetricsAutoConfiguration is instantiated too late

Created on 13 Apr 2018  路  5Comments  路  Source: spring-projects/spring-boot

Hi,

Micrometer metrics for RabbitMQ are instantiated too late which leads that metrics are never updated. Instead a NoOpMetricsCollector is used and it's methods are invoked. It seems that rabbitmq connection is created before in com.rabbitmq.client.ConnectionFactory.

    public Connection newConnection(ExecutorService executor, AddressResolver addressResolver, String clientProvidedName)
        throws IOException, TimeoutException {
        if(this.metricsCollector == null) {
            this.metricsCollector = new NoOpMetricsCollector();
        }

Workaround that works for me now is to create a RabbitMQConfiguration class with:

@PostConstruct
public void init() {
  com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = this.connectionFactory.getRabbitConnectionFactory();
  rabbitConnectionFactory.setMetricsCollector(new MicrometerMetricsCollector(registry));
}

spring-boot: 2.0.1.RELEASE

bug

All 5 comments

@mzakes Can you share a sample that we can run ourselves that demonstrates what you mean by "too late"?

@snicoll Here is the example. Hope that it helps.

/cc @jkschneider

Micrometer metrics for RabbitMQ are instantiated too late

I am not sure I understand that statement, nor the NoOp part. Debugging your sample application (that indeed exhibits an issue), a connection is created when the factory is created (I'd assume to check that the broker is available?) which will initialize the metricsCollector but that field is overridden later on by the auto-configuration and before message starts to be consumed. Something doesn't look right in this arrangement but I haven't figured out what exactly.

Alright so that configure method on your example creates a Connection and that Connection is cached and reused. So this very first connection uses the NoOp metrics collector while any further call to create a Connection will effectively use the micrometer metrics collector.

Was this page helpful?
0 / 5 - 0 ratings