Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: functionality to configure instrumentation_scope from logging handler class (WIP) #4073

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand All @@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Expand All @@ -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,
Expand Down
32 changes: 32 additions & 0 deletions opentelemetry-sdk/tests/logs/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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())
Expand Down
Loading