opentracing-contrib / java-mongo-driver

OpenTracing Instrumentation for Mongo Driver
Apache License 2.0
13 stars 12 forks source link

Add support to java mongo driver 4 #21

Closed jorgerod closed 3 years ago

jorgerod commented 4 years ago

Hi

Spring Boot 2.3.x upgrade spring data mongo to 3.0.2. This version use mongo-driver-core 4.x.x and opentracing-contrib:java-mongo-driver:0.1.5 not support this version.

Thanks

olivierboudet commented 4 years ago

Is there a workaround to get mongo tracing even if java-mongo-driver:0.1.5̀ does not support if officially ?

emas80 commented 4 years ago

I've tried adding the support for Mongo v 4.0, but it's harder than expected. I've opened a ticket to the Jira board of the MongoDb java drivers. https://jira.mongodb.org/browse/JAVA-3873?filter=-2

Our workaround is to have a TracingMongoClient

public class TracingMongoClient implements MongoClient {

    private MongoClient mongoClient;

    public TracingMongoClient(TracingCommandListener listener, String mongoDbUri, String applicationName) {
        this(listener, mongoDbUri, applicationName, ReadPreference.primaryPreferred());
    }

    public TracingMongoClient(TracingCommandListener listener, String mongoDbUri, String applicationName, ReadPreference readPreference) {
        MongoClientSettings settings = MongoClientSettings.builder().addCommandListener(listener).applyConnectionString(new ConnectionString(mongoDbUri)).applicationName(applicationName)
                .readPreference(readPreference).build();
        this.mongoClient = create(settings);
    }

  /// wraps all the methods of the MongoClient interface
      /**
     * @param databaseName
     * @return
     * @see com.mongodb.client.internal.MongoClientImpl#getDatabase(java.lang.String)
     */
    @Override
    public MongoDatabase getDatabase(String databaseName) {
        return mongoClient.getDatabase(databaseName);
    }

   ....

and then to do something like this

public MongoClient mongoClient() {
        MongoClient mc;

        if (tracingEnabled) {
            TracingCommandListener commandListener = new TracingCommandListener.Builder(tracer).build();

            mc = new TracingMongoClient(commandListener, mongoDbUri, applicationName);
        } else {
            MongoClientSettings settings = MongoClientSettings.builder().applyConnectionString(new ConnectionString(mongoDbUri)).applicationName(applicationName)
                    .readPreference(primaryPreferred()).build();
            mc = create(settings);
        }

        logger.info("MongoDb Url for Primary will be {}", getAllAddresses(mc));
        return mc;
    }
}

It's less elegant compared to the Spring annotation-based way of doing it, but it gets the job done for now.

JoakimLofgren commented 3 years ago

Is @emas80 workaround similar to what opentelemetry is doing? https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/master/instrumentation/mongo

Can their solution be ported to opentracing?

I would really love to get this working with the latest spring boot version without doing a workaround.