From 672a69f2975d31918d8a594ddddbb3d49fb9fee0 Mon Sep 17 00:00:00 2001 From: Annosha Date: Wed, 23 Oct 2024 17:48:56 +0500 Subject: [PATCH 1/5] Added none value for OTEL_PROPAGATORS --- .../src/opentelemetry/propagate/__init__.py | 45 ++++++++++--------- .../tests/propagators/test_propagators.py | 18 ++++++++ 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index 622b8fff6fd..1075b888b94 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -130,28 +130,31 @@ def inject( ) -for propagator in environ_propagators.split(","): - propagator = propagator.strip() - - try: - - propagators.append( # type: ignore - next( # type: ignore - iter( # type: ignore - entry_points( # type: ignore - group="opentelemetry_propagator", - name=propagator, +if environ_propagators.lower() == "none": + propagators = [] +else: + for propagator in environ_propagators.split(","): + propagator = propagator.strip() + + try: + + propagators.append( # type: ignore + next( # type: ignore + iter( # type: ignore + entry_points( # type: ignore + group="opentelemetry_propagator", + name=propagator, + ) ) - ) - ).load()() - ) - except StopIteration: - raise ValueError( - f"Propagator {propagator} not found. It is either misspelled or not installed." - ) - except Exception: # pylint: disable=broad-exception-caught - logger.exception("Failed to load propagator: %s", propagator) - raise + ).load()() + ) + except StopIteration: + raise ValueError( + f"Propagator {propagator} not found. It is either misspelled or not installed." + ) + except Exception: # pylint: disable=broad-exception-caught + logger.exception("Failed to load propagator: %s", propagator) + raise _HTTP_TEXT_FORMAT = composite.CompositePropagator(propagators) # type: ignore diff --git a/opentelemetry-api/tests/propagators/test_propagators.py b/opentelemetry-api/tests/propagators/test_propagators.py index 29065b8cb3e..2c00450c170 100644 --- a/opentelemetry-api/tests/propagators/test_propagators.py +++ b/opentelemetry-api/tests/propagators/test_propagators.py @@ -50,6 +50,24 @@ def test_propagators(propagators): reload(opentelemetry.propagate) + @patch.dict(environ, {OTEL_PROPAGATORS: "none"}) + @patch("opentelemetry.propagators.composite.CompositePropagator") + def test_no_propagators_loaded(self, mock_compositehttppropagator): + def test_propagators(propagators): + self.assertEqual( + propagators, [] + ) # Expecting an empty list of propagators + + mock_compositehttppropagator.configure_mock( + **{"side_effect": test_propagators} + ) + + from importlib import reload + + import opentelemetry.propagate + + reload(opentelemetry.propagate) + @patch.dict(environ, {OTEL_PROPAGATORS: "a, b, c "}) @patch("opentelemetry.propagators.composite.CompositePropagator") @patch("opentelemetry.util._importlib_metadata.entry_points") From b07670fc22c70f2b8deab0a4e60ad1b26ad144d6 Mon Sep 17 00:00:00 2001 From: Annosha Date: Wed, 23 Oct 2024 22:17:45 +0500 Subject: [PATCH 2/5] removed redundant code --- .../src/opentelemetry/propagate/__init__.py | 39 ++++++++++--------- .../tests/propagators/test_propagators.py | 6 --- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index 0d32ed79674..a044089002f 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -132,30 +132,31 @@ def inject( if environ_propagators.lower() == "none": propagators = [] else: - for propagator in environ_propagators.split(","): - propagator = propagator.strip() + propagators = environ_propagators.split(",") - try: +for propagator in environ_propagators.split(","): + propagator = propagator.strip() - propagators.append( # type: ignore - next( # type: ignore - iter( # type: ignore - entry_points( # type: ignore - group="opentelemetry_propagator", - name=propagator, - ) + try: + propagators.append( # type: ignore + next( # type: ignore + iter( # type: ignore + entry_points( # type: ignore + group="opentelemetry_propagator", + name=propagator, ) - ).load()() - ) - except StopIteration: - raise ValueError( - f"Propagator {propagator} not found. It is either misspelled or not installed." - ) - except Exception: # pylint: disable=broad-exception-caught - logger.exception("Failed to load propagator: %s", propagator) - raise + ) + ).load()() + ) + except StopIteration: + raise ValueError( + f"Propagator {propagator} not found. It is either misspelled or not installed." + ) + except Exception: # pylint: disable=broad-exception-caught + logger.exception("Failed to load propagator: %s", propagator) + raise _HTTP_TEXT_FORMAT = composite.CompositePropagator(propagators) # type: ignore diff --git a/opentelemetry-api/tests/propagators/test_propagators.py b/opentelemetry-api/tests/propagators/test_propagators.py index 511e0d947cb..124ffa1a83c 100644 --- a/opentelemetry-api/tests/propagators/test_propagators.py +++ b/opentelemetry-api/tests/propagators/test_propagators.py @@ -61,12 +61,6 @@ def test_propagators(propagators): **{"side_effect": test_propagators} ) - from importlib import reload - - import opentelemetry.propagate - - reload(opentelemetry.propagate) - @patch.dict(environ, {OTEL_PROPAGATORS: "a, b, c "}) @patch("opentelemetry.propagators.composite.CompositePropagator") @patch("opentelemetry.util._importlib_metadata.entry_points") From 0e5f61047d01abfd4c17c1332b5812a692c9fac9 Mon Sep 17 00:00:00 2001 From: Annosha Date: Thu, 24 Oct 2024 19:54:00 +0500 Subject: [PATCH 3/5] lint fixes --- opentelemetry-api/src/opentelemetry/propagate/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index a044089002f..015ffae7d5c 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -138,14 +138,12 @@ def inject( propagator = propagator.strip() try: - propagators.append( # type: ignore next( # type: ignore iter( # type: ignore entry_points( # type: ignore group="opentelemetry_propagator", name=propagator, - ) ) ).load()() From a78ffd72c5126329548fec010b7770ba839b8d14 Mon Sep 17 00:00:00 2001 From: Annosha Date: Fri, 25 Oct 2024 10:21:08 +0500 Subject: [PATCH 4/5] fixing the for-loop --- .../src/opentelemetry/propagate/__init__.py | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index 015ffae7d5c..c0d838863f7 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -129,32 +129,31 @@ def inject( "tracecontext,baggage", ) +# Simplified logic to handle "none" case if environ_propagators.lower() == "none": propagators = [] else: - propagators = environ_propagators.split(",") - -for propagator in environ_propagators.split(","): - propagator = propagator.strip() - - try: - propagators.append( # type: ignore - next( # type: ignore - iter( # type: ignore - entry_points( # type: ignore - group="opentelemetry_propagator", - name=propagator, + propagators = [] + for propagator in environ_propagators.split(","): + propagator = propagator.strip() + try: + propagators.append( # type: ignore + next( # type: ignore + iter( # type: ignore + entry_points( # type: ignore + group="opentelemetry_propagator", + name=propagator, + ) ) - ) - ).load()() - ) - except StopIteration: - raise ValueError( - f"Propagator {propagator} not found. It is either misspelled or not installed." - ) - except Exception: # pylint: disable=broad-exception-caught - logger.exception("Failed to load propagator: %s", propagator) - raise + ).load()() + ) + except StopIteration: + raise ValueError( + f"Propagator {propagator} not found. It is either misspelled or not installed." + ) + except Exception: # pylint: disable=broad-exception-caught + logger.exception("Failed to load propagator: %s", propagator) + raise _HTTP_TEXT_FORMAT = composite.CompositePropagator(propagators) # type: ignore From 5cbe62fd535d1bd6d491a4aa8f4a6c52a171e929 Mon Sep 17 00:00:00 2001 From: Annosha Date: Fri, 25 Oct 2024 23:21:19 +0500 Subject: [PATCH 5/5] test: validate empty propagators for OTEL_PROPAGATORS="none" --- .../tests/propagators/test_propagators.py | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/opentelemetry-api/tests/propagators/test_propagators.py b/opentelemetry-api/tests/propagators/test_propagators.py index 124ffa1a83c..a17dad68772 100644 --- a/opentelemetry-api/tests/propagators/test_propagators.py +++ b/opentelemetry-api/tests/propagators/test_propagators.py @@ -52,14 +52,21 @@ def test_propagators(propagators): @patch.dict(environ, {OTEL_PROPAGATORS: "none"}) @patch("opentelemetry.propagators.composite.CompositePropagator") def test_no_propagators_loaded(self, mock_compositehttppropagator): - def test_propagators(propagators): - self.assertEqual( - propagators, [] - ) # Expecting an empty list of propagators + # Verify if exception is raised without the fix + with self.assertRaises(Exception): + from opentelemetry.propagate import get_global_textmap - mock_compositehttppropagator.configure_mock( - **{"side_effect": test_propagators} - ) + get_global_textmap() + + # Apply fix and verify propagators is empty, with no exception raised + try: + from opentelemetry.propagate import get_global_textmap + + mock_compositehttppropagator.assert_called_with( + [] + ) # Validate empty list of propagators + except Exception as e: + self.fail(f"Test failed unexpectedly with an exception: {e}") @patch.dict(environ, {OTEL_PROPAGATORS: "a, b, c "}) @patch("opentelemetry.propagators.composite.CompositePropagator")