Skip to content

Commit

Permalink
opentelemetry-sdk: setup EventLogger when initializing logging (#4270)
Browse files Browse the repository at this point in the history
  • Loading branch information
xrmx authored Nov 12, 2024
1 parent 47b0541 commit a119185
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#4251](https://github.com/open-telemetry/opentelemetry-python/pull/4251))
- Fix recursion error with sdk disabled and handler added to root logger
([#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))

## Version 1.28.0/0.49b0 (2024-11-05)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 (
Expand Down Expand Up @@ -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],
Expand Down
24 changes: 24 additions & 0 deletions opentelemetry-sdk/tests/test_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit a119185

Please sign in to comment.