Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(huey): Use new scopes API #2880

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions sentry_sdk/integrations/huey.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import sys
from datetime import datetime

import sentry_sdk
from sentry_sdk._types import TYPE_CHECKING
from sentry_sdk import Hub
from sentry_sdk.api import continue_trace, get_baggage, get_traceparent
from sentry_sdk.consts import OP
from sentry_sdk.hub import _should_send_default_pii
from sentry_sdk.integrations import DidNotEnable, Integration
from sentry_sdk.scope import Scope, should_send_default_pii
from sentry_sdk.tracing import (
BAGGAGE_HEADER_NAME,
SENTRY_TRACE_HEADER_NAME,
TRANSACTION_SOURCE_TASK,
)
from sentry_sdk.scope import Scope
from sentry_sdk.utils import (
capture_internal_exceptions,
ensure_integration_enabled,
event_from_exception,
SENSITIVE_DATA_SUBSTITUTE,
reraise,
Expand Down Expand Up @@ -52,14 +52,10 @@ def patch_enqueue():
# type: () -> None
old_enqueue = Huey.enqueue

@ensure_integration_enabled(HueyIntegration, old_enqueue)
def _sentry_enqueue(self, task):
# type: (Huey, Task) -> Optional[Union[Result, ResultGroup]]
hub = Hub.current

if hub.get_integration(HueyIntegration) is None:
return old_enqueue(self, task)

with hub.start_span(op=OP.QUEUE_SUBMIT_HUEY, description=task.name):
with sentry_sdk.start_span(op=OP.QUEUE_SUBMIT_HUEY, description=task.name):
if not isinstance(task, PeriodicTask):
# Attach trace propagation data to task kwargs. We do
# not do this for periodic tasks, as these don't
Expand Down Expand Up @@ -87,12 +83,12 @@ def event_processor(event, hint):
"task": task.name,
"args": (
task.args
if _should_send_default_pii()
if should_send_default_pii()
else SENSITIVE_DATA_SUBSTITUTE
),
"kwargs": (
task.kwargs
if _should_send_default_pii()
if should_send_default_pii()
else SENSITIVE_DATA_SUBSTITUTE
),
"retry": (task.default_retries or 0) - task.retries,
Expand Down Expand Up @@ -122,12 +118,10 @@ def _capture_exception(exc_info):

def _wrap_task_execute(func):
# type: (F) -> F

@ensure_integration_enabled(HueyIntegration, func)
def _sentry_execute(*args, **kwargs):
# type: (*Any, **Any) -> Any
hub = Hub.current
if hub.get_integration(HueyIntegration) is None:
return func(*args, **kwargs)

try:
result = func(*args, **kwargs)
except Exception:
Expand All @@ -144,14 +138,10 @@ def patch_execute():
# type: () -> None
old_execute = Huey._execute

@ensure_integration_enabled(HueyIntegration, old_execute)
def _sentry_execute(self, task, timestamp=None):
# type: (Huey, Task, Optional[datetime]) -> Any
hub = Hub.current

if hub.get_integration(HueyIntegration) is None:
return old_execute(self, task, timestamp)

with hub.push_scope() as scope:
szokeasaurusrex marked this conversation as resolved.
Show resolved Hide resolved
with sentry_sdk.isolation_scope() as scope:
with capture_internal_exceptions():
scope._name = "huey"
scope.clear_breadcrumbs()
Expand All @@ -171,7 +161,7 @@ def _sentry_execute(self, task, timestamp=None):
task.execute = _wrap_task_execute(task.execute)
task._sentry_is_patched = True

with hub.start_transaction(transaction):
with sentry_sdk.start_transaction(transaction):
return old_execute(self, task, timestamp)

Huey._execute = _sentry_execute
Loading