Skip to content

Commit d7a3444

Browse files
author
mriamah
committed
Add test for when OTEL_SEMCONV_STABILITY_OPT_IN="messaging/dup" to have both legacy and new semantic conventions
1 parent a5a51f7 commit d7a3444

File tree

1 file changed

+74
-2
lines changed
  • instrumentation/opentelemetry-instrumentation-celery/tests

1 file changed

+74
-2
lines changed

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

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,12 @@ def _retrieve_context_wrapper_none_token(
228228
unwrap(utils, "retrieve_context")
229229

230230
def test_task_new_sem_conv(self):
231-
CeleryInstrumentor().uninstrument()
232231
with mock.patch.dict(
233232
"os.environ", {OTEL_SEMCONV_STABILITY_OPT_IN: "messaging"}
234233
):
235234
CeleryInstrumentor().instrument()
236235

237236
result = task_add.delay(1, 2)
238-
239237
timeout = time.time() + 60 * 1 # 1 minutes from now
240238
while not result.ready():
241239
if time.time() > timeout:
@@ -286,6 +284,80 @@ def test_task_new_sem_conv(self):
286284
consumer.context.trace_id, producer.context.trace_id
287285
)
288286

287+
def test_task_both_sem_conv(self):
288+
with mock.patch.dict(
289+
"os.environ", {OTEL_SEMCONV_STABILITY_OPT_IN: "messaging/dup"}
290+
):
291+
CeleryInstrumentor().instrument()
292+
293+
result = task_add.delay(1, 2)
294+
timeout = time.time() + 60 * 1 # 1 minutes from now
295+
while not result.ready():
296+
if time.time() > timeout:
297+
break
298+
time.sleep(0.05)
299+
300+
spans = self.sorted_spans(
301+
self.memory_exporter.get_finished_spans()
302+
)
303+
self.assertEqual(len(spans), 2)
304+
305+
consumer, producer = spans
306+
307+
self.assertEqual(
308+
consumer.name, "run/tests.celery_test_tasks.task_add"
309+
)
310+
self.assertEqual(consumer.kind, SpanKind.CONSUMER)
311+
self.assertSpanHasAttributes(
312+
consumer,
313+
{
314+
"celery.action": "run",
315+
"celery.state": "SUCCESS",
316+
messaging_attributes.MESSAGING_DESTINATION_NAME: "celery",
317+
"celery.task_name": "tests.celery_test_tasks.task_add",
318+
},
319+
)
320+
self.assertSpanHasAttributes(
321+
consumer,
322+
{
323+
"celery.action": "run",
324+
"celery.state": "SUCCESS",
325+
SpanAttributes.MESSAGING_DESTINATION: "celery",
326+
"celery.task_name": "tests.celery_test_tasks.task_add",
327+
},
328+
)
329+
330+
self.assertEqual(consumer.status.status_code, StatusCode.UNSET)
331+
332+
self.assertEqual(0, len(consumer.events))
333+
334+
self.assertEqual(
335+
producer.name, "apply_async/tests.celery_test_tasks.task_add"
336+
)
337+
self.assertEqual(producer.kind, SpanKind.PRODUCER)
338+
self.assertSpanHasAttributes(
339+
producer,
340+
{
341+
"celery.action": "apply_async",
342+
"celery.task_name": "tests.celery_test_tasks.task_add",
343+
messaging_attributes.MESSAGING_DESTINATION_NAME: "celery",
344+
},
345+
)
346+
self.assertSpanHasAttributes(
347+
consumer,
348+
{
349+
"celery.action": "run",
350+
"celery.state": "SUCCESS",
351+
SpanAttributes.MESSAGING_DESTINATION: "celery",
352+
"celery.task_name": "tests.celery_test_tasks.task_add",
353+
},
354+
)
355+
self.assertNotEqual(consumer.parent, producer.context)
356+
self.assertEqual(consumer.parent.span_id, producer.context.span_id)
357+
self.assertEqual(
358+
consumer.context.trace_id, producer.context.trace_id
359+
)
360+
289361

290362
class TestCelerySignatureTask(TestBase):
291363
def setUp(self):

0 commit comments

Comments
 (0)