Skip to content

Commit bda4ca1

Browse files
committed
Add test
Without the fix we have this in stdout: [2024-10-28 12:14:18,868: ERROR/MainProcess] Failed to detach context Traceback (most recent call last): File "/home/rm/src/opentelemetry-python-contrib/.tox/py310-test-instrumentation-celery-1/lib/python3.10/site-packages/opentelemetry/context/__init__.py", line 152, in detach _RUNTIME_CONTEXT.detach(token) File "/home/rm/src/opentelemetry-python-contrib/.tox/py310-test-instrumentation-celery-1/lib/python3.10/site-packages/opentelemetry/context/contextvars_context.py", line 50, in detach self._current_context.reset(token) # type: ignore TypeError: expected an instance of Token, got None
1 parent 0058822 commit bda4ca1

File tree

1 file changed

+38
-1
lines changed
  • instrumentation/opentelemetry-instrumentation-celery/tests

1 file changed

+38
-1
lines changed

instrumentation/opentelemetry-instrumentation-celery/tests/test_tasks.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
import threading
1616
import time
1717

18+
from wrapt import wrap_function_wrapper
19+
1820
from opentelemetry import baggage, context
19-
from opentelemetry.instrumentation.celery import CeleryInstrumentor
21+
from opentelemetry.instrumentation.celery import CeleryInstrumentor, utils
22+
from opentelemetry.instrumentation.utils import unwrap
2023
from opentelemetry.semconv.trace import SpanAttributes
2124
from opentelemetry.test.test_base import TestBase
2225
from opentelemetry.trace import SpanKind, StatusCode
@@ -185,6 +188,40 @@ def test_baggage(self):
185188

186189
self.assertEqual(task.result, {"key": "value"})
187190

191+
def _retrieve_context_wrapper_none_token(
192+
self, wrapped, instance, args, kwargs
193+
):
194+
ctx = wrapped(*args, **kwargs)
195+
if ctx is None:
196+
return ctx
197+
span, activation, _ = ctx
198+
return span, activation, None
199+
200+
def test_task_not_instrumented_does_not_raise(self):
201+
wrap_function_wrapper(
202+
utils,
203+
"retrieve_context",
204+
self._retrieve_context_wrapper_none_token,
205+
)
206+
207+
CeleryInstrumentor().instrument()
208+
209+
result = task_add.delay(1, 2)
210+
211+
timeout = time.time() + 60 * 1 # 1 minutes from now
212+
while not result.ready():
213+
if time.time() > timeout:
214+
break
215+
time.sleep(0.05)
216+
217+
spans = self.sorted_spans(self.memory_exporter.get_finished_spans())
218+
self.assertEqual(len(spans), 2)
219+
220+
# TODO: assert we don't have "TypeError: expected an instance of Token, got None" in logs
221+
self.assertTrue(result)
222+
223+
unwrap(utils, "retrieve_context")
224+
188225

189226
class TestCelerySignatureTask(TestBase):
190227
def setUp(self):

0 commit comments

Comments
 (0)