OpenTracing Instrumentation for Mongo Driver
Clone or download

README.md

Build Status Coverage Status Released Version

OpenTracing Mongo Driver Instrumentation

OpenTracing instrumentation for Mongo Driver.

Installation

pom.xml

<dependency>
    <groupId>io.opentracing.contrib</groupId>
    <artifactId>opentracing-mongo-driver</artifactId>
    <version>VERSION</version>
</dependency>

Usage

// Instantiate tracer
Tracer tracer = ...

// Optionally register tracer with GlobalTracer
GlobalTracer.register(tracer);

There are 2 ways to instrument MongoClient:

  • using Mongo Tracing Client
  • using MongoClientSettings.Builder with TracingCommandListener

Mongo Tracing Client

// Instantiate Synchronous Tracing MongoClient
MongoClient mongoClient = new TracingMongoClient(tracer, ...);

// Instantiate Asynchronous Tracing MongoClient
MongoClient mongoClient = new TracingAsyncMongoClient(tracer, ...);

MongoClientSettings.Builder with TracingCommandListener

// Add TracingCommandListener to MongoClientSettings.Builder
MongoClient mongoClient = MongoClients.create(
        MongoClientSettings.builder()
                .addCommandListener(new TracingCommandListener(tracer))
                ...
                .build());

Mongo Span Name

By default, span names are set to the operation performed by the Mongo client. To customize the span name, provide a MongoSpanNameProvider to the client that alters the span name. If a provder is not provided, the span name will remain the default.

// Create TracingMongoClient with custom span name
TracingMongoClient client = new TracingMongoClient(
    tracer, 
    replicaSetAddresses, 
    credentials, 
    clientOptions, 
    new PrefixSpanNameProvider("mongo."));
Document doc = new Document();
client.getDatabase("db").getCollection("collection).insertOne(doc);
// Span name is now set to "mongo.insert"

License

Apache 2.0 License.