PyMongo¶
The logfire.instrument_pymongo() method creates a span for each operation performed using the PyMongo client.
install¶
Install logfire using pymongo extra:
{{ install_logfire(extras=['pymongo']) }}
usage¶
The following example demonstrates how to useLogfirewith 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 a Python script¶
The following script connects to the MongoDB database, inserts the document, and queries it:
import logfire
from pymongo import MongoClient
logfire.configure()
logfire.instrument_pymongo(capture_statement=True) # (1)!
client = MongoClient()
db = client["database"]
collection = db["collection"]
collection.insert_one({"name": "MongoDB"})
collection.find_one()
-
将 capture_statementparameter is set toTrueTo capture executed statements.This is the default behavior on other OpenTelemetry instrumentation packages, but disabled by default in PyMongo.
logfire.instrument_pymongo() Keyword parameters are passed to OpenTelemetry pymongo Instrumentation package PymongoInstrumentor().instrument() Methods,replace this text with Read more about it.
本文总阅读量次