|
| 1 | +--- |
| 2 | +title: PyMongo |
| 3 | +description: "Learn about using Sentry with PyMongo." |
| 4 | +--- |
| 5 | + |
| 6 | +The PyMongo integration adds support for [PyMongo](https://www.mongodb.com/docs/drivers/pymongo/), the official MongoDB driver. |
| 7 | + |
| 8 | + |
| 9 | +## Install |
| 10 | + |
| 11 | +Install `sentry-sdk` from PyPI with the `pymongo` extra: |
| 12 | + |
| 13 | +```bash |
| 14 | +pip install --upgrade 'sentry-sdk[pymongo]' |
| 15 | +``` |
| 16 | + |
| 17 | +## Configure |
| 18 | + |
| 19 | +To configure the SDK, initialize it before creating any of PyMongo's MongoClient instances: |
| 20 | + |
| 21 | +```python |
| 22 | +import sentry_sdk |
| 23 | +from sentry_sdk.integrations.pymongo import PyMongoIntegration |
| 24 | + |
| 25 | + |
| 26 | +sentry_sdk.init( |
| 27 | + dsn="___PUBLIC_DSN___", |
| 28 | + integrations=[ |
| 29 | + PyMongoIntegration(), |
| 30 | + ], |
| 31 | + |
| 32 | + # Set traces_sample_rate to 1.0 to capture 100% |
| 33 | + # of transactions for performance monitoring. |
| 34 | + # We recommend adjusting this value in production, |
| 35 | + traces_sample_rate=1.0, |
| 36 | +) |
| 37 | +``` |
| 38 | + |
| 39 | +<Note> |
| 40 | + |
| 41 | +In the future, we will make this auto-enabling, so you won't need to add the `PyMongoIntegration` to your |
| 42 | +`sentry_sdk.init()` call. Instead, the Sentry SDK will detect the presence of PyMongo and will set up the integration |
| 43 | +automatically. |
| 44 | + |
| 45 | +</Note> |
| 46 | + |
| 47 | + |
| 48 | +## Behavior |
| 49 | + |
| 50 | +The following information about your MonogDB queries will be available to you on Sentry.io: |
| 51 | + |
| 52 | +- Performance traces for all MongoDB queries |
| 53 | +- Breadcrumbs for all MongoDB queries |
| 54 | +- 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) |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +### Other MongoDB libraries |
| 59 | + |
| 60 | +PyMongo is an official synchronous driver for MongoDB. It means that many other Python libraries interacting with |
| 61 | +MongoDB use it under the hood, like, for example `mongoengine` (always) or `umongo` (if selected from multiple available |
| 62 | +drivers). Official async MongoDB driver called `Motor` uses PyMongo under the hood as well. |
| 63 | +Queries generated by those libraries will also be monitored. |
| 64 | + |
| 65 | +If you use a different driver (for example, `TxMongo`), this integration won't work. |
| 66 | + |
| 67 | +### Mongomock |
| 68 | + |
| 69 | +While `mongomock` can be used to replace `PyMongo` in tests, it doesn't implement all features available in the official |
| 70 | +driver. This integration will not generate any breadcrumbs or spans from `mongomock`'s clients. |
0 commit comments