0

I followed the Spring's documentation to see traces in Mongodb by adding the bean

@Bean
MongoClientSettingsBuilderCustomizer mongoMetricsSynchronousContextProvider(ObservationRegistry registry) {
    return (clientSettingsBuilder) -> {
        clientSettingsBuilder.contextProvider(ContextProviderFactory.create(registry))
                             .addCommandListener(new MongoObservationCommandListener(registry));
    };
}

and adding the following properties to :

# Disable Spring Boot's autoconfigured tracing
management.metrics.mongo.command.enabled=false
# Enable it manually
management.tracing.enabled=true

I can see the traces in the zipkins. However my question was since, Spring Already provides an autoconfiguration class metrics MongoAutoConfiguration.

@AutoConfiguration(before = MongoAutoConfiguration.class,
        after = { MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class })
@ConditionalOnClass(MongoClientSettings.class)
@ConditionalOnBean(MeterRegistry.class)
public class MongoMetricsAutoConfiguration {

    @ConditionalOnClass(MongoMetricsCommandListener.class)
    @ConditionalOnProperty(name = "management.metrics.mongo.command.enabled", havingValue = "true",
            matchIfMissing = true)
    static class MongoCommandMetricsConfiguration {

    

        @Bean
        MongoClientSettingsBuilderCustomizer mongoMetricsCommandListenerClientSettingsBuilderCustomizer(
                MongoMetricsCommandListener mongoMetricsCommandListener) {
            return (clientSettingsBuilder) -> clientSettingsBuilder.addCommandListener(mongoMetricsCommandListener);
        }

Why do we need to disable bean from MongoAutoConfiguration? How do we get both metrics and traces ?

1 Answer 1

0

I think this is what is happening:

  1. Micrometer itself has metrics instrumentation for MongoDB (using Micrometer's Metrics API), Spring Boot auto-configures this (so you can get metrics out of the box through this mechanism).
  2. Spring Data MongoDB instruments itself using Micrometer's Observation API which provides metrics and tracing outputs.
  3. management.metrics.mongo.command.enabled=false is needed so that #1 is disabled, so MongoDB will not be instrumented twice.
  4. management.tracing.enabled=true might not be needed, its default value is already true (see: Spring Boot Docs).

So since #2 already provides metrics and tracing data, you should disable #1 so you will not record metrics twice. Please check your metrics output, you should see MongoDB metrics.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.