diff --git a/CHANGELOG.md b/CHANGELOG.md index 26962b4eee..8efbf4a53a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#3517](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3517)) - `opentelemetry-instrumentation-httpx` Add support for HTTP metrics ([#3513](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3513)) +- `opentelemetry-instrumentation` Allow re-raising exception when instrumentation fails + ([#3545](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3545)) ### Deprecated diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/__init__.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/__init__.py index 69af0b4cea..8571d0e5a2 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/__init__.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/__init__.py @@ -118,8 +118,12 @@ def run() -> None: execl(executable, executable, *args.command_args) -def initialize(): - """Setup auto-instrumentation, called by the sitecustomize module""" +def initialize(*, swallow_exceptions: bool = True) -> None: + """ + Setup auto-instrumentation, called by the sitecustomize module + + :param swallow_exceptions: Whether or not to propagate instrumentation exceptions to the caller. Exceptions are logged and swallowed by default. + """ # prevents auto-instrumentation of subprocesses if code execs another python process if "PYTHONPATH" in environ: environ["PYTHONPATH"] = _python_path_without_directory( @@ -131,5 +135,7 @@ def initialize(): distro.configure() _load_configurators() _load_instrumentors(distro) - except Exception: # pylint: disable=broad-except + except Exception as exc: # pylint: disable=broad-except _logger.exception("Failed to auto initialize OpenTelemetry") + if not swallow_exceptions: + raise exc diff --git a/opentelemetry-instrumentation/tests/auto_instrumentation/test_initialize.py b/opentelemetry-instrumentation/tests/auto_instrumentation/test_initialize.py index 6d05a69c8e..715da0d2f6 100644 --- a/opentelemetry-instrumentation/tests/auto_instrumentation/test_initialize.py +++ b/opentelemetry-instrumentation/tests/auto_instrumentation/test_initialize.py @@ -59,3 +59,16 @@ def test_handles_exceptions(self, load_distro_mock, logger_mock): logger_mock.exception.assert_called_once_with( "Failed to auto initialize OpenTelemetry" ) + + @patch("opentelemetry.instrumentation.auto_instrumentation._logger") + @patch("opentelemetry.instrumentation.auto_instrumentation._load_distro") + def test_reraises_exceptions(self, load_distro_mock, logger_mock): + # pylint:disable=no-self-use + load_distro_mock.side_effect = ValueError("inner exception") + with self.assertRaises(ValueError) as em: + auto_instrumentation.initialize(swallow_exceptions=False) + logger_mock.exception.assert_called_once_with( + "Failed to auto initialize OpenTelemetry" + ) + + self.assertEqual("inner exception", str(em.exception))