diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index afe406fb59..c0d838863f 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -129,28 +129,31 @@ def inject( "tracecontext,baggage", ) - -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, +# Simplified logic to handle "none" case +if environ_propagators.lower() == "none": + propagators = [] +else: + 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 diff --git a/opentelemetry-api/tests/propagators/test_propagators.py b/opentelemetry-api/tests/propagators/test_propagators.py index f0554ad281..a17dad6877 100644 --- a/opentelemetry-api/tests/propagators/test_propagators.py +++ b/opentelemetry-api/tests/propagators/test_propagators.py @@ -49,6 +49,25 @@ 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): + # Verify if exception is raised without the fix + with self.assertRaises(Exception): + from opentelemetry.propagate import get_global_textmap + + 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") @patch("opentelemetry.util._importlib_metadata.entry_points")