forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdramatiq.py
More file actions
246 lines (199 loc) · 8.02 KB
/
Copy pathdramatiq.py
File metadata and controls
246 lines (199 loc) · 8.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import json
from typing import TypeVar
import sentry_sdk
from sentry_sdk.api import continue_trace, get_baggage, get_traceparent
from sentry_sdk.consts import OP, SPANSTATUS
from sentry_sdk.integrations import DidNotEnable, Integration
from sentry_sdk.integrations._wsgi_common import request_body_within_bounds
from sentry_sdk.traces import SegmentSource
from sentry_sdk.tracing import (
BAGGAGE_HEADER_NAME,
SENTRY_TRACE_HEADER_NAME,
TransactionSource,
)
from sentry_sdk.tracing_utils import has_span_streaming_enabled
from sentry_sdk.utils import (
AnnotatedValue,
capture_internal_exceptions,
event_from_exception,
)
R = TypeVar("R")
try:
from dramatiq.broker import Broker
from dramatiq.errors import Retry
from dramatiq.message import Message
from dramatiq.middleware import Middleware, default_middleware
except ImportError:
raise DidNotEnable("Dramatiq is not installed")
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Any, Callable, Dict, Optional, Union
from sentry_sdk._types import Event, Hint
class DramatiqIntegration(Integration):
"""
Dramatiq integration for Sentry
Please make sure that you call `sentry_sdk.init` *before* initializing
your broker, as it monkey patches `Broker.__init__`.
This integration was originally developed and maintained
by https://github.com/jacobsvante and later donated to the Sentry
project.
"""
identifier = "dramatiq"
origin = f"auto.queue.{identifier}"
@staticmethod
def setup_once() -> None:
_patch_dramatiq_broker()
def _patch_dramatiq_broker() -> None:
original_broker__init__ = Broker.__init__
def sentry_patched_broker__init__(
self: "Broker", *args: "Any", **kw: "Any"
) -> None:
integration = sentry_sdk.get_client().get_integration(DramatiqIntegration)
try:
middleware = kw.pop("middleware")
except KeyError:
# Unfortunately Broker and StubBroker allows middleware to be
# passed in as positional arguments, whilst RabbitmqBroker and
# RedisBroker does not.
if len(args) == 1:
middleware = args[0]
args = ()
else:
middleware = None
if middleware is None:
middleware = list(m() for m in default_middleware)
else:
middleware = list(middleware)
if integration is not None:
middleware = [m for m in middleware if not isinstance(m, SentryMiddleware)]
middleware.insert(0, SentryMiddleware())
kw["middleware"] = middleware
original_broker__init__(self, *args, **kw)
Broker.__init__ = sentry_patched_broker__init__
class SentryMiddleware(Middleware): # type: ignore[misc]
"""
A Dramatiq middleware that automatically captures and sends
exceptions to Sentry.
This is automatically added to every instantiated broker via the
DramatiqIntegration.
"""
SENTRY_HEADERS_NAME = "_sentry_headers"
def before_enqueue(
self, broker: "Broker", message: "Message[R]", delay: int
) -> None:
integration = sentry_sdk.get_client().get_integration(DramatiqIntegration)
if integration is None:
return
message.options[self.SENTRY_HEADERS_NAME] = {
BAGGAGE_HEADER_NAME: get_baggage(),
SENTRY_TRACE_HEADER_NAME: get_traceparent(),
}
def before_process_message(self, broker: "Broker", message: "Message[R]") -> None:
client = sentry_sdk.get_client()
integration = client.get_integration(DramatiqIntegration)
if integration is None:
return
message._scope_manager = sentry_sdk.isolation_scope()
scope = message._scope_manager.__enter__()
scope.clear_breadcrumbs()
scope.set_extra("dramatiq_message_id", message.message_id)
scope.add_event_processor(_make_message_event_processor(message, integration))
sentry_headers = message.options.get(self.SENTRY_HEADERS_NAME) or {}
if "retries" in message.options:
# start new trace in case of retrying
sentry_headers = {}
if has_span_streaming_enabled(client.options):
sentry_sdk.traces.continue_trace(sentry_headers)
span = sentry_sdk.traces.start_span(
name=message.actor_name,
attributes={
"sentry.op": OP.QUEUE_TASK_DRAMATIQ,
"sentry.origin": DramatiqIntegration.origin,
"sentry.span.source": SegmentSource.TASK.value,
},
parent_span=None,
)
message._sentry_span_ctx = span
else:
transaction = continue_trace(
sentry_headers,
name=message.actor_name,
op=OP.QUEUE_TASK_DRAMATIQ,
source=TransactionSource.TASK,
origin=DramatiqIntegration.origin,
)
transaction.set_status(SPANSTATUS.OK)
sentry_sdk.start_transaction(
transaction,
name=message.actor_name,
op=OP.QUEUE_TASK_DRAMATIQ,
source=TransactionSource.TASK,
)
transaction.__enter__()
message._sentry_span_ctx = transaction
def after_process_message(
self,
broker: "Broker",
message: "Message[R]",
*,
result: "Optional[Any]" = None,
exception: "Optional[Exception]" = None,
) -> None:
integration = sentry_sdk.get_client().get_integration(DramatiqIntegration)
if integration is None:
return
actor = broker.get_actor(message.actor_name)
throws = message.options.get("throws") or actor.options.get("throws")
scope_manager = message._scope_manager
span_ctx = getattr(message, "_sentry_span_ctx", None)
if span_ctx is None:
return None
is_event_capture_required = (
exception is not None
and not (throws and isinstance(exception, throws))
and not isinstance(exception, Retry)
)
if not is_event_capture_required:
# normal transaction finish
span_ctx.__exit__(None, None, None)
scope_manager.__exit__(None, None, None)
return
event, hint = event_from_exception(
exception, # type: ignore[arg-type]
client_options=sentry_sdk.get_client().options,
mechanism={
"type": DramatiqIntegration.identifier,
"handled": False,
},
)
sentry_sdk.capture_event(event, hint=hint)
# transaction error
span_ctx.__exit__(type(exception), exception, None)
scope_manager.__exit__(type(exception), exception, None)
after_skip_message = after_process_message
def _make_message_event_processor(
message: "Message[R]", integration: "DramatiqIntegration"
) -> "Callable[[Event, Hint], Optional[Event]]":
def inner(event: "Event", hint: "Hint") -> "Optional[Event]":
with capture_internal_exceptions():
DramatiqMessageExtractor(message).extract_into_event(event)
return event
return inner
class DramatiqMessageExtractor:
def __init__(self, message: "Message[R]") -> None:
self.message_data = dict(message.asdict())
def content_length(self) -> int:
return len(json.dumps(self.message_data))
def extract_into_event(self, event: "Event") -> None:
client = sentry_sdk.get_client()
if not client.is_active():
return
contexts = event.setdefault("contexts", {})
request_info = contexts.setdefault("dramatiq", {})
request_info["type"] = "dramatiq"
data: "Optional[Union[AnnotatedValue, Dict[str, Any]]]" = None
if not request_body_within_bounds(client, self.content_length()):
data = AnnotatedValue.removed_because_over_size_limit()
else:
data = self.message_data
request_info["data"] = data