spring-data-mongodb icon indicating copy to clipboard operation
spring-data-mongodb copied to clipboard

MongoDB observability not work after enable Transaction

Open desertTown opened this issue 1 year ago • 4 comments

Hi All, I had previously used certain settings for MongoDB of observability, and those configurations were effective.

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

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

However, after adding the provided snippet to enable database rollback in transactions, I am encountering an issue where there is no longer any database observability.

@EnableTransactionManagement
@Configuration
public class MongoTransactionConfiguration extends AbstractMongoClientConfiguration {

    @Value("${spring.data.mongodb.uri}")
    private String uri;

    @Bean
    MongoTransactionManager transactionManager(MongoDatabaseFactory dbFactory) {
        return new MongoTransactionManager(dbFactory);
    }

    @NotNull
    @Bean
    @Override
    public MongoClient mongoClient() {
        return MongoClients.create(this.uri);
    }

    @Override
    protected String getDatabaseName() {
        return "test";
    }
}

SpringBoot 3.0.9 Could you please assist in troubleshooting this issue? how should I make MongoDB observability work after enable Transaction

desertTown avatar Aug 30 '23 01:08 desertTown

At a quick glance it looks like the MongoClient bean present in MongoTransactionConfiguration will prevent boot from creating the client instance (since there's now already one) and therefore MongoClientSettingsBuilderCustomizer will have no effect. Without knowing the details of the configuration it's hard to tell, but maybe it would be sufficient to just provide the MongoTransactionManager bean without necessarily having to bring in the AbstractMongoClientConfiguration via MongoTransactionConfiguration.

@EnableTransactionManagement
@Configuration
public class MongoTransactionConfiguration {
    @Bean
    MongoTransactionManager transactionManager(MongoDatabaseFactory dbFactory) {
        return new MongoTransactionManager(dbFactory);
    }
}

christophstrobl avatar Aug 30 '23 05:08 christophstrobl

Yes, the solution works in a single MongoDB. But if I use this snippet in a MongoDB cluster(replset) with these MongoDB URI parameters:

mongodb://<username>:<passwrod>@<host1>,<host2>,<host3>/test?retryWrites=true&replicaSet=rs0&readPreference=primary&serverSelectionTimeoutMS=5000&connectTimeoutMS=10000&authSource=admin&authMechanism=SCRAM-SHA-256

it will throw class cast exception like this:

java.lang.ClassCastException: class org.springframework.http.server.observation.ServerRequestObservationContext cannot be cast to class org.springframework.data.mongodb.observability.MongoHandlerContext (org.springframework.http.server.observation.ServerRequestObservationContext and org.springframework.data.mongodb.observability.MongoHandlerContext are in unnamed module of loader 'app')
    at org.springframework.data.mongodb.observability.MongoObservationCommandListener.commandSucceeded(MongoObservationCommandListener.java:140) ~[spring-data-mongodb-4.1.2.jar:4.1.2]
    at com.mongodb.internal.connection.ProtocolHelper.sendCommandSucceededEvent(ProtocolHelper.java:296) ~[mongodb-driver-core-4.9.1.jar:na]

desertTown avatar Aug 31 '23 03:08 desertTown

The error above looks like the the one in #4481 which was fixed in spring-data-mongodb-4.1.3.

christophstrobl avatar Aug 31 '23 05:08 christophstrobl

still the same exception even after apply the version spring-data-mongodb-4.1.3

desertTown avatar Sep 01 '23 01:09 desertTown