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

logs: introduce LogAttributes type #4342

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 Attributes
from opentelemetry.util.types import AnyValue, LogAttributes

_logger = getLogger(__name__)

Expand All @@ -66,8 +66,8 @@ def __init__(
trace_flags: Optional["TraceFlags"] = None,
severity_text: Optional[str] = None,
severity_number: Optional[SeverityNumber] = None,
body: Optional[Any] = None,
attributes: Optional["Attributes"] = None,
body: AnyValue = None,
attributes: LogAttributes = None,
):
self.timestamp = timestamp
if observed_timestamp is None:
Expand All @@ -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


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"]]
4 changes: 2 additions & 2 deletions opentelemetry-api/tests/logs/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

Expand Down
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
Loading