Skip to content

Logs API: not possible to use without SDK #4319

@lmolkova

Description

@lmolkova

Let's assume I'm building a logging bridge for one of many python non-standard loggers (e.g. structlog).

I should be able to create a library using logging (bridge) API alone.

So I should write a code like:

from opentelemetry._logs import get_logger_provider
from opentelemetry._logs import LogRecord as APILogRecord

logger = get_logger_provider().get_logger(__name__)

logger.emit(APILogRecord(body="Hello, World!", attributes={"foo": "bar"}))

It'd fail with

opentelemetry\sdk\_logs\_internal\export\__init__.py", line 97, in <lambda>
    formatter: Callable[[LogRecord], str] = lambda record: record.to_json()
                                                           ^^^^^^^^^^^^^^
AttributeError: 'LogRecord' object has no attribute 'to_json'

Probably related to #3552


The recommendation

  1. Make it possible to (properly) use Logs API alone without touching SDK types
  2. Add example and tests that use vanilla logs API (with note that it's for logging bridges, not end users) (update - this is not necessary anymore, logs API is now for everyone to use)
  3. Call API and SDK log records differently to minimize confusion. E.g. SDK one could be ReadWriteLogRecord and should probably replace LogData (see Logs SDK: consider removing LogData and extending SDK LogRecord to have instrumentation scope #4313)
  4. Consider using a pattern similar to tracing when users don't need to instantiate a new span. Instead they could call logger.emit(....) with parameters and the SDK implementation of logger will create SDK implementation of LogRecord

Part of open-telemetry/community#1751

Metadata

Metadata

Assignees

No one assigned

    Labels

    loggingpriority:p1Issues that should be resolved in the upcoming release (except for zero-day hotfix release)

    Type

    No type

    Projects

    Status

    In Progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions