From 6631e89dd14f433628663aaa9bd45687b5519878 Mon Sep 17 00:00:00 2001 From: soumyadeepm04 <84105194+soumyadeepm04@users.noreply.github.com> Date: Fri, 19 Jul 2024 14:12:50 -0400 Subject: [PATCH] optional scope attributes for logger creation (#4035) --- CHANGELOG.md | 2 ++ .../opentelemetry/_logs/_internal/__init__.py | 20 +++++++++++++++++-- opentelemetry-api/tests/logs/test_proxy.py | 2 ++ .../sdk/_logs/_internal/__init__.py | 10 +++++++++- opentelemetry-sdk/tests/logs/test_logs.py | 4 ++++ 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d0a605580d..02866e9711a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +- optional scope attributes for logger creation + ([#4035](https://github.com/open-telemetry/opentelemetry-python/pull/4035)) - optional scope attribute for tracer creation ([#4028](https://github.com/open-telemetry/opentelemetry-python/pull/4028)) - OTLP exporter is encoding invalid span/trace IDs in the logs fix diff --git a/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py b/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py index c02f0bb3dd4..f20bd8507e5 100644 --- a/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py +++ b/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py @@ -90,11 +90,13 @@ def __init__( name: str, version: Optional[str] = None, schema_url: Optional[str] = None, + attributes: Optional[Attributes] = None, ) -> None: super().__init__() self._name = name self._version = version self._schema_url = schema_url + self._attributes = attributes @abstractmethod def emit(self, record: "LogRecord") -> None: @@ -117,10 +119,12 @@ def __init__( # pylint: disable=super-init-not-called name: str, version: Optional[str] = None, schema_url: Optional[str] = None, + attributes: Optional[Attributes] = None, ): self._name = name self._version = version self._schema_url = schema_url + self._attributes = attributes self._real_logger: Optional[Logger] = None self._noop_logger = NoOpLogger(name) @@ -134,6 +138,7 @@ def _logger(self) -> Logger: self._name, self._version, self._schema_url, + self._attributes, ) return self._real_logger return self._noop_logger @@ -153,6 +158,7 @@ def get_logger( name: str, version: Optional[str] = None, schema_url: Optional[str] = None, + attributes: Optional[Attributes] = None, ) -> Logger: """Returns a `Logger` for use by the given instrumentation library. @@ -190,9 +196,12 @@ def get_logger( name: str, version: Optional[str] = None, schema_url: Optional[str] = None, + attributes: Optional[Attributes] = None, ) -> Logger: """Returns a NoOpLogger.""" - return NoOpLogger(name, version=version, schema_url=schema_url) + return NoOpLogger( + name, version=version, schema_url=schema_url, attributes=attributes + ) class ProxyLoggerProvider(LoggerProvider): @@ -201,17 +210,20 @@ def get_logger( name: str, version: Optional[str] = None, schema_url: Optional[str] = None, + attributes: Optional[Attributes] = None, ) -> Logger: if _LOGGER_PROVIDER: return _LOGGER_PROVIDER.get_logger( name, version=version, schema_url=schema_url, + attributes=attributes, ) return ProxyLogger( name, version=version, schema_url=schema_url, + attributes=attributes, ) @@ -261,6 +273,7 @@ def get_logger( instrumenting_library_version: str = "", logger_provider: Optional[LoggerProvider] = None, schema_url: Optional[str] = None, + attributes: Optional[Attributes] = None, ) -> "Logger": """Returns a `Logger` for use within a python process. @@ -272,5 +285,8 @@ def get_logger( if logger_provider is None: logger_provider = get_logger_provider() return logger_provider.get_logger( - instrumenting_module_name, instrumenting_library_version, schema_url + instrumenting_module_name, + instrumenting_library_version, + schema_url, + attributes, ) diff --git a/opentelemetry-api/tests/logs/test_proxy.py b/opentelemetry-api/tests/logs/test_proxy.py index 9508a66327d..8e87ceb96ea 100644 --- a/opentelemetry-api/tests/logs/test_proxy.py +++ b/opentelemetry-api/tests/logs/test_proxy.py @@ -19,6 +19,7 @@ import opentelemetry._logs._internal as _logs_internal from opentelemetry import _logs from opentelemetry.test.globals_test import LoggingGlobalsTest +from opentelemetry.util.types import Attributes class TestProvider(_logs.NoOpLoggerProvider): @@ -27,6 +28,7 @@ def get_logger( name: str, version: typing.Optional[str] = None, schema_url: typing.Optional[str] = None, + attributes: typing.Optional[Attributes] = None, ) -> _logs.Logger: return LoggerTest(name) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py index 81103ea1638..89432d5b339 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py @@ -597,6 +597,7 @@ def __init__( instrumentation_scope.name, instrumentation_scope.version, instrumentation_scope.schema_url, + instrumentation_scope.attributes, ) self._resource = resource self._multi_log_record_processor = multi_log_record_processor @@ -646,10 +647,16 @@ def get_logger( name: str, version: Optional[str] = None, schema_url: Optional[str] = None, + attributes: Optional[Attributes] = None, ) -> Logger: if self._disabled: _logger.warning("SDK is disabled.") - return NoOpLogger(name, version=version, schema_url=schema_url) + return NoOpLogger( + name, + version=version, + schema_url=schema_url, + attributes=attributes, + ) return Logger( self._resource, self._multi_log_record_processor, @@ -657,6 +664,7 @@ def get_logger( name, version, schema_url, + attributes, ), ) diff --git a/opentelemetry-sdk/tests/logs/test_logs.py b/opentelemetry-sdk/tests/logs/test_logs.py index 71098531d4f..4cb2a46c00c 100644 --- a/opentelemetry-sdk/tests/logs/test_logs.py +++ b/opentelemetry-sdk/tests/logs/test_logs.py @@ -55,6 +55,7 @@ def test_get_logger(self): "name", version="version", schema_url="schema_url", + attributes={"key": "value"}, ) self.assertEqual(logger._instrumentation_scope.name, "name") @@ -62,6 +63,9 @@ def test_get_logger(self): self.assertEqual( logger._instrumentation_scope.schema_url, "schema_url" ) + self.assertEqual( + logger._instrumentation_scope.attributes, {"key": "value"} + ) @patch.dict("os.environ", {OTEL_SDK_DISABLED: "true"}) def test_get_logger_with_sdk_disabled(self):