Skip to content

Commit 84a2afc

Browse files
authored
feat(pymongo): Send query description as valid JSON (#3291)
MongoDB queries were being sent as invalid JSON, since the keys and values were surrounded by single quotes instead of double quotes. Relay cannot parse the queries unless they are sent as valid JSON. This PR converts MongoDB queries into a JSON string before sending it on the span, so that Relay may properly parse it and extract metrics.
1 parent 301c4b8 commit 84a2afc

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

sentry_sdk/integrations/pymongo.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
import json
23

34
import sentry_sdk
45
from sentry_sdk.consts import SPANSTATUS, SPANDATA, OP
@@ -154,7 +155,7 @@ def started(self, event):
154155
if not should_send_default_pii():
155156
command = _strip_pii(command)
156157

157-
query = "{}".format(command)
158+
query = json.dumps(command, default=str)
158159
span = sentry_sdk.start_span(
159160
op=OP.DB,
160161
description=query,

tests/integrations/pymongo/test_pymongo.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ def test_transactions(sentry_init, capture_events, mongo_server, with_pii):
7171
assert insert_success["tags"]["db.operation"] == "insert"
7272
assert insert_fail["tags"]["db.operation"] == "insert"
7373

74-
assert find["description"].startswith("{'find")
75-
assert insert_success["description"].startswith("{'insert")
76-
assert insert_fail["description"].startswith("{'insert")
74+
assert find["description"].startswith('{"find')
75+
assert insert_success["description"].startswith('{"insert')
76+
assert insert_fail["description"].startswith('{"insert')
7777

7878
assert find["tags"][SPANDATA.DB_MONGODB_COLLECTION] == "test_collection"
7979
assert insert_success["tags"][SPANDATA.DB_MONGODB_COLLECTION] == "test_collection"
@@ -117,7 +117,7 @@ def test_breadcrumbs(sentry_init, capture_events, mongo_server, with_pii):
117117
(crumb,) = event["breadcrumbs"]["values"]
118118

119119
assert crumb["category"] == "query"
120-
assert crumb["message"].startswith("{'find")
120+
assert crumb["message"].startswith('{"find')
121121
if with_pii:
122122
assert "1" in crumb["message"]
123123
else:

0 commit comments

Comments
 (0)