From ea809eb7a1e91bb8ddda1aeecd9bf268d3b5f97e Mon Sep 17 00:00:00 2001 From: Aaron Abbott Date: Thu, 14 Nov 2024 22:11:03 -0500 Subject: [PATCH] [release/v1.28.x-0.49bx] opentelemetry-sdk: setup EventLogger when initializing logging (#4270) (#4283) * opentelemetry-sdk: setup EventLogger when initializing logging (#4270) * Move cherry-picked changelog entry to Unreleased section --------- Co-authored-by: Riccardo Magliocchetti --- CHANGELOG.md | 2 ++ .../sdk/_configuration/__init__.py | 5 ++++ opentelemetry-sdk/tests/test_configurator.py | 24 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cb1e1e0f2..7fb6fc4411 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix crash exporting a log record with None body ([#4276](https://github.com/open-telemetry/opentelemetry-python/pull/4276)) +- sdk: setup EventLogger when OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED is set + ([#4270](https://github.com/open-telemetry/opentelemetry-python/pull/4270)) ## Version 1.28.1/0.49b1 (2024-11-08) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index 8cc15d3bf4..2dffd78023 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -25,6 +25,7 @@ from typing_extensions import Literal +from opentelemetry._events import set_event_logger_provider from opentelemetry._logs import set_logger_provider from opentelemetry.environment_variables import ( OTEL_LOGS_EXPORTER, @@ -33,6 +34,7 @@ OTEL_TRACES_EXPORTER, ) from opentelemetry.metrics import set_meter_provider +from opentelemetry.sdk._events import EventLoggerProvider from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler from opentelemetry.sdk._logs.export import BatchLogRecordProcessor, LogExporter from opentelemetry.sdk.environment_variables import ( @@ -248,6 +250,9 @@ def _init_logging( logging.getLogger().addHandler(handler) + event_logger_provider = EventLoggerProvider(logger_provider=provider) + set_event_logger_provider(event_logger_provider) + def _import_exporters( trace_exporter_names: Sequence[str], diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index 5b04a6f3b9..034d5b8c9b 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -586,14 +586,32 @@ def setUp(self): "opentelemetry.sdk._configuration.set_logger_provider" ) + self.event_logger_provider_instance_mock = Mock() + self.event_logger_provider_patch = patch( + "opentelemetry.sdk._configuration.EventLoggerProvider", + return_value=self.event_logger_provider_instance_mock, + ) + self.set_event_logger_provider_patch = patch( + "opentelemetry.sdk._configuration.set_event_logger_provider" + ) + self.processor_mock = self.processor_patch.start() self.provider_mock = self.provider_patch.start() self.set_provider_mock = self.set_provider_patch.start() + self.event_logger_provider_mock = ( + self.event_logger_provider_patch.start() + ) + self.set_event_logger_provider_mock = ( + self.set_event_logger_provider_patch.start() + ) + def tearDown(self): self.processor_patch.stop() self.set_provider_patch.stop() self.provider_patch.stop() + self.event_logger_provider_patch.stop() + self.set_event_logger_provider_patch.stop() root_logger = getLogger("root") root_logger.handlers = [ handler @@ -616,6 +634,12 @@ def test_logging_init_empty(self): provider.resource.attributes.get("telemetry.auto.version"), "auto-version", ) + self.event_logger_provider_mock.assert_called_once_with( + logger_provider=provider + ) + self.set_event_logger_provider_mock.assert_called_once_with( + self.event_logger_provider_instance_mock + ) @patch.dict( environ,