Skip to content

Commit 7156ac4

Browse files
author
igorek
committed
feat(integration): Fix dramatiq py3.6 and linters issues (#3454)
- import ContextVar from utils for python3.6 support - do not import type "R" from dramatiq package because it was introduced only in newer version of dramatiq - fix mypy settings to ignore missing imports - fix tox.ini to include aiocontextvars for py3.6
1 parent 9764f19 commit 7156ac4

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ ignore_missing_imports = true
195195
module = "agents.*"
196196
ignore_missing_imports = true
197197

198+
[[tool.mypy.overrides]]
199+
module = "dramatiq.*"
200+
ignore_missing_imports = true
201+
198202
#
199203
# Tool: Flake8
200204
#

scripts/populate_tox/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@
100100
},
101101
"dramatiq": {
102102
"package": "dramatiq",
103+
"deps": {
104+
"py3.6": ["aiocontextvars"],
105+
},
103106
},
104107
"falcon": {
105108
"package": "falcon",

sentry_sdk/integrations/dramatiq.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import contextvars
32

43
import sentry_sdk
54
from sentry_sdk.consts import OP, SPANSTATUS
@@ -15,21 +14,26 @@
1514
AnnotatedValue,
1615
capture_internal_exceptions,
1716
event_from_exception,
17+
ContextVar,
18+
HAS_REAL_CONTEXTVARS,
19+
CONTEXTVARS_ERROR_MESSAGE,
1820
)
21+
from typing import TypeVar
22+
23+
R = TypeVar("R")
1924

2025
try:
2126
from dramatiq.broker import Broker
22-
from dramatiq.message import Message, R
2327
from dramatiq.middleware import Middleware, default_middleware
2428
from dramatiq.errors import Retry
29+
from dramatiq.message import Message
2530
except ImportError:
2631
raise DidNotEnable("Dramatiq is not installed")
2732

2833
from typing import TYPE_CHECKING
2934

3035
if TYPE_CHECKING:
3136
from typing import Any, Callable, Dict, Optional, Union
32-
from sentry_sdk.tracing import Transaction
3337
from sentry_sdk._types import Event, Hint
3438

3539

@@ -50,6 +54,12 @@ class DramatiqIntegration(Integration):
5054
@staticmethod
5155
def setup_once():
5256
# type: () -> None
57+
if not HAS_REAL_CONTEXTVARS:
58+
raise DidNotEnable(
59+
"The dramatiq integration for Sentry requires Python 3.7+ "
60+
" or aiocontextvars package." + CONTEXTVARS_ERROR_MESSAGE
61+
)
62+
5363
_patch_dramatiq_broker()
5464

5565

@@ -85,10 +95,10 @@ def sentry_patched_broker__init__(self, *args, **kw):
8595
kw["middleware"] = middleware
8696
original_broker__init__(self, *args, **kw)
8797

88-
Broker.__init__ = sentry_patched_broker__init__ # type: ignore[method-assign]
98+
Broker.__init__ = sentry_patched_broker__init__
8999

90100

91-
class SentryMiddleware(Middleware):
101+
class SentryMiddleware(Middleware): # type: ignore[misc]
92102
"""
93103
A Dramatiq middleware that automatically captures and sends
94104
exceptions to Sentry.
@@ -97,9 +107,7 @@ class SentryMiddleware(Middleware):
97107
DramatiqIntegration.
98108
"""
99109

100-
_transaction = contextvars.ContextVar(
101-
"_transaction"
102-
) # type: contextvars.ContextVar[Transaction]
110+
_transaction = ContextVar("_transaction")
103111

104112
def before_enqueue(self, broker, message, delay):
105113
# type: (Broker, Message[R], int) -> None
@@ -130,7 +138,7 @@ def before_process_message(self, broker, message):
130138
sentry_sdk.start_transaction(
131139
transaction,
132140
name=message.actor_name,
133-
op=OP.QUEUE_PROCESS,
141+
op=OP.QUEUE_TASK_DRAMATIQ,
134142
source=TransactionSource.TASK,
135143
)
136144
transaction.__enter__()
@@ -142,7 +150,9 @@ def after_process_message(self, broker, message, *, result=None, exception=None)
142150
actor = broker.get_actor(message.actor_name)
143151
throws = message.options.get("throws") or actor.options.get("throws")
144152

145-
transaction = self._transaction.get()
153+
transaction = self._transaction.get(None)
154+
if not transaction:
155+
return None
146156

147157
is_event_capture_required = (
148158
exception is not None

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ deps =
647647
dramatiq-v1.12.3: dramatiq==1.12.3
648648
dramatiq-v1.15.0: dramatiq==1.15.0
649649
dramatiq-v1.18.0: dramatiq==1.18.0
650+
{py3.6}-dramatiq: aiocontextvars
650651

651652
huey-v2.1.3: huey==2.1.3
652653
huey-v2.2.0: huey==2.2.0

0 commit comments

Comments
 (0)