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 <riccardo.magliocchetti@gmail.com>
This commit is contained in:
Phillip Verheyden
2025-06-04 03:44:45 -05:00
committed by GitHub
parent 4a21b3974b
commit b7301823a0
3 changed files with 24 additions and 3 deletions

View File

@ -59,3 +59,16 @@ class TestInitialize(TestCase):
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))