Skip to content

Commit 7448b68

Browse files
committed
Fix the propagation test
1 parent ed39550 commit 7448b68

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

tests/integrations/celery/test_celery.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import threading
22
import kombu
33
from unittest import mock
4-
from urllib.parse import quote
54

65
import pytest
76
from celery import Celery, VERSION
87
from celery.bin import worker
8+
from celery.app.task import Task
9+
from opentelemetry import trace as otel_trace, context
910

1011
import sentry_sdk
1112
from sentry_sdk import get_current_span
@@ -191,9 +192,6 @@ def test_transaction_events(capture_events, init_celery, celery_invocation, task
191192
def dummy_task(x, y):
192193
return x / y
193194

194-
# XXX: For some reason the first call does not get instrumented properly.
195-
# celery_invocation(dummy_task, 1, 1)
196-
197195
events = capture_events()
198196

199197
with sentry_sdk.start_span(name="submission") as root_span:
@@ -221,11 +219,13 @@ def dummy_task(x, y):
221219
assert execution_event["contexts"]["trace"]["status"] == "ok"
222220

223221
assert len(execution_event["spans"]) == 1
224-
assert execution_event["spans"][0] == ApproxDict({
225-
"trace_id": str(root_span.trace_id),
226-
"op": "queue.process",
227-
"description": "dummy_task",
228-
})
222+
assert execution_event["spans"][0] == ApproxDict(
223+
{
224+
"trace_id": str(root_span.trace_id),
225+
"op": "queue.process",
226+
"description": "dummy_task",
227+
}
228+
)
229229
assert submission_event["spans"] == [
230230
{
231231
"data": ApproxDict(),
@@ -240,7 +240,7 @@ def dummy_task(x, y):
240240
"status": "ok",
241241
"tags": {
242242
"status": "ok",
243-
}
243+
},
244244
}
245245
]
246246

@@ -537,6 +537,20 @@ def test_sentry_propagate_traces_override(init_celery):
537537
propagate_traces=True, traces_sample_rate=1.0, release="abcdef"
538538
)
539539

540+
# Since we're applying the task inline eagerly,
541+
# we need to cleanup the otel context for this test.
542+
# and since we patch build_tracer, we need to do this before that runs...
543+
# TODO: the right way is to not test this inline
544+
original_apply = Task.apply
545+
546+
def cleaned_apply(*args, **kwargs):
547+
token = context.attach(otel_trace.set_span_in_context(otel_trace.INVALID_SPAN))
548+
rv = original_apply(*args, **kwargs)
549+
context.detach(token)
550+
return rv
551+
552+
Task.apply = cleaned_apply
553+
540554
@celery.task(name="dummy_task", bind=True)
541555
def dummy_task(self, message):
542556
trace_id = get_current_span().trace_id
@@ -558,6 +572,8 @@ def dummy_task(self, message):
558572
).get()
559573
assert root_span_trace_id != task_trace_id, "Trace should NOT be propagated"
560574

575+
Task.apply = original_apply
576+
561577

562578
def test_apply_async_manually_span(sentry_init):
563579
sentry_init(

0 commit comments

Comments
 (0)