Skip to content

Commit 4746db6

Browse files
author
igorek
committed
feat(integrations): Remove ContextVar usage.
- No need to store transaction separately. All contextvar mentions removed - dramatiq_mesage_id tag removed Issue: #3454
1 parent a41b477 commit 4746db6

File tree

4 files changed

+3
-42
lines changed

4 files changed

+3
-42
lines changed

scripts/populate_tox/config.py

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

sentry_sdk/integrations/dramatiq.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
AnnotatedValue,
1515
capture_internal_exceptions,
1616
event_from_exception,
17-
ContextVar,
18-
HAS_REAL_CONTEXTVARS,
19-
CONTEXTVARS_ERROR_MESSAGE,
2017
)
2118
from typing import TypeVar
2219

@@ -50,15 +47,11 @@ class DramatiqIntegration(Integration):
5047
"""
5148

5249
identifier = "dramatiq"
50+
origin = f"auto.queue.{identifier}"
5351

5452
@staticmethod
5553
def setup_once():
5654
# 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-
)
6255

6356
_patch_dramatiq_broker()
6457

@@ -107,8 +100,6 @@ class SentryMiddleware(Middleware): # type: ignore[misc]
107100
DramatiqIntegration.
108101
"""
109102

110-
_transaction = ContextVar("_transaction")
111-
112103
def before_enqueue(self, broker, message, delay):
113104
# type: (Broker, Message[R], int) -> None
114105
message.options["sentry_headers"] = {
@@ -123,7 +114,6 @@ def before_process_message(self, broker, message):
123114
return
124115

125116
scope = sentry_sdk.get_current_scope()
126-
scope.set_tag("dramatiq_message_id", message.message_id)
127117
scope.clear_breadcrumbs()
128118
scope.add_event_processor(_make_message_event_processor(message, integration))
129119

@@ -137,7 +127,7 @@ def before_process_message(self, broker, message):
137127
name=message.actor_name,
138128
op=OP.QUEUE_TASK_DRAMATIQ,
139129
source=TransactionSource.TASK,
140-
# origin=DramatiqIntegration.origin,
130+
origin=DramatiqIntegration.origin,
141131
)
142132
transaction.set_status(SPANSTATUS.OK)
143133
sentry_sdk.start_transaction(
@@ -147,15 +137,14 @@ def before_process_message(self, broker, message):
147137
source=TransactionSource.TASK,
148138
)
149139
transaction.__enter__()
150-
self._transaction.set(transaction)
151140

152141
def after_process_message(self, broker, message, *, result=None, exception=None):
153142
# type: (Broker, Message[R], Optional[Any], Optional[Exception]) -> None
154143

155144
actor = broker.get_actor(message.actor_name)
156145
throws = message.options.get("throws") or actor.options.get("throws")
157146

158-
transaction = self._transaction.get(None)
147+
transaction = sentry_sdk.get_current_scope().transaction
159148
if not transaction:
160149
return None
161150

tests/integrations/dramatiq/test_dramatiq.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -106,30 +106,6 @@ def propagated_trace_task():
106106
assert events[0]["contexts"]["trace"]["trace_id"] == outer_transaction.trace_id
107107

108108

109-
def test_that_dramatiq_message_id_is_set_as_tag(broker, worker, capture_events):
110-
events = capture_events()
111-
112-
@dramatiq.actor(max_retries=0)
113-
def dummy_actor(x, y):
114-
sentry_sdk.capture_message("hi")
115-
return x / y
116-
117-
dummy_actor.send(1, 0)
118-
broker.join(dummy_actor.queue_name)
119-
worker.join()
120-
121-
event_message, event_error = events
122-
123-
assert "dramatiq_message_id" in event_message["tags"]
124-
assert "dramatiq_message_id" in event_error["tags"]
125-
assert (
126-
event_message["tags"]["dramatiq_message_id"]
127-
== event_error["tags"]["dramatiq_message_id"]
128-
)
129-
msg_ids = [e["tags"]["dramatiq_message_id"] for e in events]
130-
assert all(uuid.UUID(msg_id) and isinstance(msg_id, str) for msg_id in msg_ids)
131-
132-
133109
def test_that_local_variables_are_captured(broker, worker, capture_events):
134110
events = capture_events()
135111

tox.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,6 @@ deps =
589589
dramatiq-v1.12.3: dramatiq==1.12.3
590590
dramatiq-v1.15.0: dramatiq==1.15.0
591591
dramatiq-v1.18.0: dramatiq==1.18.0
592-
{py3.6}-dramatiq: aiocontextvars
593592

594593
huey-v2.1.3: huey==2.1.3
595594
huey-v2.2.0: huey==2.2.0

0 commit comments

Comments
 (0)