OpenTelemetry Instrumentation for Logging
Automatic OpenTelemetry trace context injection into Python's standard logging module
Repository Health
Technical Analysis
opentelemetry-instrumentation-logging automatically instruments Python’s standard logging module so log records carry OpenTelemetry trace context. Once installed, it patches the active log record factory to attach otelSpanID, otelTraceID, otelTraceSampled, and otelServiceName attributes to every log record emitted while a span is active.
This lets log lines be correlated with distributed traces in an observability backend without hand-wiring trace IDs into log statements. It supports both an opt-in inject_trace_context mode that adds the attributes silently, and a set_logging_format mode that also reconfigures the root logging format to print those attributes inline.
What You Get
- A
LoggingInstrumentorthat patches the stdlib logging record factory - Opt-in injection of
otelSpanID,otelTraceID,otelTraceSampled, andotelServiceNameonto log records - Automatic
logging.basicConfig()reconfiguration viaset_logging_format=True - An auto-installed logging handler that forwards records to the OpenTelemetry SDK’s logger provider
- Environment-variable configuration (
OTEL_PYTHON_LOG_CORRELATION,OTEL_PYTHON_LOG_LEVEL, etc.) alongside code-based options
Common Use Cases
- Correlating application log lines with distributed traces in an observability backend
- Adding trace/span IDs to logs without changing every log call site
- Feeding structured application logs into an OpenTelemetry Collector pipeline
- Standardizing log correlation across microservices that share OTel instrumentation
Under The Hood
Architecture — the instrumentor subclasses BaseInstrumentor from opentelemetry-instrumentation and, on _instrument(), wraps the stdlib’s logging.getLogRecordFactory() with a closure that stamps trace/span identifiers from get_current_span() onto each new LogRecord before restoring the original factory on _uninstrument(); a separate _setup_logging_handler() (in handler.py) optionally attaches a handler that forwards records to the configured LoggerProvider. Tech Stack — pure Python, depending only on opentelemetry-api, opentelemetry-instrumentation, and opentelemetry-semantic-conventions from the same monorepo, with no external runtime dependencies; built with hatchling and versioned in lockstep with the rest of the contrib release train. Code Quality — covered by three test files (test_handler.py, test_logging.py, test_logging_handler_recursion.py) totaling over 1,400 lines, including a dedicated regression test for handler recursion, plus a benchmarks/ directory measuring the logging handler’s overhead. API Design — a single LoggingInstrumentor().instrument(...) call with clearly named boolean flags (inject_trace_context, set_logging_format) keeps adoption to one line for the common case, while environment variables mirror every code-level option for container/config-driven deployments.
Used by 7 apps in this directory
Baserow
No Code Platforms · Databases
Open-source no-code platform to build databases, apps, automations, and AI agents — self-hosted or cloud, with full data ownership.
Helicone
Monitoring · AI Development · Analytics
An open-source AI gateway and LLM observability platform that routes requests to 100+ models while logging cost, latency, and full traces for every call.
highlight.io
Developer Tools · Analytics · Monitoring
Open-source full-stack monitoring that unifies session replay, error tracking, logging, and distributed tracing so you can stop context-switching between tools.
Keep
Devops · Automation · Monitoring
The open-source AIOps and alert management platform that unifies 130+ monitoring tools into a single pane of glass with AI-powered correlation, deduplication, and workflow automation.
Open WebUI
AI Assistants · AI Agents
The extensible, privacy-first AI platform that runs Ollama, OpenAI, and any LLM backend behind a polished, feature-packed web interface.
SurfSense
Search · AI Assistants
The open-source, unlimited NotebookLM alternative with real-time collaboration, a desktop app, and no vendor lock-in.
Uptrace
Monitoring · Devops
Unified open-source APM that collects OpenTelemetry traces, metrics, and logs into a single self-hosted platform backed by ClickHouse.