From a53f66ebb287e93107506709e97025e6e20a2a7f Mon Sep 17 00:00:00 2001 From: shivanshu Date: Thu, 12 Dec 2024 18:51:54 +0530 Subject: [PATCH 1/4] fix: remove the origin check --- .../src/opentelemetry/instrumentation/celery/utils.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) 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") From 9d05c3956a61ff538deff839fd14515e80afd697 Mon Sep 17 00:00:00 2001 From: shivanshu Date: Fri, 20 Dec 2024 17:52:54 +0530 Subject: [PATCH 2/4] test: added a unit test for set_attributes_from_context Signed-off-by: shivanshu --- .../tests/test_utils.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py b/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py index a2f6e4338c..b627332e79 100644 --- a/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py +++ b/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py @@ -282,3 +282,27 @@ 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""" + # Create a mock span + span = mock.Mock() + span.is_recording.return_value = True + + # Create a context with both 'origin' and 'hostname' keys + context = { + "origin": "gen8@b98c7aca4628", + "hostname": "celery@7c2c2cd6a5b5", + } + + # Call the function + utils.set_attributes_from_context(span, context) + + # Verify that both attributes were set with their original keys + span.set_attribute.assert_has_calls( + [ + mock.call("celery.origin", "gen8@b98c7aca4628"), + mock.call("celery.hostname", "celery@7c2c2cd6a5b5"), + ], + any_order=True, + ) From f016b4406af1368bfb4b432d12a8b0b24dd4e04a Mon Sep 17 00:00:00 2001 From: Shivanshu Raj Shrivastava Date: Thu, 13 Feb 2025 16:55:18 +0530 Subject: [PATCH 3/4] chore: review comments Signed-off-by: Shivanshu Raj Shrivastava --- CHANGELOG.md | 3 +++ .../opentelemetry-instrumentation-celery/tests/test_utils.py | 4 ---- 2 files changed, 3 insertions(+), 4 deletions(-) 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/tests/test_utils.py b/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py index b627332e79..c5d38be7f7 100644 --- a/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py +++ b/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py @@ -285,20 +285,16 @@ def test_task_id_from_protocol_v2(self): def test_origin_and_hostname_attributes(self): """Test that 'origin' and 'hostname' are distinct attributes""" - # Create a mock span span = mock.Mock() span.is_recording.return_value = True - # Create a context with both 'origin' and 'hostname' keys context = { "origin": "gen8@b98c7aca4628", "hostname": "celery@7c2c2cd6a5b5", } - # Call the function utils.set_attributes_from_context(span, context) - # Verify that both attributes were set with their original keys span.set_attribute.assert_has_calls( [ mock.call("celery.origin", "gen8@b98c7aca4628"), From 2e837308754cd7b38d13443355b79a9e268e783b Mon Sep 17 00:00:00 2001 From: Shivanshu Raj Shrivastava Date: Thu, 20 Feb 2025 00:14:08 +0530 Subject: [PATCH 4/4] lint: fix lint Signed-off-by: Shivanshu Raj Shrivastava --- .../tests/test_utils.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py b/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py index c5d38be7f7..c8713c65b6 100644 --- a/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py +++ b/instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py @@ -302,3 +302,14 @@ def test_origin_and_hostname_attributes(self): ], 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", + )