Skip to content

Commit 22611a1

Browse files
authored
ref(huey): Use new scopes API (#2880)
1 parent acb8eae commit 22611a1

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

sentry_sdk/integrations/huey.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import sys
22
from datetime import datetime
33

4+
import sentry_sdk
45
from sentry_sdk._types import TYPE_CHECKING
5-
from sentry_sdk import Hub
66
from sentry_sdk.api import continue_trace, get_baggage, get_traceparent
77
from sentry_sdk.consts import OP
8-
from sentry_sdk.hub import _should_send_default_pii
98
from sentry_sdk.integrations import DidNotEnable, Integration
9+
from sentry_sdk.scope import Scope, should_send_default_pii
1010
from sentry_sdk.tracing import (
1111
BAGGAGE_HEADER_NAME,
1212
SENTRY_TRACE_HEADER_NAME,
1313
TRANSACTION_SOURCE_TASK,
1414
)
15-
from sentry_sdk.scope import Scope
1615
from sentry_sdk.utils import (
1716
capture_internal_exceptions,
17+
ensure_integration_enabled,
1818
event_from_exception,
1919
SENSITIVE_DATA_SUBSTITUTE,
2020
reraise,
@@ -52,14 +52,10 @@ def patch_enqueue():
5252
# type: () -> None
5353
old_enqueue = Huey.enqueue
5454

55+
@ensure_integration_enabled(HueyIntegration, old_enqueue)
5556
def _sentry_enqueue(self, task):
5657
# type: (Huey, Task) -> Optional[Union[Result, ResultGroup]]
57-
hub = Hub.current
58-
59-
if hub.get_integration(HueyIntegration) is None:
60-
return old_enqueue(self, task)
61-
62-
with hub.start_span(op=OP.QUEUE_SUBMIT_HUEY, description=task.name):
58+
with sentry_sdk.start_span(op=OP.QUEUE_SUBMIT_HUEY, description=task.name):
6359
if not isinstance(task, PeriodicTask):
6460
# Attach trace propagation data to task kwargs. We do
6561
# not do this for periodic tasks, as these don't
@@ -87,12 +83,12 @@ def event_processor(event, hint):
8783
"task": task.name,
8884
"args": (
8985
task.args
90-
if _should_send_default_pii()
86+
if should_send_default_pii()
9187
else SENSITIVE_DATA_SUBSTITUTE
9288
),
9389
"kwargs": (
9490
task.kwargs
95-
if _should_send_default_pii()
91+
if should_send_default_pii()
9692
else SENSITIVE_DATA_SUBSTITUTE
9793
),
9894
"retry": (task.default_retries or 0) - task.retries,
@@ -122,12 +118,10 @@ def _capture_exception(exc_info):
122118

123119
def _wrap_task_execute(func):
124120
# type: (F) -> F
121+
122+
@ensure_integration_enabled(HueyIntegration, func)
125123
def _sentry_execute(*args, **kwargs):
126124
# type: (*Any, **Any) -> Any
127-
hub = Hub.current
128-
if hub.get_integration(HueyIntegration) is None:
129-
return func(*args, **kwargs)
130-
131125
try:
132126
result = func(*args, **kwargs)
133127
except Exception:
@@ -144,14 +138,10 @@ def patch_execute():
144138
# type: () -> None
145139
old_execute = Huey._execute
146140

141+
@ensure_integration_enabled(HueyIntegration, old_execute)
147142
def _sentry_execute(self, task, timestamp=None):
148143
# type: (Huey, Task, Optional[datetime]) -> Any
149-
hub = Hub.current
150-
151-
if hub.get_integration(HueyIntegration) is None:
152-
return old_execute(self, task, timestamp)
153-
154-
with hub.push_scope() as scope:
144+
with sentry_sdk.isolation_scope() as scope:
155145
with capture_internal_exceptions():
156146
scope._name = "huey"
157147
scope.clear_breadcrumbs()
@@ -171,7 +161,7 @@ def _sentry_execute(self, task, timestamp=None):
171161
task.execute = _wrap_task_execute(task.execute)
172162
task._sentry_is_patched = True
173163

174-
with hub.start_transaction(transaction):
164+
with sentry_sdk.start_transaction(transaction):
175165
return old_execute(self, task, timestamp)
176166

177167
Huey._execute = _sentry_execute

0 commit comments

Comments
 (0)