Skip to content

Commit 431a521

Browse files
authored
ref(integrations): Use new scopes API in pymongo, clickhouse integration (#2862)
1 parent 9bd4436 commit 431a521

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

sentry_sdk/integrations/clickhouse_driver.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from sentry_sdk import Hub
1+
import sentry_sdk
22
from sentry_sdk.consts import OP, SPANDATA
3-
from sentry_sdk.hub import _should_send_default_pii
43
from sentry_sdk.integrations import Integration, DidNotEnable
54
from sentry_sdk.tracing import Span
65
from sentry_sdk._types import TYPE_CHECKING
6+
from sentry_sdk.scope import should_send_default_pii
77
from sentry_sdk.utils import capture_internal_exceptions
88

99
from typing import TypeVar
@@ -75,15 +75,14 @@ def setup_once() -> None:
7575

7676
def _wrap_start(f: Callable[P, T]) -> Callable[P, T]:
7777
def _inner(*args: P.args, **kwargs: P.kwargs) -> T:
78-
hub = Hub.current
79-
if hub.get_integration(ClickhouseDriverIntegration) is None:
78+
if sentry_sdk.get_client().get_integration(ClickhouseDriverIntegration) is None:
8079
return f(*args, **kwargs)
8180
connection = args[0]
8281
query = args[1]
8382
query_id = args[2] if len(args) > 2 else kwargs.get("query_id")
8483
params = args[3] if len(args) > 3 else kwargs.get("params")
8584

86-
span = hub.start_span(op=OP.DB, description=query)
85+
span = sentry_sdk.start_span(op=OP.DB, description=query)
8786

8887
connection._sentry_span = span # type: ignore[attr-defined]
8988

@@ -94,7 +93,7 @@ def _inner(*args: P.args, **kwargs: P.kwargs) -> T:
9493
if query_id:
9594
span.set_data("db.query_id", query_id)
9695

97-
if params and _should_send_default_pii():
96+
if params and should_send_default_pii():
9897
span.set_data("db.params", params)
9998

10099
# run the original code
@@ -112,7 +111,7 @@ def _inner_end(*args: P.args, **kwargs: P.kwargs) -> T:
112111
span = instance.connection._sentry_span # type: ignore[attr-defined]
113112

114113
if span is not None:
115-
if res is not None and _should_send_default_pii():
114+
if res is not None and should_send_default_pii():
116115
span.set_data("db.result", res)
117116

118117
with capture_internal_exceptions():
@@ -135,7 +134,7 @@ def _inner_send_data(*args: P.args, **kwargs: P.kwargs) -> T:
135134

136135
_set_db_data(span, instance.connection)
137136

138-
if _should_send_default_pii():
137+
if should_send_default_pii():
139138
db_params = span._data.get("db.params", [])
140139
db_params.extend(data)
141140
span.set_data("db.params", db_params)

sentry_sdk/integrations/pymongo.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import copy
22

3-
from sentry_sdk import Hub
3+
import sentry_sdk
44
from sentry_sdk.consts import SPANDATA
5-
from sentry_sdk.hub import _should_send_default_pii
65
from sentry_sdk.integrations import DidNotEnable, Integration
6+
from sentry_sdk.scope import should_send_default_pii
77
from sentry_sdk.tracing import Span
88
from sentry_sdk.utils import capture_internal_exceptions
99

@@ -116,9 +116,9 @@ def _operation_key(self, event):
116116

117117
def started(self, event):
118118
# type: (CommandStartedEvent) -> None
119-
hub = Hub.current
120-
if hub.get_integration(PyMongoIntegration) is None:
119+
if sentry_sdk.get_client().get_integration(PyMongoIntegration) is None:
121120
return
121+
122122
with capture_internal_exceptions():
123123
command = dict(copy.deepcopy(event.command))
124124

@@ -152,11 +152,11 @@ def started(self, event):
152152
except KeyError:
153153
pass
154154

155-
if not _should_send_default_pii():
155+
if not should_send_default_pii():
156156
command = _strip_pii(command)
157157

158158
query = "{} {}".format(event.command_name, command)
159-
span = hub.start_span(op=op, description=query)
159+
span = sentry_sdk.start_span(op=op, description=query)
160160

161161
for tag, value in tags.items():
162162
span.set_tag(tag, value)
@@ -165,14 +165,15 @@ def started(self, event):
165165
span.set_data(key, value)
166166

167167
with capture_internal_exceptions():
168-
hub.add_breadcrumb(message=query, category="query", type=op, data=tags)
168+
sentry_sdk.add_breadcrumb(
169+
message=query, category="query", type=op, data=tags
170+
)
169171

170172
self._ongoing_operations[self._operation_key(event)] = span.__enter__()
171173

172174
def failed(self, event):
173175
# type: (CommandFailedEvent) -> None
174-
hub = Hub.current
175-
if hub.get_integration(PyMongoIntegration) is None:
176+
if sentry_sdk.get_client().get_integration(PyMongoIntegration) is None:
176177
return
177178

178179
try:
@@ -184,8 +185,7 @@ def failed(self, event):
184185

185186
def succeeded(self, event):
186187
# type: (CommandSucceededEvent) -> None
187-
hub = Hub.current
188-
if hub.get_integration(PyMongoIntegration) is None:
188+
if sentry_sdk.get_client().get_integration(PyMongoIntegration) is None:
189189
return
190190

191191
try:

0 commit comments

Comments
 (0)