|
25 | 25 | ) |
26 | 26 | from opentelemetry.instrumentation.celery import CeleryInstrumentor, utils |
27 | 27 | from opentelemetry.instrumentation.utils import unwrap |
28 | | -from opentelemetry.semconv._incubating.attributes import messaging_attributes |
| 28 | +from opentelemetry.semconv._incubating.attributes import ( |
| 29 | + exception_attributes, |
| 30 | + messaging_attributes, |
| 31 | +) |
29 | 32 | from opentelemetry.semconv.trace import SpanAttributes |
30 | 33 | from opentelemetry.test.test_base import TestBase |
31 | 34 | from opentelemetry.trace import SpanKind, StatusCode |
|
36 | 39 | class TestCeleryInstrumentation(TestBase): |
37 | 40 | def setUp(self): |
38 | 41 | super().setUp() |
39 | | - _OpenTelemetrySemanticConventionStability._initialized = False |
| 42 | + _OpenTelemetrySemanticConventionStability._initialized = False # to have consistent behavior accross tests since this attribute ensures that initialization happens only once |
40 | 43 | self._worker = app.Worker(app=app, pool="solo", concurrency=1) |
41 | 44 | self._thread = threading.Thread(target=self._worker.start) |
42 | 45 | self._thread.daemon = True |
@@ -286,6 +289,80 @@ def test_task_new_sem_conv(self): |
286 | 289 | consumer.context.trace_id, producer.context.trace_id |
287 | 290 | ) |
288 | 291 |
|
| 292 | + def test_task_raises_new_sem_conv(self): |
| 293 | + with mock.patch.dict( |
| 294 | + "os.environ", {OTEL_SEMCONV_STABILITY_OPT_IN: "messaging"} |
| 295 | + ): |
| 296 | + CeleryInstrumentor().instrument() |
| 297 | + |
| 298 | + result = task_raises.delay() |
| 299 | + |
| 300 | + timeout = time.time() + 60 * 1 # 1 minutes from now |
| 301 | + while not result.ready(): |
| 302 | + if time.time() > timeout: |
| 303 | + break |
| 304 | + time.sleep(0.05) |
| 305 | + |
| 306 | + spans = self.sorted_spans( |
| 307 | + self.memory_exporter.get_finished_spans() |
| 308 | + ) |
| 309 | + self.assertEqual(len(spans), 2) |
| 310 | + |
| 311 | + consumer, producer = spans |
| 312 | + |
| 313 | + self.assertEqual( |
| 314 | + consumer.name, "run/tests.celery_test_tasks.task_raises" |
| 315 | + ) |
| 316 | + self.assertEqual(consumer.kind, SpanKind.CONSUMER) |
| 317 | + self.assertSpanHasAttributes( |
| 318 | + consumer, |
| 319 | + { |
| 320 | + "celery.action": "run", |
| 321 | + "celery.state": "FAILURE", |
| 322 | + messaging_attributes.MESSAGING_DESTINATION_NAME: "celery", |
| 323 | + "celery.task_name": "tests.celery_test_tasks.task_raises", |
| 324 | + }, |
| 325 | + ) |
| 326 | + |
| 327 | + self.assertEqual(consumer.status.status_code, StatusCode.ERROR) |
| 328 | + |
| 329 | + self.assertEqual(1, len(consumer.events)) |
| 330 | + event = consumer.events[0] |
| 331 | + |
| 332 | + self.assertIn( |
| 333 | + exception_attributes.EXCEPTION_STACKTRACE, event.attributes |
| 334 | + ) |
| 335 | + |
| 336 | + self.assertEqual( |
| 337 | + "tests.celery_test_tasks.CustomError", |
| 338 | + event.attributes[exception_attributes.EXCEPTION_TYPE], |
| 339 | + ) |
| 340 | + |
| 341 | + self.assertEqual( |
| 342 | + event.attributes[exception_attributes.EXCEPTION_MESSAGE], |
| 343 | + "The task failed!", |
| 344 | + ) |
| 345 | + |
| 346 | + self.assertEqual( |
| 347 | + producer.name, |
| 348 | + "apply_async/tests.celery_test_tasks.task_raises", |
| 349 | + ) |
| 350 | + self.assertEqual(producer.kind, SpanKind.PRODUCER) |
| 351 | + self.assertSpanHasAttributes( |
| 352 | + producer, |
| 353 | + { |
| 354 | + "celery.action": "apply_async", |
| 355 | + "celery.task_name": "tests.celery_test_tasks.task_raises", |
| 356 | + messaging_attributes.MESSAGING_DESTINATION_NAME: "celery", |
| 357 | + }, |
| 358 | + ) |
| 359 | + |
| 360 | + self.assertNotEqual(consumer.parent, producer.context) |
| 361 | + self.assertEqual(consumer.parent.span_id, producer.context.span_id) |
| 362 | + self.assertEqual( |
| 363 | + consumer.context.trace_id, producer.context.trace_id |
| 364 | + ) |
| 365 | + |
289 | 366 | def test_task_both_sem_conv(self): |
290 | 367 | with mock.patch.dict( |
291 | 368 | "os.environ", {OTEL_SEMCONV_STABILITY_OPT_IN: "messaging/dup"} |
|
0 commit comments