diff --git a/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py b/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py index f20bd8507e..fa5e9e6937 100644 --- a/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py +++ b/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py @@ -41,6 +41,7 @@ from opentelemetry._logs.severity import SeverityNumber from opentelemetry.environment_variables import _OTEL_PYTHON_LOGGER_PROVIDER +from opentelemetry.sdk.util.instrumentation import InstrumentationScope from opentelemetry.trace.span import TraceFlags from opentelemetry.util._once import Once from opentelemetry.util._providers import _load_provider @@ -159,6 +160,7 @@ def get_logger( version: Optional[str] = None, schema_url: Optional[str] = None, attributes: Optional[Attributes] = None, + instrumentation_scope: Optional[InstrumentationScope] = None, ) -> Logger: """Returns a `Logger` for use by the given instrumentation library. @@ -274,6 +276,7 @@ def get_logger( logger_provider: Optional[LoggerProvider] = None, schema_url: Optional[str] = None, attributes: Optional[Attributes] = None, + instrumentation_scope: Optional[InstrumentationScope] = None, ) -> "Logger": """Returns a `Logger` for use within a python process. @@ -284,9 +287,17 @@ def get_logger( """ if logger_provider is None: logger_provider = get_logger_provider() + if isinstance(logger_provider, NoOpLoggerProvider): + return NoOpLogger( + instrumenting_module_name, + version=instrumenting_library_version, + schema_url=schema_url, + attributes=attributes, + ) return logger_provider.get_logger( instrumenting_module_name, instrumenting_library_version, schema_url, attributes, + instrumentation_scope, ) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py index 89432d5b33..f79b63376c 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py @@ -468,11 +468,14 @@ def __init__( self, level=logging.NOTSET, logger_provider=None, + instrumentation_scope=None, ) -> None: super().__init__(level=level) self._logger_provider = logger_provider or get_logger_provider() self._logger = get_logger( - __name__, logger_provider=self._logger_provider + __name__, + logger_provider=self._logger_provider, + instrumentation_scope=instrumentation_scope, ) @staticmethod @@ -648,6 +651,7 @@ def get_logger( version: Optional[str] = None, schema_url: Optional[str] = None, attributes: Optional[Attributes] = None, + instrumentation_scope: Optional[InstrumentationScope] = None, ) -> Logger: if self._disabled: _logger.warning("SDK is disabled.") @@ -657,6 +661,17 @@ def get_logger( schema_url=schema_url, attributes=attributes, ) + if instrumentation_scope: + return Logger( + self._resource, + self._multi_log_record_processor, + InstrumentationScope( + instrumentation_scope.name, + instrumentation_scope.version, + instrumentation_scope.schema_url, + instrumentation_scope.attributes, + ), + ) return Logger( self._resource, self._multi_log_record_processor, diff --git a/opentelemetry-sdk/tests/logs/test_logs.py b/opentelemetry-sdk/tests/logs/test_logs.py index 4cb2a46c00..894fb0ed1c 100644 --- a/opentelemetry-sdk/tests/logs/test_logs.py +++ b/opentelemetry-sdk/tests/logs/test_logs.py @@ -24,6 +24,7 @@ ) from opentelemetry.sdk.environment_variables import OTEL_SDK_DISABLED from opentelemetry.sdk.resources import Resource +from opentelemetry.sdk.util.instrumentation import InstrumentationScope class TestLoggerProvider(unittest.TestCase): @@ -67,6 +68,37 @@ def test_get_logger(self): logger._instrumentation_scope.attributes, {"key": "value"} ) + def test_get_logger_instrumentation(self): + """ + `LoggerProvider.get_logger` arguments are used to create an + `InstrumentationScope` object on the created `Logger`. + """ + instruments = { + "name": "instrument_name", + "version": "instrument_version", + "schema_url": "instrument_schema_url", + "attributes": {"instrument_key": "instrument_value"}, + } + logger = LoggerProvider().get_logger( + "name", + version="version", + schema_url="schema_url", + attributes={"key": "value"}, + instrumentation_scope=InstrumentationScope(**instruments), + ) + + self.assertEqual(logger._instrumentation_scope.name, "instrument_name") + self.assertEqual( + logger._instrumentation_scope.version, "instrument_version" + ) + self.assertEqual( + logger._instrumentation_scope.schema_url, "instrument_schema_url" + ) + self.assertEqual( + logger._instrumentation_scope.attributes, + {"instrument_key": "instrument_value"}, + ) + @patch.dict("os.environ", {OTEL_SDK_DISABLED: "true"}) def test_get_logger_with_sdk_disabled(self): logger = LoggerProvider().get_logger(Mock())