|
6 | 6 | from sentry_sdk._compat import reraise
|
7 | 7 | from sentry_sdk._types import TYPE_CHECKING
|
8 | 8 | from sentry_sdk import Hub
|
| 9 | +from sentry_sdk.api import continue_trace, get_baggage, get_traceparent |
9 | 10 | from sentry_sdk.consts import OP
|
10 | 11 | from sentry_sdk.hub import _should_send_default_pii
|
11 | 12 | from sentry_sdk.integrations import DidNotEnable, Integration
|
12 |
| -from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_TASK |
| 13 | +from sentry_sdk.tracing import ( |
| 14 | + BAGGAGE_HEADER_NAME, |
| 15 | + SENTRY_TRACE_HEADER_NAME, |
| 16 | + TRANSACTION_SOURCE_TASK, |
| 17 | +) |
13 | 18 | from sentry_sdk.utils import (
|
14 | 19 | capture_internal_exceptions,
|
15 | 20 | event_from_exception,
|
|
25 | 30 | F = TypeVar("F", bound=Callable[..., Any])
|
26 | 31 |
|
27 | 32 | try:
|
28 |
| - from huey.api import Huey, Result, ResultGroup, Task |
| 33 | + from huey.api import Huey, Result, ResultGroup, Task, PeriodicTask |
29 | 34 | from huey.exceptions import CancelExecution, RetryTask, TaskLockedException
|
30 | 35 | except ImportError:
|
31 | 36 | raise DidNotEnable("Huey is not installed")
|
@@ -56,6 +61,14 @@ def _sentry_enqueue(self, task):
|
56 | 61 | return old_enqueue(self, task)
|
57 | 62 |
|
58 | 63 | with hub.start_span(op=OP.QUEUE_SUBMIT_HUEY, description=task.name):
|
| 64 | + if not isinstance(task, PeriodicTask): |
| 65 | + # Attach trace propagation data to task kwargs. We do |
| 66 | + # not do this for periodic tasks, as these don't |
| 67 | + # really have an originating transaction. |
| 68 | + task.kwargs["sentry_headers"] = { |
| 69 | + BAGGAGE_HEADER_NAME: get_baggage(), |
| 70 | + SENTRY_TRACE_HEADER_NAME: get_traceparent(), |
| 71 | + } |
59 | 72 | return old_enqueue(self, task)
|
60 | 73 |
|
61 | 74 | Huey.enqueue = _sentry_enqueue
|
@@ -145,12 +158,15 @@ def _sentry_execute(self, task, timestamp=None):
|
145 | 158 | scope.clear_breadcrumbs()
|
146 | 159 | scope.add_event_processor(_make_event_processor(task))
|
147 | 160 |
|
148 |
| - transaction = Transaction( |
| 161 | + sentry_headers = task.kwargs.pop("sentry_headers", None) |
| 162 | + |
| 163 | + transaction = continue_trace( |
| 164 | + sentry_headers or {}, |
149 | 165 | name=task.name,
|
150 |
| - status="ok", |
151 | 166 | op=OP.QUEUE_TASK_HUEY,
|
152 | 167 | source=TRANSACTION_SOURCE_TASK,
|
153 | 168 | )
|
| 169 | + transaction.set_status("ok") |
154 | 170 |
|
155 | 171 | if not getattr(task, "_sentry_is_patched", False):
|
156 | 172 | task.execute = _wrap_task_execute(task.execute)
|
|
0 commit comments