PyMongo

Learn about the PyMongo integration and how it adds support for connections to MongoDB databases.

The PyMongo integration adds support for PyMongo, the official MongoDB driver.

Install

Install sentry-sdk from PyPI with the pymongo extra:

Copied
pip install "sentry-sdk[pymongo]"

Configure

If you have the pymongo package in your dependencies, the PyMongo integration will be enabled automatically when you initialize the Sentry SDK.

Copied
import sentry_sdk

sentry_sdk.init(
    dsn="___PUBLIC_DSN___",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # ___PRODUCT_OPTION_START___ performance
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # ___PRODUCT_OPTION_END___ performance
    # ___PRODUCT_OPTION_START___ profiling
    # To collect profiles for all profile sessions,
    # set `profile_session_sample_rate` to 1.0.
    profile_session_sample_rate=1.0,
    # Profiles will be automatically collected while
    # there is an active span.
    profile_lifecycle="trace",
    # ___PRODUCT_OPTION_END___ profiling
    # ___PRODUCT_OPTION_START___ logs

    # Enable logs to be sent to Sentry
    enable_logs=True,
    # ___PRODUCT_OPTION_END___ logs
)

Verify

Copied
import pymongo

def main():
    sentry_sdk.init(...)  # same as above
    client = pymongo.MongoClient(DATABASE_HOST, DATABASE_PORT)

    db = client["sentry_test_db"]
    collection = db["sentry_test"]

    with sentry_sdk.start_transaction(name="testing_sentry"):
        collection.insert_one({"test": "sentry"})
        collection.find_one({"test": "sentry"})

main()

This will create a transaction called testing_sentry in the Performance section of sentry.io, and create spans for the database operations.

It takes a couple of moments for the data to appear in sentry.io.

Behavior

The following information about your MongoDB queries will be available to you on Sentry.io:

  • Performance traces for all MongoDB queries
  • Breadcrumbs for all MongoDB queries
  • Personal identifiable information (PII) will be stripped from all MongoDB queries if send_default_pii is disabled in the SDK. (This was tested for PyMongo 4.2 and below, but "should" also be future proof)

Other MongoDB libraries

PyMongo is an official synchronous driver for MongoDB. It means that many other Python libraries interacting with MongoDB use it under the hood, like, for example mongoengine (always) or umongo (if selected from multiple available drivers). Official async MongoDB driver called Motor uses PyMongo under the hood as well. Queries generated by those libraries will also be monitored.

If you use a different driver (for example, TxMongo), this integration won't work.

Mongomock

While mongomock can be used to replace PyMongo in tests, it doesn't implement all features available in the official driver. This integration will not generate any breadcrumbs or spans from mongomock's clients.

Supported Versions

  • PyMongo: 3.1+
  • Python: Python 3.6+

The versions above apply for the current major version of the Python SDK. If you're looking to use Sentry with older Python or framework versions, consider using an older major version of the SDK.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").