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:
- 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
- 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.