Skip to content

Commit b730182

Browse files
Allow reraising the root exception if instrumentation fails (#3545)
* Allow reraising the root exception if instrumentation fails I would rather completely fail startup in my services if instrumentation fails for whatever reason instead of just logging an exception and continuing. Use case: from opentelemetry import autoinstrumentation autoinstrumentation.initialize(swallow_exceptions=False) * Fix lint * Type hinting, re-raise original exception * One more type hint to indicate None return --------- Co-authored-by: Riccardo Magliocchetti <[email protected]>
1 parent 4a21b39 commit b730182

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6969
([#3517](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3517))
7070
- `opentelemetry-instrumentation-httpx` Add support for HTTP metrics
7171
([#3513](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3513))
72+
- `opentelemetry-instrumentation` Allow re-raising exception when instrumentation fails
73+
([#3545](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3545))
7274

7375
### Deprecated
7476

opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,12 @@ def run() -> None:
118118
execl(executable, executable, *args.command_args)
119119

120120

121-
def initialize():
122-
"""Setup auto-instrumentation, called by the sitecustomize module"""
121+
def initialize(*, swallow_exceptions: bool = True) -> None:
122+
"""
123+
Setup auto-instrumentation, called by the sitecustomize module
124+
125+
:param swallow_exceptions: Whether or not to propagate instrumentation exceptions to the caller. Exceptions are logged and swallowed by default.
126+
"""
123127
# prevents auto-instrumentation of subprocesses if code execs another python process
124128
if "PYTHONPATH" in environ:
125129
environ["PYTHONPATH"] = _python_path_without_directory(
@@ -131,5 +135,7 @@ def initialize():
131135
distro.configure()
132136
_load_configurators()
133137
_load_instrumentors(distro)
134-
except Exception: # pylint: disable=broad-except
138+
except Exception as exc: # pylint: disable=broad-except
135139
_logger.exception("Failed to auto initialize OpenTelemetry")
140+
if not swallow_exceptions:
141+
raise exc

opentelemetry-instrumentation/tests/auto_instrumentation/test_initialize.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,16 @@ def test_handles_exceptions(self, load_distro_mock, logger_mock):
5959
logger_mock.exception.assert_called_once_with(
6060
"Failed to auto initialize OpenTelemetry"
6161
)
62+
63+
@patch("opentelemetry.instrumentation.auto_instrumentation._logger")
64+
@patch("opentelemetry.instrumentation.auto_instrumentation._load_distro")
65+
def test_reraises_exceptions(self, load_distro_mock, logger_mock):
66+
# pylint:disable=no-self-use
67+
load_distro_mock.side_effect = ValueError("inner exception")
68+
with self.assertRaises(ValueError) as em:
69+
auto_instrumentation.initialize(swallow_exceptions=False)
70+
logger_mock.exception.assert_called_once_with(
71+
"Failed to auto initialize OpenTelemetry"
72+
)
73+
74+
self.assertEqual("inner exception", str(em.exception))

0 commit comments

Comments
 (0)