From 6fdaf4472335bf5dc3118dc29ec5f83cee895b78 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Tue, 19 Nov 2024 20:34:10 +0100 Subject: [PATCH] opentelemetry-api: fix wrong conditional on duplicated events log provider warning (#4299) --- CHANGELOG.md | 2 ++ .../src/opentelemetry/_events/__init__.py | 2 +- .../tests/events/test_event_logger_provider.py | 17 ++++++++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47fac23541..bf0ae6a891 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#4259](https://github.com/open-telemetry/opentelemetry-python/pull/4259)) - sdk: setup EventLogger when OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED is set ([#4270](https://github.com/open-telemetry/opentelemetry-python/pull/4270)) +- api: fix logging of duplicate EventLogger setup warning + ([#4299](https://github.com/open-telemetry/opentelemetry-python/pull/4299)) ## Version 1.28.0/0.49b0 (2024-11-05) diff --git a/opentelemetry-api/src/opentelemetry/_events/__init__.py b/opentelemetry-api/src/opentelemetry/_events/__init__.py index a291055ec7..e1e6a675a5 100644 --- a/opentelemetry-api/src/opentelemetry/_events/__init__.py +++ b/opentelemetry-api/src/opentelemetry/_events/__init__.py @@ -192,7 +192,7 @@ def set_elp() -> None: did_set = _EVENT_LOGGER_PROVIDER_SET_ONCE.do_once(set_elp) - if log and did_set: + if log and not did_set: _logger.warning( "Overriding of current EventLoggerProvider is not allowed" ) diff --git a/opentelemetry-api/tests/events/test_event_logger_provider.py b/opentelemetry-api/tests/events/test_event_logger_provider.py index 4c8bb54c8d..425697bfa3 100644 --- a/opentelemetry-api/tests/events/test_event_logger_provider.py +++ b/opentelemetry-api/tests/events/test_event_logger_provider.py @@ -11,13 +11,28 @@ class TestGlobals(EventsGlobalsTest, unittest.TestCase): - def test_set_event_logger_provider(self): + @patch("opentelemetry._events._logger") + def test_set_event_logger_provider(self, logger_mock): elp_mock = Mock() # pylint: disable=protected-access self.assertIsNone(events._EVENT_LOGGER_PROVIDER) set_event_logger_provider(elp_mock) self.assertIs(events._EVENT_LOGGER_PROVIDER, elp_mock) self.assertIs(get_event_logger_provider(), elp_mock) + logger_mock.warning.assert_not_called() + + # pylint: disable=no-self-use + @patch("opentelemetry._events._logger") + def test_set_event_logger_provider_will_warn_second_call( + self, logger_mock + ): + elp_mock = Mock() + set_event_logger_provider(elp_mock) + set_event_logger_provider(elp_mock) + + logger_mock.warning.assert_called_once_with( + "Overriding of current EventLoggerProvider is not allowed" + ) def test_get_event_logger_provider(self): # pylint: disable=protected-access