Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Micrometer metrics for MongoDB

I use spring boot 2.2.5 + micrometer 1.3.5 + starter-data-mongodb

Under "io.micrometer.core.instrument.binder.mongodb" I can see 2 classes CommandListener and ConnectionPoolListener. I would like to know what purpose these serve?

In actuator metrics endpoint, mongo metrics are not available.

How do I enable metrics for mongodb in actuator? For example, actuator automatically shows several metrics of RabbitMQ. I was expecting something similar in case of MongoDB as well. Should I create my own metrics?

like image 472
Winster Avatar asked Oct 24 '25 10:10

Winster


1 Answers

In order to enable Spring Boot applying its AutoConfiguration I suggest to use the customizer pattern:

Kotlin:

@Configuration
class MongoConfiguration {

    @Bean
    fun mongoClientSettingsBuilderCustomizer(meterRegistry: MeterRegistry) =
        MongoClientSettingsBuilderCustomizer {
           it.addCommandListener(MongoMetricsCommandListener(meterRegistry))}
}

Java:

@Configuration
public class MongoConfiguration {

   @Bean
   public MongoClientSettingsBuilderCustomizer mongoClientSettingsBuilderCustomizer(MeterRegistry meterRegistry) {
        return builder -> builder.addCommandListener(new MongoMetricsCommandListener(meterRegistry));
   }

}

Please note you currenly will neither see a relation to the spring data repository nor to the mongo collection in the metrics. see open issue


EDIT (07/30/2021):

The issues has been fixed, so you likely get collection metrics in a current release.

like image 145
chrgue Avatar answered Oct 27 '25 01:10

chrgue



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!