From 75d78fe4837cb9645fa95f821daab8f8ab33b56b Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Mon, 9 Dec 2024 12:41:30 +0100 Subject: [PATCH 1/6] 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.") From a2c8574ddb80e060e0867baafbcdeefcae6a4b4e Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Mon, 9 Dec 2024 12:46:43 +0100 Subject: [PATCH 2/6] Add changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09ad03d8258..04a39c48753 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 +- Add proper type for logs signal attributes + ([#4342](https://github.com/open-telemetry/opentelemetry-python/pull/4342)) - Fix crash exporting a log record with None body ([#4276](https://github.com/open-telemetry/opentelemetry-python/pull/4276)) - Fix metrics export with exemplar and no context and filtering observable instruments From e0c959ca80b5a8ee985872b58e033e96d18afffe Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Mon, 9 Dec 2024 14:18:36 +0100 Subject: [PATCH 3/6] Update tests --- opentelemetry-api/tests/logs/test_proxy.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opentelemetry-api/tests/logs/test_proxy.py b/opentelemetry-api/tests/logs/test_proxy.py index 8e87ceb96ea..e4a46451f08 100644 --- a/opentelemetry-api/tests/logs/test_proxy.py +++ b/opentelemetry-api/tests/logs/test_proxy.py @@ -19,7 +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 +from opentelemetry.util.types import LogAttributes class TestProvider(_logs.NoOpLoggerProvider): @@ -28,7 +28,7 @@ def get_logger( name: str, version: typing.Optional[str] = None, schema_url: typing.Optional[str] = None, - attributes: typing.Optional[Attributes] = None, + attributes: LogAttributes = None, ) -> _logs.Logger: return LoggerTest(name) From ddbd0180262650d8a399bac91ecd25b2019dd3d9 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Mon, 9 Dec 2024 14:26:42 +0100 Subject: [PATCH 4/6] LogRecord body should be AnyValue --- .../src/opentelemetry/_logs/_internal/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py b/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py index cc7a7624e69..94b6ac27075 100644 --- a/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py +++ b/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py @@ -37,14 +37,14 @@ from logging import getLogger from os import environ from time import time_ns -from typing import Any, Optional, cast +from typing import Optional, cast from opentelemetry._logs.severity import SeverityNumber from opentelemetry.environment_variables import _OTEL_PYTHON_LOGGER_PROVIDER from opentelemetry.trace.span import TraceFlags from opentelemetry.util._once import Once from opentelemetry.util._providers import _load_provider -from opentelemetry.util.types import LogAttributes +from opentelemetry.util.types import AnyValue, LogAttributes _logger = getLogger(__name__) @@ -66,7 +66,7 @@ def __init__( trace_flags: Optional["TraceFlags"] = None, severity_text: Optional[str] = None, severity_number: Optional[SeverityNumber] = None, - body: Optional[Any] = None, + body: AnyValue = None, attributes: LogAttributes = None, ): self.timestamp = timestamp From f5493f918f98b2f16bffad7fbd4c0c966e800cce Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Mon, 9 Dec 2024 14:28:58 +0100 Subject: [PATCH 5/6] Fix docs build --- opentelemetry-api/src/opentelemetry/util/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-api/src/opentelemetry/util/types.py b/opentelemetry-api/src/opentelemetry/util/types.py index a544948bbce..7598e1d6530 100644 --- a/opentelemetry-api/src/opentelemetry/util/types.py +++ b/opentelemetry-api/src/opentelemetry/util/types.py @@ -57,4 +57,4 @@ ..., ] -LogAttributes = Optional[Mapping[str, AnyValue]] +LogAttributes = Optional[Mapping[str, "AnyValue"]] From 0aa6c02464125b437c0a406b2bfcba426553baa2 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Mon, 9 Dec 2024 14:53:32 +0100 Subject: [PATCH 6/6] Remove unused ignore --- opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py b/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py index 94b6ac27075..93de2fc2d81 100644 --- a/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py +++ b/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py @@ -78,7 +78,7 @@ def __init__( self.trace_flags = trace_flags self.severity_text = severity_text self.severity_number = severity_number - self.body = body # type: ignore + self.body = body self.attributes = attributes