Skip to content

Commit 22c52c2

Browse files
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(reraise=True)
1 parent e2ba6d4 commit 22c52c2

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3737
([#3520](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3520))
3838
- `opentelemetry-instrumentation-botocore` Ensure spans end on early stream closure for Bedrock Streaming APIs
3939
([#3481](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3481))
40+
- `opentelemetry-instrumentation` Allow re-raising exception when instrumentation fails
41+
([#3545](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3545))
4042

4143
### Breaking changes
4244

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(*, reraise=False):
122+
"""
123+
Setup auto-instrumentation, called by the sitecustomize module
124+
125+
:param reraise: Whether or not to re-raise exceptions in the auto-instrumentation process to the caller. Exceptions are logged and swalloed 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 e: # pylint: disable=broad-except
135139
_logger.exception("Failed to auto initialize OpenTelemetry")
140+
if reraise:
141+
raise ValueError("Failed to auto initialize OpenTelemetry") from e

opentelemetry-instrumentation/tests/auto_instrumentation/test_initialize.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,14 @@ 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
68+
with self.assertRaises(ValueError):
69+
auto_instrumentation.initialize(reraise=True)
70+
logger_mock.exception.assert_called_once_with(
71+
"Failed to auto initialize OpenTelemetry"
72+
)

0 commit comments

Comments
 (0)