Skip to content

Commit e7262e9

Browse files
authored
fix(pymongo): Add db.query.text to streamed query spans (#6637)
Sets `db.query.text` on PyMongo command spans when span streaming is enabled. Fixes PY-2548 Fixes #6636
1 parent 0546bdf commit e7262e9

2 files changed

Lines changed: 5 additions & 0 deletions

File tree

sentry_sdk/integrations/pymongo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ def started(self, event: "CommandStartedEvent") -> None:
155155
span_first_data = {
156156
"db.operation.name": operation_name,
157157
"db.collection.name": collection_name,
158+
SPANDATA.DB_QUERY_TEXT: query,
158159
"sentry.op": OP.DB,
159160
"sentry.origin": PyMongoIntegration.origin,
160161
**db_data,

tests/integrations/pymongo/test_pymongo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ def test_segment_span_streaming(sentry_init, capture_items, mongo_server, with_p
162162
assert insert_success["attributes"]["db.collection.name"] == "test_collection"
163163
assert insert_fail["attributes"]["db.collection.name"] == "erroneous"
164164

165+
for span in find, insert_success, insert_fail:
166+
assert span["attributes"][SPANDATA.DB_QUERY_TEXT] == span["name"]
167+
165168
if with_pii:
166169
assert "1" in find["name"]
167170
assert "2" in insert_success["name"]
@@ -247,6 +250,7 @@ def test_breadcrumbs_span_streaming(sentry_init, capture_items, mongo_server, wi
247250
assert data["db.driver.name"] == "pymongo"
248251
assert data["db.operation.name"] == "find"
249252
assert data["db.collection.name"] == "test_collection"
253+
assert data["db.query.text"] == crumb["message"]
250254
assert data["sentry.op"] == "db"
251255
assert data["sentry.origin"] == "auto.db.pymongo"
252256
assert data[SPANDATA.SERVER_ADDRESS] == "localhost"

0 commit comments

Comments
 (0)