OpenTelemetry Structlog Instrumentation

The OpenTelemetry structlog integration provides a processor that emits structlog events as OpenTelemetry logs.

import structlog
from opentelemetry.instrumentation.structlog import StructlogInstrumentor

StructlogInstrumentor().instrument()

logger = structlog.get_logger()
logger.info("user logged in", user_id=42)

This will emit the structlog event as an OpenTelemetry LogRecord, preserving all context including trace context, custom attributes, and exception information.

class opentelemetry.instrumentation.structlog.StructlogProcessor(logger_provider=None)[source]

Bases: object

A structlog processor that translates structlog events into OpenTelemetry LogRecords.

This processor should be added to the structlog processor chain to emit logs to OpenTelemetry. It translates structlog’s event dictionary format into the OpenTelemetry Logs data model.

Parameters:

logger_provider – The LoggerProvider to use. If None, uses the global provider.

flush()[source]

Flush the logger provider.

Return type:

None

class opentelemetry.instrumentation.structlog.StructlogInstrumentor(*args, **kwargs)[source]

Bases: BaseInstrumentor

An instrumentor for the structlog logging library.

This instrumentor adds a StructlogProcessor to the structlog processor chain, enabling automatic emission of structlog events as OpenTelemetry logs.

Example

>>> from opentelemetry.instrumentation.structlog import StructlogInstrumentor
>>> import structlog
>>> StructlogInstrumentor().instrument()
>>> logger = structlog.get_logger()
>>> logger.info("hello", user="alice")
instrumentation_dependencies()[source]

Return the required instrumentation dependencies.

Return type:

Collection[str]