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