diff --git a/CHANGELOG.md b/CHANGELOG.md index 101cafd361..7d9a0d4a02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- `opentelemetry-instrumentation-celery` Populate both origin and hostname correctly to span attributes + ([#3097](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3097)) + - `opentelemetry-instrumentation-botocore` Add support for GenAI user events and lazy initialize tracer ([#3258](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3258)) - `opentelemetry-instrumentation-botocore` Add support for GenAI system events 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 d7ca77af8a..107d992ea7 100644 --- a/instrumentation/opentelemetry-instrumentation-celery/src/opentelemetry/instrumentation/celery/utils.py +++ b/instrumentation/opentelemetry-instrumentation-celery/src/opentelemetry/instrumentation/celery/utils.py @@ -81,12 +81,7 @@ def set_attributes_from_context(span, 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") diff --git a/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py b/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py index a2f6e4338c..c8713c65b6 100644 --- a/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py +++ b/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py @@ -282,3 +282,34 @@ def test_task_id_from_protocol_v2(self): task_id = utils.retrieve_task_id_from_message(context) self.assertEqual(task_id, "7e917b83-4018-431d-9832-73a28e1fb6c0") + + def test_origin_and_hostname_attributes(self): + """Test that 'origin' and 'hostname' are distinct attributes""" + span = mock.Mock() + span.is_recording.return_value = True + + context = { + "origin": "gen8@b98c7aca4628", + "hostname": "celery@7c2c2cd6a5b5", + } + + utils.set_attributes_from_context(span, context) + + span.set_attribute.assert_has_calls( + [ + mock.call("celery.origin", "gen8@b98c7aca4628"), + mock.call("celery.hostname", "celery@7c2c2cd6a5b5"), + ], + any_order=True, + ) + span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext)) + utils.set_attributes_from_context(span, context) + + self.assertEqual( + span.attributes.get("celery.origin"), + "gen8@b98c7aca4628", + ) + self.assertEqual( + span.attributes.get("celery.hostname"), + "celery@7c2c2cd6a5b5", + )