Skip to content

PyMongo

The logfire.instrument_pymongo() method will create a span for every operation performed using your PyMongo clients.

Also works with Motor... πŸš—

This integration also works with motor, the asynchronous driver for MongoDB.

InstallationΒΆ

Install logfire with the pymongo extra:

pip install 'logfire[pymongo]'
uv add 'logfire[pymongo]'
poetry add 'logfire[pymongo]'

UsageΒΆ

The following example demonstrates how to use Logfire with PyMongo.

Run Mongo on Docker (Optional)ΒΆ

If you already have a MongoDB instance running, you can skip this step. Otherwise, you can start MongoDB using Docker with the following command:

docker run --name mongo -p 27017:27017 -d mongo:latest

Run the Python scriptΒΆ

The following script connects to a MongoDB database, inserts a document, and queries it:

import logfire
from pymongo import MongoClient

logfire.configure()
logfire.instrument_pymongo()

client = MongoClient()
db = client["database"]
collection = db["collection"]
collection.insert_one({"name": "MongoDB"})
collection.find_one()
import asyncio
import logfire
from motor.motor_asyncio import AsyncIOMotorClient

logfire.configure()
logfire.instrument_pymongo()

async def main():
    client = AsyncIOMotorClient()
    db = client["database"]
    collection = db["collection"]
    await collection.insert_one({"name": "MongoDB"})
    await collection.find_one()

asyncio.run(main())

Info

You can pass capture_statement=True to logfire.instrument_pymongo() to capture the queries.

By default, it is set to False to avoid capturing sensitive information.

The keyword arguments of logfire.instrument_pymongo() are passed to the PymongoInstrumentor().instrument() method of the OpenTelemetry pymongo Instrumentation package, read more about it here.

API ReferenceΒΆ

instrument_pymongo ΒΆ

instrument_pymongo(
    capture_statement: bool = False,
    request_hook: (
        Callable[[Span, CommandStartedEvent], None] | None
    ) = None,
    response_hook: (
        Callable[[Span, CommandSucceededEvent], None] | None
    ) = None,
    failed_hook: (
        Callable[[Span, CommandFailedEvent], None] | None
    ) = None,
    **kwargs: Any,
) -> None

Instrument the pymongo module so that spans are automatically created for each operation.

Uses the OpenTelemetry pymongo Instrumentation library, specifically PymongoInstrumentor().instrument(), to which it passes **kwargs.

Parameters:

Name Type Description Default
capture_statement ΒΆ
bool

Set to True to capture the statement in the span attributes.

False
request_hook ΒΆ
Callable[[Span, CommandStartedEvent], None] | None

A function called when a command is sent to the server.

None
response_hook ΒΆ
Callable[[Span, CommandSucceededEvent], None] | None

A function that is called when a command is successfully completed.

None
failed_hook ΒΆ
Callable[[Span, CommandFailedEvent], None] | None

A function that is called when a command fails.

None
**kwargs ΒΆ
Any

Additional keyword arguments to pass to the OpenTelemetry instrument methods for future compatibility.

{}