Skip to content

Commit

Permalink
feat(pymongo): Send query description as valid JSON (#3291)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
0Calories authored Jul 15, 2024
1 parent 301c4b8 commit 84a2afc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
3 changes: 2 additions & 1 deletion sentry_sdk/integrations/pymongo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import json

import sentry_sdk
from sentry_sdk.consts import SPANSTATUS, SPANDATA, OP
Expand Down Expand Up @@ -154,7 +155,7 @@ def started(self, event):
if not should_send_default_pii():
command = _strip_pii(command)

query = "{}".format(command)
query = json.dumps(command, default=str)
span = sentry_sdk.start_span(
op=OP.DB,
description=query,
Expand Down
8 changes: 4 additions & 4 deletions tests/integrations/pymongo/test_pymongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def test_transactions(sentry_init, capture_events, mongo_server, with_pii):
assert insert_success["tags"]["db.operation"] == "insert"
assert insert_fail["tags"]["db.operation"] == "insert"

assert find["description"].startswith("{'find")
assert insert_success["description"].startswith("{'insert")
assert insert_fail["description"].startswith("{'insert")
assert find["description"].startswith('{"find')
assert insert_success["description"].startswith('{"insert')
assert insert_fail["description"].startswith('{"insert')

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

assert crumb["category"] == "query"
assert crumb["message"].startswith("{'find")
assert crumb["message"].startswith('{"find')
if with_pii:
assert "1" in crumb["message"]
else:
Expand Down

0 comments on commit 84a2afc

Please sign in to comment.