Skip to content

Commit 2c8cfbd

Browse files
committed
fix pymongo
1 parent 11e1961 commit 2c8cfbd

File tree

3 files changed

+33
-37
lines changed

3 files changed

+33
-37
lines changed

MIGRATION_GUIDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
2525
- Spans no longer have a `description`. Use `name` instead.
2626
- Dropped support for Python 3.6.
2727
- The PyMongo integration no longer sets tags. The data is still accessible via `data`.
28+
- The PyMongo integration doesn't set `operation_ids` anymore. The individual IDs (`operation_id`, `request_id`, `session_id`) are now accessible on the top level.
2829
- `sentry_sdk.metrics` and associated metrics APIs have been removed as Sentry no longer accepts metrics data in this form. See https://sentry.zendesk.com/hc/en-us/articles/26369339769883-Upcoming-API-Changes-to-Metrics
2930
- The experimental options `enable_metrics`, `before_emit_metric` and `metric_code_locations` have been removed.
3031
- When setting span status, the HTTP status code is no longer automatically added as a tag.

sentry_sdk/integrations/pymongo.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import copy
2-
import json
32

43
import sentry_sdk
54
from sentry_sdk.consts import SPANSTATUS, SPANDATA, OP
65
from sentry_sdk.integrations import DidNotEnable, Integration
76
from sentry_sdk.scope import should_send_default_pii
87
from sentry_sdk.tracing import Span
9-
from sentry_sdk.utils import capture_internal_exceptions
8+
from sentry_sdk.utils import capture_internal_exceptions, _serialize_span_attribute
109

1110
try:
1211
from pymongo import monitoring
@@ -127,52 +126,49 @@ def started(self, event):
127126
command.pop("$clusterTime", None)
128127
command.pop("$signature", None)
129128

130-
tags = {
131-
"db.name": event.database_name,
129+
data = {
130+
SPANDATA.DB_NAME: event.database_name,
132131
SPANDATA.DB_SYSTEM: "mongodb",
133132
SPANDATA.DB_OPERATION: event.command_name,
134133
SPANDATA.DB_MONGODB_COLLECTION: command.get(event.command_name),
135134
}
136135

137136
try:
138-
tags["net.peer.name"] = event.connection_id[0]
139-
tags["net.peer.port"] = str(event.connection_id[1])
137+
data["net.peer.name"] = event.connection_id[0]
138+
data["net.peer.port"] = str(event.connection_id[1])
140139
except TypeError:
141140
pass
142141

143-
data = {"operation_ids": {}} # type: Dict[str, Any]
144-
data["operation_ids"]["operation"] = event.operation_id
145-
data["operation_ids"]["request"] = event.request_id
146-
147-
data.update(_get_db_data(event))
148-
149142
try:
150143
lsid = command.pop("lsid")["id"]
151-
data["operation_ids"]["session"] = str(lsid)
144+
data["session_id"] = str(lsid)
152145
except KeyError:
153146
pass
154147

155148
if not should_send_default_pii():
156149
command = _strip_pii(command)
157150

158-
query = json.dumps(command, default=str)
151+
query = _serialize_span_attribute(command)
159152
span = sentry_sdk.start_span(
160153
op=OP.DB,
161154
name=query,
162155
origin=PyMongoIntegration.origin,
163156
)
164157

165-
for tag, value in tags.items():
166-
span.set_data(tag, value)
167-
168-
for key, value in data.items():
169-
span.set_data(key, value)
170-
171158
with capture_internal_exceptions():
172159
sentry_sdk.add_breadcrumb(
173-
message=query, category="query", type=OP.DB, data=tags
160+
message=query, category="query", type=OP.DB, data=data
174161
)
175162

163+
for key, value in data.items():
164+
span.set_attribute(key, value)
165+
166+
for key, value in _get_db_data(event).items():
167+
span.set_attribute(key, value)
168+
169+
span.set_attribute("operation_id", event.operation_id)
170+
span.set_attribute("request_id", event.request_id)
171+
176172
self._ongoing_operations[self._operation_key(event)] = span.__enter__()
177173

178174
def failed(self, event):

tests/integrations/pymongo/test_pymongo.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_transactions(sentry_init, capture_events, mongo_server, with_pii):
4949
(event,) = events
5050
(find, insert_success, insert_fail) = event["spans"]
5151

52-
common_tags = {
52+
common_data = {
5353
"db.name": "test_db",
5454
"db.system": "mongodb",
5555
"net.peer.name": mongo_server.host,
@@ -60,31 +60,24 @@ def test_transactions(sentry_init, capture_events, mongo_server, with_pii):
6060
assert span["data"][SPANDATA.DB_NAME] == "test_db"
6161
assert span["data"][SPANDATA.SERVER_ADDRESS] == "localhost"
6262
assert span["data"][SPANDATA.SERVER_PORT] == mongo_server.port
63-
for field, value in common_tags.items():
64-
assert span["tags"][field] == value
63+
for field, value in common_data.items():
6564
assert span["data"][field] == value
6665

6766
assert find["op"] == "db"
6867
assert insert_success["op"] == "db"
6968
assert insert_fail["op"] == "db"
7069

7170
assert find["data"]["db.operation"] == "find"
72-
assert find["tags"]["db.operation"] == "find"
7371
assert insert_success["data"]["db.operation"] == "insert"
74-
assert insert_success["tags"]["db.operation"] == "insert"
7572
assert insert_fail["data"]["db.operation"] == "insert"
76-
assert insert_fail["tags"]["db.operation"] == "insert"
7773

7874
assert find["description"].startswith('{"find')
7975
assert insert_success["description"].startswith('{"insert')
8076
assert insert_fail["description"].startswith('{"insert')
8177

8278
assert find["data"][SPANDATA.DB_MONGODB_COLLECTION] == "test_collection"
83-
assert find["tags"][SPANDATA.DB_MONGODB_COLLECTION] == "test_collection"
8479
assert insert_success["data"][SPANDATA.DB_MONGODB_COLLECTION] == "test_collection"
85-
assert insert_success["tags"][SPANDATA.DB_MONGODB_COLLECTION] == "test_collection"
8680
assert insert_fail["data"][SPANDATA.DB_MONGODB_COLLECTION] == "erroneous"
87-
assert insert_fail["tags"][SPANDATA.DB_MONGODB_COLLECTION] == "erroneous"
8881
if with_pii:
8982
assert "1" in find["description"]
9083
assert "2" in insert_success["description"]
@@ -99,16 +92,22 @@ def test_transactions(sentry_init, capture_events, mongo_server, with_pii):
9992
and "4" not in insert_fail["description"]
10093
)
10194

102-
assert find["tags"]["status"] == "ok"
103-
assert insert_success["tags"]["status"] == "ok"
104-
assert insert_fail["tags"]["status"] == "internal_error"
10595

106-
107-
@pytest.mark.parametrize("with_pii", [False, True])
108-
def test_breadcrumbs(sentry_init, capture_events, mongo_server, with_pii):
96+
@pytest.mark.parametrize(
97+
"with_pii,traces_sample_rate",
98+
[
99+
[False, 0.0],
100+
[False, 1.0],
101+
[True, 0.0],
102+
[True, 1.0],
103+
],
104+
)
105+
def test_breadcrumbs(
106+
sentry_init, capture_events, mongo_server, with_pii, traces_sample_rate
107+
):
109108
sentry_init(
110109
integrations=[PyMongoIntegration()],
111-
traces_sample_rate=1.0,
110+
traces_sample_rate=traces_sample_rate,
112111
send_default_pii=with_pii,
113112
)
114113
events = capture_events()

0 commit comments

Comments
 (0)