From ddc6faf175fa60c873ce5943c4f95265e154dc9d Mon Sep 17 00:00:00 2001 From: Erica Pisani Date: Tue, 23 Jun 2026 13:25:16 -0400 Subject: [PATCH] fix(pymongo): Add db.query.text to streamed query spans Set the db.query.text span attribute for PyMongo command spans, matching the pattern used in the asyncpg integration (GH-6633). Co-Authored-By: Claude Sonnet 4.6 --- sentry_sdk/integrations/pymongo.py | 1 + tests/integrations/pymongo/test_pymongo.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/sentry_sdk/integrations/pymongo.py b/sentry_sdk/integrations/pymongo.py index e24332a816..2616f4d5a3 100644 --- a/sentry_sdk/integrations/pymongo.py +++ b/sentry_sdk/integrations/pymongo.py @@ -155,6 +155,7 @@ def started(self, event: "CommandStartedEvent") -> None: span_first_data = { "db.operation.name": operation_name, "db.collection.name": collection_name, + SPANDATA.DB_QUERY_TEXT: query, "sentry.op": OP.DB, "sentry.origin": PyMongoIntegration.origin, **db_data, diff --git a/tests/integrations/pymongo/test_pymongo.py b/tests/integrations/pymongo/test_pymongo.py index 4f350c8aea..0196eb71d4 100644 --- a/tests/integrations/pymongo/test_pymongo.py +++ b/tests/integrations/pymongo/test_pymongo.py @@ -162,6 +162,9 @@ def test_segment_span_streaming(sentry_init, capture_items, mongo_server, with_p assert insert_success["attributes"]["db.collection.name"] == "test_collection" assert insert_fail["attributes"]["db.collection.name"] == "erroneous" + for span in find, insert_success, insert_fail: + assert span["attributes"][SPANDATA.DB_QUERY_TEXT] == span["name"] + if with_pii: assert "1" in find["name"] assert "2" in insert_success["name"] @@ -247,6 +250,7 @@ def test_breadcrumbs_span_streaming(sentry_init, capture_items, mongo_server, wi assert data["db.driver.name"] == "pymongo" assert data["db.operation.name"] == "find" assert data["db.collection.name"] == "test_collection" + assert data["db.query.text"] == crumb["message"] assert data["sentry.op"] == "db" assert data["sentry.origin"] == "auto.db.pymongo" assert data[SPANDATA.SERVER_ADDRESS] == "localhost"