Skip to content

Commit 016609c

Browse files
committed
Rename function _wrap_apply_async to _wrap_task_run
1 parent c6c36ff commit 016609c

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

sentry_sdk/integrations/celery.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ def sentry_build_tracer(name, task, *args, **kwargs):
110110
from celery.app.task import Task # type: ignore
111111
from celery import Celery # type: ignore
112112

113-
Task.apply_async = _wrap_apply_async(Task.apply_async)
114-
Celery.send_task = _wrap_apply_async(Celery.send_task)
113+
Task.apply_async = _wrap_task_run(Task.apply_async)
114+
Celery.send_task = _wrap_task_run(Celery.send_task)
115115

116116
_patch_worker_exit()
117117

@@ -146,7 +146,7 @@ def __exit__(self, exc_type, exc_value, traceback):
146146
return None
147147

148148

149-
def _wrap_apply_async(f):
149+
def _wrap_task_run(f):
150150
# type: (F) -> F
151151
@wraps(f)
152152
def apply_async(*args, **kwargs):
@@ -172,10 +172,13 @@ def apply_async(*args, **kwargs):
172172
except (IndexError, TypeError):
173173
task_started_from_beat = False
174174

175-
task = args[0]
175+
if isinstance(args[0], Task):
176+
task_name = args[0].name
177+
else:
178+
task_name = args[1]
176179

177180
span_mgr = (
178-
hub.start_span(op=OP.QUEUE_SUBMIT_CELERY, description=task.name)
181+
hub.start_span(op=OP.QUEUE_SUBMIT_CELERY, description=task_name)
179182
if not task_started_from_beat
180183
else NoOpMgr()
181184
) # type: Union[Span, NoOpMgr]

tests/integrations/celery/test_celery.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from sentry_sdk.integrations.celery import (
77
CeleryIntegration,
88
_get_headers,
9-
_wrap_apply_async,
9+
_wrap_task_run,
1010
)
1111

1212
from sentry_sdk._compat import text_type
@@ -34,7 +34,7 @@ def init_celery(sentry_init, request):
3434
def inner(propagate_traces=True, backend="always_eager", **kwargs):
3535
sentry_init(
3636
integrations=[CeleryIntegration(propagate_traces=propagate_traces)],
37-
**kwargs
37+
**kwargs,
3838
)
3939
celery = Celery(__name__)
4040

@@ -372,16 +372,16 @@ def test_redis_backend_trace_propagation(
372372
runs = []
373373

374374
@celery.task(name="dummy_task", bind=True)
375-
def dummy_task(self):
375+
def dummy_task(self, x, y):
376376
runs.append(1)
377377
1 / 0
378378

379379
with start_transaction(name="submit_celery"):
380380
# Curious: Cannot use delay() here or py2.7-celery-4.2 crashes
381381
if execution_way == "apply_async":
382-
res = dummy_task.apply_async()
382+
res = dummy_task.apply_async(kwargs={"x": 1, "y": 0})
383383
elif execution_way == "send_task":
384-
res = celery.send_task("dummy_task")
384+
res = celery.send_task("dummy_task", kwargs={"x": 1, "y": 0})
385385
else: # pragma: no cover
386386
raise ValueError(execution_way)
387387

@@ -579,7 +579,7 @@ def dummy_function(*args, **kwargs):
579579
assert "sentry-trace" in headers
580580
assert "baggage" in headers
581581

582-
wrapped = _wrap_apply_async(dummy_function)
582+
wrapped = _wrap_task_run(dummy_function)
583583
wrapped(mock.MagicMock(), (), headers={})
584584

585585

@@ -593,7 +593,7 @@ def dummy_function(*args, **kwargs):
593593
assert "sentry-trace" not in headers
594594
assert "baggage" not in headers
595595

596-
wrapped = _wrap_apply_async(dummy_function)
596+
wrapped = _wrap_task_run(dummy_function)
597597
wrapped(
598598
mock.MagicMock(),
599599
[

0 commit comments

Comments
 (0)