diff --git a/.changelog/4827.fixed b/.changelog/4827.fixed new file mode 100644 index 0000000000..9c6a50b6e2 --- /dev/null +++ b/.changelog/4827.fixed @@ -0,0 +1 @@ +`opentelemetry-instrumentation-celery`: populate `celery.origin` and `celery.hostname` as distinct span attributes diff --git a/instrumentation/opentelemetry-instrumentation-celery/src/opentelemetry/instrumentation/celery/utils.py b/instrumentation/opentelemetry-instrumentation-celery/src/opentelemetry/instrumentation/celery/utils.py index be9d073f6a..90fccbb66b 100644 --- a/instrumentation/opentelemetry-instrumentation-celery/src/opentelemetry/instrumentation/celery/utils.py +++ b/instrumentation/opentelemetry-instrumentation-celery/src/opentelemetry/instrumentation/celery/utils.py @@ -86,12 +86,7 @@ def set_attributes_from_context( attribute_name = None - # Celery 4.0 uses `origin` instead of `hostname`; this change preserves - # the same name for the tag despite Celery version - if key == "origin": - key = "hostname" - - elif key == "delivery_info": + if key == "delivery_info": # Get also destination from this routing_key = value.get("routing_key") @@ -169,7 +164,7 @@ def attach_context( def detach_context( task: Optional[Task], task_id: str, is_publish: bool = False ) -> None: - """Helper to remove `Span`, `ContextManager` and context token in a + """Helper to remove `Span`, `ContextManager` and context token in a Celery task when it's propagated. This function handles tasks where no values are attached to the `Task`. """ @@ -217,8 +212,8 @@ def retrieve_task_from_sender(kwargs: Mapping[str, Any]) -> Optional[Task]: # for retry and failure signals sender is the task object if isinstance(sender, str): sender = registry.tasks.get(sender) - if sender is None: - logger.debug("Unable to retrieve the task from sender=%s", sender) + if sender is None: + logger.debug("Unable to retrieve the task from sender=%s", sender) return cast(Optional[Task], sender) diff --git a/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py b/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py index 49fe16ddbd..b619952073 100644 --- a/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py +++ b/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py @@ -28,6 +28,7 @@ def test_set_attributes_from_context(self): "expires": "later", "hostname": "localhost", "id": "44b7f305", + "origin": "gen8@localhost", "reply_to": "44b7f305", "retries": 4, "timelimit": ("now", "later"), @@ -56,6 +57,7 @@ def test_set_attributes_from_context(self): self.assertEqual(span.attributes.get("celery.eta"), "soon") self.assertEqual(span.attributes.get("celery.expires"), "later") self.assertEqual(span.attributes.get("celery.hostname"), "localhost") + self.assertEqual(span.attributes.get("celery.origin"), "gen8@localhost") self.assertEqual(span.attributes.get("celery.reply_to"), "44b7f305") self.assertEqual(span.attributes.get("celery.retries"), 4)