Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB observability not work after enable Transaction #4487

Closed
desertTown opened this issue Aug 30, 2023 · 7 comments
Closed

MongoDB observability not work after enable Transaction #4487

desertTown opened this issue Aug 30, 2023 · 7 comments
Assignees

Comments

@desertTown
Copy link

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 30, 2023
@christophstrobl
Copy link
Member

christophstrobl commented Aug 30, 2023

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 christophstrobl added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 30, 2023
@desertTown
Copy link
Author

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]

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Aug 31, 2023
@christophstrobl
Copy link
Member

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

@christophstrobl christophstrobl added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Aug 31, 2023
@desertTown
Copy link
Author

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

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Sep 1, 2023
@marcingrzejszczak
Copy link
Contributor

Hey @desertTown , I've tried to replicate this locally and I can't do it. Can you provide a sample that replicates this problem?

@marcingrzejszczak marcingrzejszczak added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Oct 25, 2024
@spring-projects-issues
Copy link

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Nov 1, 2024
@spring-projects-issues
Copy link

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

@spring-projects-issues spring-projects-issues closed this as not planned Won't fix, can't repro, duplicate, stale Nov 8, 2024
@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants