:root-target: ../../../../target/

[[mongodb.observability]]
= Observability

[NOTE]
====
MongoDB Java Driver 5.7+ comes with observability directly built in.
We recommend switching to the driver native `ObservabilitySettings`, which can be configured as outlined below:
[source,java]
----
@Bean
MongoClientSettingsBuilderCustomizer mongoDbObservabilitySettings(ObservationRegistry registry) {
    return (clientSettingsBuilder) -> {
        clientSettingsBuilder.observabilitySettings(ObservabilitySettings.micrometerBuilder()
                .observationRegistry(observationRegistry)
                .build());
    };
}
----
In the light of driver native observability support, the types within the Spring Data provided _org.springframework.data.mongodb.observability_ package will not see further development and are subject to deprecation/removal in subsequent releases.
====

To use Spring Data MongoDB's flavor of Observability you must:

. opt into Spring Data MongoDB's configuration settings by customizing `MongoClientSettings` through either your `@SpringBootApplication` class or one of your configuration classes.
+
.Registering MongoDB Micrometer customizer setup
====
[source,java]
----
@Bean
MongoClientSettingsBuilderCustomizer mongoMetricsSynchronousContextProvider(ObservationRegistry registry) {
    return (clientSettingsBuilder) -> {
        clientSettingsBuilder.contextProvider(ContextProviderFactory.create(registry))
                             .addCommandListener(new MongoObservationCommandListener(registry));
    };
}
----
====
+
. Your project must include *Spring Boot Actuator*.
. Disable Spring Boot's autoconfigured MongoDB command listener and enable tracing manually by adding the following properties to your `application.properties`
+
.Custom settings to apply
====
[source]
----
# Disable Spring Boot's autoconfigured tracing
management.metrics.mongo.command.enabled=false
# Enable it manually
management.tracing.enabled=true
----
Be sure to add any other relevant settings needed to configure the tracer you are using based upon Micrometer's reference documentation.
====

This should do it! You are now running with Spring Data MongoDB's usage of Spring Observability's `Observation` API.
See also https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/database/#mongodb[OpenTelemetry Semantic Conventions] for further reference.
