Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
lmolkova committed Dec 7, 2024
1 parent 95f43f3 commit 87c2b08
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 245 deletions.
86 changes: 28 additions & 58 deletions opentelemetry-api/src/opentelemetry/_logs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,39 +73,6 @@
_logger = getLogger(__name__)


class LogRecord(ABC):
"""A LogRecord instance represents an event being logged.
LogRecord instances are created and emitted via `Logger`
every time something is logged. They contain all the information
pertinent to the event being logged.
"""

def __init__(
self,
timestamp: Optional[int] = None,
observed_timestamp: Optional[int] = None,
trace_id: Optional[int] = None,
span_id: Optional[int] = None,
trace_flags: Optional["TraceFlags"] = None,
severity_text: Optional[str] = None,
severity_number: Optional[SeverityNumber] = None,
body: Optional[Any] = None,
attributes: Optional["Attributes"] = None,
):
self.timestamp = timestamp
if observed_timestamp is None:
observed_timestamp = time_ns()
self.observed_timestamp = observed_timestamp
self.trace_id = trace_id
self.span_id = span_id
self.trace_flags = trace_flags
self.severity_text = severity_text
self.severity_number = severity_number
self.body = body # type: ignore
self.attributes = attributes


class Logger(ABC):
"""Handles emitting events and logs via `LogRecord`."""

Expand All @@ -126,18 +93,14 @@ def __init__(
def is_enabled(
self,
severity_number: Optional[SeverityNumber] = None,
context: Optional[Context] = None
context: Optional[Context] = None,
) -> bool:
"""Returns True if the logger is enabled for given severity and context, False otherwise."""

@abstractmethod
def emit(self, record: "LogRecord") -> None:
"""Emits a :class:`LogRecord` representing a log to the processing pipeline."""

@abstractmethod
def emit_event(
def emit(
self,
name: str,
name: str = None,
timestamp: Optional[int] = None,
observed_timestamp: Optional[int] = None,
severity_number: Optional[SeverityNumber] = None,
Expand All @@ -146,7 +109,7 @@ def emit_event(
body: Optional[Any] = None,
attributes: Optional[Attributes] = None,
) -> None:
"""Emits an event to the processing pipeline."""
"""Emits a log record."""


class NoOpLogger(Logger):
Expand All @@ -158,20 +121,19 @@ class NoOpLogger(Logger):
def is_enabled(
self,
severity_number: Optional[SeverityNumber] = None,
context: Optional[Context] = None
context: Optional[Context] = None,
) -> bool:
return False

def emit(self, record: "LogRecord") -> None:
pass

def emit_event(
def emit(
self,
name: str,
name: str = None,
timestamp: Optional[int] = None,
observed_timestamp: Optional[int] = None,
severity_number: Optional[SeverityNumber] = None,
severity_text: Optional[str] = None,
context: Optional[Context] = None,
body: Optional[Any] = None,
severity_number: Optional[SeverityNumber] = None,
attributes: Optional[Attributes] = None,
) -> None:
pass
Expand Down Expand Up @@ -210,24 +172,32 @@ def _logger(self) -> Logger:
def is_enabled(
self,
severity_number: Optional[SeverityNumber] = None,
context: Optional[Context] = None
context: Optional[Context] = None,
) -> bool:
return self._logger.is_enabled(severity_number=severity_number, context=context)

def emit(self, record: LogRecord) -> None:
self._logger.emit(record)
return self._logger.is_enabled(
severity_number=severity_number, context=context
)

def emit_event(
def emit(
self,
name: str,
name: str = None,
timestamp: Optional[int] = None,
observed_timestamp: Optional[int] = None,
severity_number: Optional[SeverityNumber] = None,
severity_text: Optional[str] = None,
context: Optional[Context] = None,
body: Optional[Any] = None,
severity_number: Optional[SeverityNumber] = None,
attributes: Optional[Attributes] = None,
) -> None:
self._logger.emit_event(
name, timestamp, context, body, severity_number, attributes
self._logger.emit(
name=name,
timestamp=timestamp,
observed_timestamp=observed_timestamp,
severity_number=severity_number,
severity_text=severity_text,
context=context,
body=body,
attributes=attributes,
)


Expand Down
61 changes: 0 additions & 61 deletions opentelemetry-api/tests/events/test_event_logger_provider.py

This file was deleted.

27 changes: 0 additions & 27 deletions opentelemetry-api/tests/logs/test_log_record.py

This file was deleted.

2 changes: 1 addition & 1 deletion opentelemetry-api/tests/logs/test_logger_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_get_logger_provider(self):
"os.environ",
{_OTEL_PYTHON_LOGGER_PROVIDER: "test_logger_provider"},
):
with patch("opentelemetry._logs._internal._load_provider", Mock()):
with patch("opentelemetry._logs._load_provider", Mock()):
with patch(
"opentelemetry._logs.cast",
Mock(**{"return_value": "test_logger_provider"}),
Expand Down
12 changes: 11 additions & 1 deletion opentelemetry-api/tests/logs/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import unittest

from opentelemetry import _logs
from opentelemetry.context.context import Context
from opentelemetry.test.globals_test import LoggingGlobalsTest
from opentelemetry.util.types import Attributes

Expand All @@ -33,7 +34,16 @@ def get_logger(


class LoggerTest(_logs.NoOpLogger):
def emit(self, record: _logs.LogRecord) -> None:
def emit(self,
name: str = None,
timestamp: typing.Optional[int] = None,
observed_timestamp: typing.Optional[int] = None,
severity_number: typing.Optional[_logs.SeverityNumber] = None,
severity_text: typing.Optional[str] = None,
context: typing.Optional[Context] = None,
body: typing.Optional[typing.Any] = None,
attributes: typing.Optional[Attributes] = None,
) -> None:
pass


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

from typing_extensions import Literal

from opentelemetry._events import set_event_logger_provider
from opentelemetry._logs import set_logger_provider
from opentelemetry.environment_variables import (
OTEL_LOGS_EXPORTER,
Expand All @@ -34,7 +33,6 @@
OTEL_TRACES_EXPORTER,
)
from opentelemetry.metrics import set_meter_provider
from opentelemetry.sdk._events import EventLoggerProvider
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor, LogExporter
from opentelemetry.sdk.environment_variables import (
Expand Down Expand Up @@ -250,10 +248,6 @@ def _init_logging(

logging.getLogger().addHandler(handler)

event_logger_provider = EventLoggerProvider(logger_provider=provider)
set_event_logger_provider(event_logger_provider)


def _import_exporters(
trace_exporter_names: Sequence[str],
metric_exporter_names: Sequence[str],
Expand Down
Loading

0 comments on commit 87c2b08

Please sign in to comment.