Skip to content

Commit 5f442ea

Browse files
committed
Fix the propagation test
1 parent ed39550 commit 5f442ea

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

tests/integrations/celery/test_celery.py

Lines changed: 26 additions & 7 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
@@ -221,11 +222,13 @@ def dummy_task(x, y):
221222
assert execution_event["contexts"]["trace"]["status"] == "ok"
222223

223224
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-
})
225+
assert execution_event["spans"][0] == ApproxDict(
226+
{
227+
"trace_id": str(root_span.trace_id),
228+
"op": "queue.process",
229+
"description": "dummy_task",
230+
}
231+
)
229232
assert submission_event["spans"] == [
230233
{
231234
"data": ApproxDict(),
@@ -240,7 +243,7 @@ def dummy_task(x, y):
240243
"status": "ok",
241244
"tags": {
242245
"status": "ok",
243-
}
246+
},
244247
}
245248
]
246249

@@ -537,6 +540,20 @@ def test_sentry_propagate_traces_override(init_celery):
537540
propagate_traces=True, traces_sample_rate=1.0, release="abcdef"
538541
)
539542

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

578+
Task.apply = original_apply
579+
561580

562581
def test_apply_async_manually_span(sentry_init):
563582
sentry_init(

0 commit comments

Comments
 (0)