Skip to main content

OpenTracing support for PyMongo

Project description

This package enables tracing Mongo commands in a PyMongo MongoClient via The OpenTracing Project. Once a production system contends with real concurrency or splits into many services, crucial (and formerly easy) tasks become difficult: user-facing latency optimization, root-cause analysis of backend errors, communication about distinct pieces of a now-distributed system, etc. Distributed tracing follows a request on its journey from inception to completion from mobile/browser all the way to the microservices.

As core services and libraries adopt OpenTracing, the application builder is no longer burdened with the task of adding basic tracing instrumentation to their own code. In this way, developers can build their applications with the tools they prefer and benefit from built-in tracing instrumentation. OpenTracing implementations exist for major distributed tracing systems and can be bound or swapped with a one-line configuration change.

If you want to learn more about the underlying Python API, visit the Python source code.


Run the following command:

$ pip install pymongo-opentracing


This PyMongo monitor allows the tracing of database queries and commands using the OpenTracing API. All that it requires is for a CommandTracing listener instance to be initialized using an instance of an OpenTracing tracer and registered via the pymongo.monitoring module.


CommandTracing takes the Tracer instance that is supported by OpenTracing and an optional dictionary of desired tags for each created span. To create a CommandTracing object, you can either pass in a tracer object directly or default to the opentracing.tracer global tracer that’s set elsewhere in your application:

from pymongo_opentracing import CommandTracing
from pymongo import monitoring

opentracing_tracer = ## some OpenTracing tracer implementation
# All future clients will have CommandTracing registered for their events
                                   span_tags={'MyCustomTag': 'HelpfulVal'}))


from pymongo_opentracing import CommandTracing
import opentracing
import pymongo

opentracing.tracer = ## some OpenTracing tracer implementation
# Only this client will have CommandTracing trace their events
client = pymongo.MongoClient(event_listeners=[CommandTracing()])

Further Information

If you’re interested in learning more about the OpenTracing standard, please visit or join the mailing list. If you would like to implement OpenTracing in your project and need help, feel free to send us a note at

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Built Distribution

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page