Skip to content

Add sdk._logs directory to pyright, fix existing lint errors #4583

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
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 AnyValue, _ExtendedAttributes
from opentelemetry.util.types import AnyValue, Attributes

_logger = getLogger(__name__)

Expand All @@ -67,7 +67,7 @@ def __init__(
severity_text: Optional[str] = None,
severity_number: Optional[SeverityNumber] = None,
body: AnyValue = None,
attributes: Optional[_ExtendedAttributes] = None,
attributes: Optional[Attributes] = 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[_ExtendedAttributes] = None,
attributes: Optional[Attributes] = 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[_ExtendedAttributes] = None,
attributes: Optional[Attributes] = 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[_ExtendedAttributes] = None,
attributes: Optional[Attributes] = None,
) -> Logger:
"""Returns a `Logger` for use by the given instrumentation library.

Expand Down Expand Up @@ -197,7 +197,7 @@ def get_logger(
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
attributes: Optional[_ExtendedAttributes] = None,
attributes: Optional[Attributes] = None,
) -> Logger:
"""Returns a NoOpLogger."""
return NoOpLogger(
Expand All @@ -211,7 +211,7 @@ def get_logger(
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
attributes: Optional[_ExtendedAttributes] = None,
attributes: Optional[Attributes] = None,
) -> Logger:
if _LOGGER_PROVIDER:
return _LOGGER_PROVIDER.get_logger(
Expand Down Expand Up @@ -274,7 +274,7 @@ def get_logger(
instrumenting_library_version: str = "",
logger_provider: Optional[LoggerProvider] = None,
schema_url: Optional[str] = None,
attributes: Optional[_ExtendedAttributes] = None,
attributes: Optional[Attributes] = None,
) -> "Logger":
"""Returns a `Logger` for use within a python process.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
get_current_span,
)
from opentelemetry.trace.span import TraceFlags
from opentelemetry.util.types import AnyValue, _ExtendedAttributes
from opentelemetry.util.types import AnyValue, Attributes, _ExtendedAttributes

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -197,10 +197,14 @@ def __init__(
"severity_number": severity_number,
"body": body,
"attributes": BoundedAttributes(
maxlen=limits.max_attributes,
maxlen=limits.max_attributes
if limits
else LogLimits.UNSET,
attributes=attributes if bool(attributes) else None,
immutable=False,
max_value_len=limits.max_attribute_length,
max_value_len=limits.max_attribute_length
if limits
else LogLimits.UNSET,
extended_attributes=True,
),
}
Expand Down Expand Up @@ -232,7 +236,9 @@ def to_json(self, indent: int | None = 4) -> str:
dict(self.attributes) if bool(self.attributes) else None
),
"dropped_attributes": self.dropped_attributes,
"timestamp": ns_to_iso_str(self.timestamp),
"timestamp": ns_to_iso_str(self.timestamp)
if self.timestamp is not None
else None,
"observed_timestamp": ns_to_iso_str(self.observed_timestamp),
"trace_id": (
f"0x{format_trace_id(self.trace_id)}"
Expand Down Expand Up @@ -289,7 +295,7 @@ def shutdown(self):
"""Called when a :class:`opentelemetry.sdk._logs.Logger` is shutdown"""

@abc.abstractmethod
def force_flush(self, timeout_millis: int = 30000):
def force_flush(self, timeout_millis: int = 30000) -> bool:
"""Export all the received logs to the configured Exporter that have not yet
been exported.

Expand Down Expand Up @@ -554,7 +560,7 @@ def _translate(self, record: logging.LogRecord) -> LogRecord:
severity_text=level_name,
severity_number=severity_number,
body=body,
resource=logger.resource,
resource=logger.resource, # type: ignore [reportAttributeAccessIssue]
attributes=attributes,
)

Expand All @@ -573,9 +579,9 @@ def flush(self) -> None:
Flushes the logging output. Skip flushing if logging_provider has no force_flush method.
"""
if hasattr(self._logger_provider, "force_flush") and callable(
self._logger_provider.force_flush
self._logger_provider.force_flush # type: ignore [#reportAttributeAccessIssue]
):
self._logger_provider.force_flush()
self._logger_provider.force_flush() # type: ignore [#reportAttributeAccessIssue]


class Logger(APILogger):
Expand All @@ -602,7 +608,7 @@ def __init__(
def resource(self):
return self._resource

def emit(self, record: LogRecord):
def emit(self, record: LogRecord): # type: ignore [#reportIncompatibleMethodOverride]
"""Emits the :class:`LogData` by associating :class:`LogRecord`
and instrumentation info.
"""
Expand Down Expand Up @@ -643,7 +649,7 @@ def _get_logger_no_cache(
name: str,
version: str | None = None,
schema_url: str | None = None,
attributes: _ExtendedAttributes | None = None,
attributes: Attributes | None = None,
) -> Logger:
return Logger(
self._resource,
Expand Down Expand Up @@ -677,8 +683,8 @@ def get_logger(
name: str,
version: str | None = None,
schema_url: str | None = None,
attributes: _ExtendedAttributes | None = None,
) -> Logger:
attributes: Attributes | None = None,
) -> Union[Logger, APILogger]:
if self._disabled:
return NoOpLogger(
name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class LogExporter(abc.ABC):
"""

@abc.abstractmethod
def export(self, batch: Sequence[LogData]):
def export(self, batch: Sequence[LogData]) -> LogExportResult:
"""Exports a batch of logs.
Args:
batch: The list of `LogData` objects to be exported
Expand Down Expand Up @@ -192,7 +192,7 @@ def emit(self, log_data: LogData) -> None:
def shutdown(self):
return self._batch_processor.shutdown()

def force_flush(self, timeout_millis: Optional[int] = None):
def force_flush(self, timeout_millis: Optional[int] = None): # type: ignore [#reportIncompatibleMethodOverride]
return self._batch_processor.force_flush(timeout_millis)

@staticmethod
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ exclude = [
"opentelemetry-sdk/tests",
"opentelemetry-sdk/src/opentelemetry/sdk/_configuration",
"opentelemetry-sdk/src/opentelemetry/sdk/_events",
"opentelemetry-sdk/src/opentelemetry/sdk/_logs",
"opentelemetry-sdk/src/opentelemetry/sdk/error_handler",
"opentelemetry-sdk/src/opentelemetry/sdk/metrics",
"opentelemetry-sdk/src/opentelemetry/sdk/trace",
Expand Down
Loading