Skip to content

Commit 74b4405

Browse files
authored
fix(celery): Un-break usages of apply_async (getsentry#357)
1 parent 773e439 commit 74b4405

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

sentry_sdk/integrations/celery.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def sentry_build_tracer(name, task, *args, **kwargs):
5151

5252

5353
def _wrap_apply_async(task, f):
54-
def apply_async(self, *args, **kwargs):
54+
def apply_async(*args, **kwargs):
5555
hub = Hub.current
5656
integration = hub.get_integration(CeleryIntegration)
5757
if integration is not None and integration.propagate_traces:
@@ -62,7 +62,7 @@ def apply_async(self, *args, **kwargs):
6262
headers[key] = value
6363
if headers is not None:
6464
kwargs["headers"] = headers
65-
return f(self, *args, **kwargs)
65+
return f(*args, **kwargs)
6666

6767
return apply_async
6868

tests/integrations/celery/test_celery.py

+22-8
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,22 @@ def celery(init_celery):
4040
return init_celery()
4141

4242

43-
def test_simple(capture_events, celery):
43+
@pytest.mark.parametrize(
44+
"invocation,expected_context",
45+
[
46+
[lambda task, x, y: task.delay(x, y), {"args": [1, 0], "kwargs": {}}],
47+
[lambda task, x, y: task.apply_async((x, y)), {"args": [1, 0], "kwargs": {}}],
48+
[
49+
lambda task, x, y: task.apply_async(args=(x, y)),
50+
{"args": [1, 0], "kwargs": {}},
51+
],
52+
[
53+
lambda task, x, y: task.apply_async(kwargs=dict(x=x, y=y)),
54+
{"args": [], "kwargs": {"x": 1, "y": 0}},
55+
],
56+
],
57+
)
58+
def test_simple(capture_events, celery, invocation, expected_context):
4459
events = capture_events()
4560

4661
@celery.task(name="dummy_task")
@@ -51,18 +66,17 @@ def dummy_task(x, y):
5166
span_context = SpanContext.start_trace()
5267
with configure_scope() as scope:
5368
scope.set_span_context(span_context)
54-
dummy_task.delay(1, 2)
55-
dummy_task.delay(1, 0)
69+
70+
invocation(dummy_task, 1, 2)
71+
invocation(dummy_task, 1, 0)
5672

5773
event, = events
5874
assert event["contexts"]["trace"]["trace_id"] == span_context.trace_id
5975
assert event["contexts"]["trace"]["span_id"] != span_context.span_id
6076
assert event["transaction"] == "dummy_task"
61-
assert event["extra"]["celery-job"] == {
62-
"args": [1, 0],
63-
"kwargs": {},
64-
"task_name": "dummy_task",
65-
}
77+
assert event["extra"]["celery-job"] == dict(
78+
task_name="dummy_task", **expected_context
79+
)
6680

6781
exception, = event["exception"]["values"]
6882
assert exception["type"] == "ZeroDivisionError"

0 commit comments

Comments
 (0)