OpenTelemetry pymongo Instrumentation¶
The integration with MongoDB supports the pymongo library, it can be
enabled using the
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()
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
- opentelemetry.instrumentation.pymongo.dummy_callback(span, event)¶
- class opentelemetry.instrumentation.pymongo.CommandTracer(tracer, request_hook=<function dummy_callback>, response_hook=<function dummy_callback>, failed_hook=<function dummy_callback>)¶
Method to handle a pymongo CommandStartedEvent
Method to handle a pymongo CommandSucceededEvent
Method to handle a pymongo CommandFailedEvent
- class opentelemetry.instrumentation.pymongo.PymongoInstrumentor(*args, **kwargs)¶
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 setup.py.
For example, if an instrumentation instruments requests 1.x, this method should look like:
- 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.