|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import sys |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +from sentry_sdk._compat import reraise |
| 7 | +from sentry_sdk._types import MYPY |
| 8 | +from sentry_sdk import Hub |
| 9 | +from sentry_sdk.consts import OP, SENSITIVE_DATA_SUBSTITUTE |
| 10 | +from sentry_sdk.hub import _should_send_default_pii |
| 11 | +from sentry_sdk.integrations import DidNotEnable, Integration |
| 12 | +from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_TASK |
| 13 | +from sentry_sdk.utils import capture_internal_exceptions, event_from_exception |
| 14 | + |
| 15 | +if MYPY: |
| 16 | + from typing import Any, Callable, Optional, Union, TypeVar |
| 17 | + |
| 18 | + from sentry_sdk._types import EventProcessor, Event, Hint |
| 19 | + from sentry_sdk.utils import ExcInfo |
| 20 | + |
| 21 | + F = TypeVar("F", bound=Callable[..., Any]) |
| 22 | + |
| 23 | +try: |
| 24 | + from huey.api import Huey, Result, ResultGroup, Task |
| 25 | + from huey.exceptions import CancelExecution, RetryTask |
| 26 | +except ImportError: |
| 27 | + raise DidNotEnable("Huey is not installed") |
| 28 | + |
| 29 | + |
| 30 | +HUEY_CONTROL_FLOW_EXCEPTIONS = (CancelExecution, RetryTask) |
| 31 | + |
| 32 | + |
| 33 | +class HueyIntegration(Integration): |
| 34 | + identifier = "huey" |
| 35 | + |
| 36 | + @staticmethod |
| 37 | + def setup_once(): |
| 38 | + # type: () -> None |
| 39 | + patch_enqueue() |
| 40 | + patch_execute() |
| 41 | + |
| 42 | + |
| 43 | +def patch_enqueue(): |
| 44 | + # type: () -> None |
| 45 | + old_enqueue = Huey.enqueue |
| 46 | + |
| 47 | + def _sentry_enqueue(self, task): |
| 48 | + # type: (Huey, Task) -> Optional[Union[Result, ResultGroup]] |
| 49 | + hub = Hub.current |
| 50 | + |
| 51 | + if hub.get_integration(HueyIntegration) is None: |
| 52 | + return old_enqueue(self, task) |
| 53 | + |
| 54 | + with hub.start_span(op=OP.QUEUE_SUBMIT_HUEY, description=task.name): |
| 55 | + return old_enqueue(self, task) |
| 56 | + |
| 57 | + Huey.enqueue = _sentry_enqueue |
| 58 | + |
| 59 | + |
| 60 | +def _make_event_processor(task): |
| 61 | + # type: (Any) -> EventProcessor |
| 62 | + def event_processor(event, hint): |
| 63 | + # type: (Event, Hint) -> Optional[Event] |
| 64 | + |
| 65 | + with capture_internal_exceptions(): |
| 66 | + tags = event.setdefault("tags", {}) |
| 67 | + tags["huey_task_id"] = task.id |
| 68 | + tags["huey_task_retry"] = task.default_retries > task.retries |
| 69 | + extra = event.setdefault("extra", {}) |
| 70 | + extra["huey-job"] = { |
| 71 | + "task": task.name, |
| 72 | + "args": task.args |
| 73 | + if _should_send_default_pii() |
| 74 | + else SENSITIVE_DATA_SUBSTITUTE, |
| 75 | + "kwargs": task.kwargs |
| 76 | + if _should_send_default_pii() |
| 77 | + else SENSITIVE_DATA_SUBSTITUTE, |
| 78 | + "retry": (task.default_retries or 0) - task.retries, |
| 79 | + } |
| 80 | + |
| 81 | + return event |
| 82 | + |
| 83 | + return event_processor |
| 84 | + |
| 85 | + |
| 86 | +def _capture_exception(exc_info): |
| 87 | + # type: (ExcInfo) -> None |
| 88 | + hub = Hub.current |
| 89 | + |
| 90 | + if exc_info[0] in HUEY_CONTROL_FLOW_EXCEPTIONS: |
| 91 | + hub.scope.transaction.set_status("aborted") |
| 92 | + return |
| 93 | + |
| 94 | + hub.scope.transaction.set_status("internal_error") |
| 95 | + event, hint = event_from_exception( |
| 96 | + exc_info, |
| 97 | + client_options=hub.client.options if hub.client else None, |
| 98 | + mechanism={"type": HueyIntegration.identifier, "handled": False}, |
| 99 | + ) |
| 100 | + hub.capture_event(event, hint=hint) |
| 101 | + |
| 102 | + |
| 103 | +def _wrap_task_execute(func): |
| 104 | + # type: (F) -> F |
| 105 | + def _sentry_execute(*args, **kwargs): |
| 106 | + # type: (*Any, **Any) -> Any |
| 107 | + hub = Hub.current |
| 108 | + if hub.get_integration(HueyIntegration) is None: |
| 109 | + return func(*args, **kwargs) |
| 110 | + |
| 111 | + try: |
| 112 | + result = func(*args, **kwargs) |
| 113 | + except Exception: |
| 114 | + exc_info = sys.exc_info() |
| 115 | + _capture_exception(exc_info) |
| 116 | + reraise(*exc_info) |
| 117 | + |
| 118 | + return result |
| 119 | + |
| 120 | + return _sentry_execute # type: ignore |
| 121 | + |
| 122 | + |
| 123 | +def patch_execute(): |
| 124 | + # type: () -> None |
| 125 | + old_execute = Huey._execute |
| 126 | + |
| 127 | + def _sentry_execute(self, task, timestamp=None): |
| 128 | + # type: (Huey, Task, Optional[datetime]) -> Any |
| 129 | + hub = Hub.current |
| 130 | + |
| 131 | + if hub.get_integration(HueyIntegration) is None: |
| 132 | + return old_execute(self, task, timestamp) |
| 133 | + |
| 134 | + with hub.push_scope() as scope: |
| 135 | + with capture_internal_exceptions(): |
| 136 | + scope._name = "huey" |
| 137 | + scope.clear_breadcrumbs() |
| 138 | + scope.add_event_processor(_make_event_processor(task)) |
| 139 | + |
| 140 | + transaction = Transaction( |
| 141 | + name=task.name, |
| 142 | + status="ok", |
| 143 | + op=OP.QUEUE_TASK_HUEY, |
| 144 | + source=TRANSACTION_SOURCE_TASK, |
| 145 | + ) |
| 146 | + |
| 147 | + if not getattr(task, "_sentry_is_patched", False): |
| 148 | + task.execute = _wrap_task_execute(task.execute) |
| 149 | + task._sentry_is_patched = True |
| 150 | + |
| 151 | + with hub.start_transaction(transaction): |
| 152 | + return old_execute(self, task, timestamp) |
| 153 | + |
| 154 | + Huey._execute = _sentry_execute |
0 commit comments