OpenTelemetry pymongo Instrumentation

The integration with MongoDB supports the pymongo library, it can be enabled using the PymongoInstrumentor.

Usage

from pymongo import MongoClient
from opentelemetry.instrumentation.pymongo import PymongoInstrumentor

PymongoInstrumentor().instrument()
client = MongoClient()
db = client["MongoDB_Database"]
collection = db["MongoDB_Collection"]
collection.find_one()

API

The instrument method accepts the following keyword args:

tracer_provider (TracerProvider) - an optional tracer provider request_hook (Callable) - a function with extra user-defined logic to be performed before querying mongodb this function signature is: def request_hook(span: Span, event: CommandStartedEvent) -> None response_hook (Callable) - a function with extra user-defined logic to be performed after the query returns with a successful response this function signature is: def response_hook(span: Span, event: CommandSucceededEvent) -> None failed_hook (Callable) - a function with extra user-defined logic to be performed after the query returns with a failed response this function signature is: def failed_hook(span: Span, event: CommandFailedEvent) -> None capture_statement (bool) - an optional value to enable capturing the database statement that is being executed

for example:

opentelemetry.instrumentation.pymongo.dummy_callback(span, event)[source]
class opentelemetry.instrumentation.pymongo.CommandTracer(tracer, request_hook=<function dummy_callback>, response_hook=<function dummy_callback>, failed_hook=<function dummy_callback>, capture_statement=False)[source]

Bases: CommandListener

started(event)[source]

Method to handle a pymongo CommandStartedEvent

succeeded(event)[source]

Method to handle a pymongo CommandSucceededEvent

failed(event)[source]

Method to handle a pymongo CommandFailedEvent

class opentelemetry.instrumentation.pymongo.PymongoInstrumentor(*args, **kwargs)[source]

Bases: BaseInstrumentor

instrumentation_dependencies()[source]

Return a list of python packages with versions that the will be instrumented.

The format should be the same as used in requirements.txt or pyproject.toml.

For example, if an instrumentation instruments requests 1.x, this method should look like: :rtype: Collection[str]

def instrumentation_dependencies(self) -> Collection[str]:

return [‘requests ~= 1.0’]

This will ensure that the instrumentation will only be used when the specified library is present in the environment.