Skip to content

Commit

Permalink
logs: introduce LogAttributes type
Browse files Browse the repository at this point in the history
Logs attribute accepts AnyValue as AttributeValue add a type to describe
that and start using it.
  • Loading branch information
xrmx committed Dec 9, 2024
1 parent 415c94f commit 75d78fe
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
16 changes: 8 additions & 8 deletions opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-api/src/opentelemetry/util/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@
],
...,
]

LogAttributes = Optional[Mapping[str, AnyValue]]
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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__(
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.")
Expand Down

0 comments on commit 75d78fe

Please sign in to comment.