From 75d78fe4837cb9645fa95f821daab8f8ab33b56b Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Mon, 9 Dec 2024 12:41:30 +0100 Subject: [PATCH] logs: introduce LogAttributes type Logs attribute accepts AnyValue as AttributeValue add a type to describe that and start using it. --- .../opentelemetry/_logs/_internal/__init__.py | 16 ++++++++-------- .../src/opentelemetry/util/types.py | 2 ++ .../sdk/_logs/_internal/__init__.py | 12 ++++++------ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py b/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py index f20bd8507e5..cc7a7624e69 100644 --- a/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py +++ b/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py @@ -44,7 +44,7 @@ from opentelemetry.trace.span import TraceFlags from opentelemetry.util._once import Once from opentelemetry.util._providers import _load_provider -from opentelemetry.util.types import Attributes +from opentelemetry.util.types import LogAttributes _logger = getLogger(__name__) @@ -67,7 +67,7 @@ def __init__( severity_text: Optional[str] = None, severity_number: Optional[SeverityNumber] = None, body: Optional[Any] = None, - attributes: Optional["Attributes"] = None, + attributes: LogAttributes = None, ): self.timestamp = timestamp if observed_timestamp is None: @@ -90,7 +90,7 @@ def __init__( name: str, version: Optional[str] = None, schema_url: Optional[str] = None, - attributes: Optional[Attributes] = None, + attributes: LogAttributes = None, ) -> None: super().__init__() self._name = name @@ -119,7 +119,7 @@ def __init__( # pylint: disable=super-init-not-called name: str, version: Optional[str] = None, schema_url: Optional[str] = None, - attributes: Optional[Attributes] = None, + attributes: LogAttributes = None, ): self._name = name self._version = version @@ -158,7 +158,7 @@ def get_logger( name: str, version: Optional[str] = None, schema_url: Optional[str] = None, - attributes: Optional[Attributes] = None, + attributes: LogAttributes = None, ) -> Logger: """Returns a `Logger` for use by the given instrumentation library. @@ -196,7 +196,7 @@ def get_logger( name: str, version: Optional[str] = None, schema_url: Optional[str] = None, - attributes: Optional[Attributes] = None, + attributes: LogAttributes = None, ) -> Logger: """Returns a NoOpLogger.""" return NoOpLogger( @@ -210,7 +210,7 @@ def get_logger( name: str, version: Optional[str] = None, schema_url: Optional[str] = None, - attributes: Optional[Attributes] = None, + attributes: LogAttributes = None, ) -> Logger: if _LOGGER_PROVIDER: return _LOGGER_PROVIDER.get_logger( @@ -273,7 +273,7 @@ def get_logger( instrumenting_library_version: str = "", logger_provider: Optional[LoggerProvider] = None, schema_url: Optional[str] = None, - attributes: Optional[Attributes] = None, + attributes: LogAttributes = None, ) -> "Logger": """Returns a `Logger` for use within a python process. diff --git a/opentelemetry-api/src/opentelemetry/util/types.py b/opentelemetry-api/src/opentelemetry/util/types.py index d9490ff08c9..a544948bbce 100644 --- a/opentelemetry-api/src/opentelemetry/util/types.py +++ b/opentelemetry-api/src/opentelemetry/util/types.py @@ -56,3 +56,5 @@ ], ..., ] + +LogAttributes = Optional[Mapping[str, AnyValue]] diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py index c2db81687a2..29060bf38f7 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py @@ -51,7 +51,7 @@ get_current_span, ) from opentelemetry.trace.span import TraceFlags -from opentelemetry.util.types import AnyValue, Attributes +from opentelemetry.util.types import AnyValue, LogAttributes _logger = logging.getLogger(__name__) @@ -179,9 +179,9 @@ def __init__( trace_flags: Optional[TraceFlags] = None, severity_text: Optional[str] = None, severity_number: Optional[SeverityNumber] = None, - body: Optional[AnyValue] = None, + body: AnyValue = None, resource: Optional[Resource] = None, - attributes: Optional[Attributes] = None, + attributes: LogAttributes = None, limits: Optional[LogLimits] = _UnsetLogLimits, ): super().__init__( @@ -476,7 +476,7 @@ def __init__( self._logger_provider = logger_provider or get_logger_provider() @staticmethod - def _get_attributes(record: logging.LogRecord) -> Attributes: + def _get_attributes(record: logging.LogRecord) -> LogAttributes: attributes = { k: v for k, v in vars(record).items() if k not in _RESERVED_ATTRS } @@ -633,7 +633,7 @@ def _get_logger_no_cache( name: str, version: Optional[str] = None, schema_url: Optional[str] = None, - attributes: Optional[Attributes] = None, + attributes: LogAttributes = None, ) -> Logger: return Logger( self._resource, @@ -667,7 +667,7 @@ def get_logger( name: str, version: Optional[str] = None, schema_url: Optional[str] = None, - attributes: Optional[Attributes] = None, + attributes: LogAttributes = None, ) -> Logger: if self._disabled: warnings.warn("SDK is disabled.")