Skip to main content

OpenTelemetry log handler and tracing plugin for simple_log_factory

Project description

simple-log-factory-ext-otel

OpenTelemetry log handler and tracing plugin for simple_log_factory.

Ship your log messages and traces to any OpenTelemetry-compatible backend (Tempo, Jaeger, Grafana Cloud, SigNoz, etc.) without changing any existing logging code.

Installation

pip install simple-log-factory-ext-otel

Or with UV:

uv add simple-log-factory-ext-otel

Quick Start

Pattern 1 — Logs Only

from simple_log_factory import log_factory
from simple_log_factory_ext_otel import OtelLogHandler

# 1. Initialize the handler
otel_handler = OtelLogHandler(
    service_name="my-service",
    endpoint="http://localhost:4317",
)

# 2. Pass it to log_factory — done
logger = log_factory(
    __name__,
    custom_handlers=[otel_handler],
)

logger.info("This goes to console AND to your OTel backend")

# 3. Clean shutdown (optional but recommended)
otel_handler.shutdown()

Pattern 2 — Logs + Tracing with setup_otel()

from simple_log_factory import log_factory
from simple_log_factory_ext_otel import setup_otel, TracedLogger

# 1. One-call setup — creates both log handler and tracer
#    with a shared Resource; registers TracerProvider globally
handler, otel_tracer = setup_otel(
    service_name="my-service",
    endpoint="http://localhost:4317",
)

# 2. Create a logger with the OTel handler
logger = log_factory(__name__, custom_handlers=[handler])

# 3. Wrap with TracedLogger for span support
traced = TracedLogger(logger=logger, tracer=otel_tracer.tracer)

# 4. Use span() to create correlated spans
with traced.span("process-order", attributes={"order.id": "123"}):
    traced.info("Processing order")   # auto-correlated with the span
    # ... your business logic ...

# 5. Or use @trace() as a decorator
@traced.trace("fetch-user")
def fetch_user(user_id: int) -> dict:
    traced.info("Fetching user %d", user_id)
    return {"id": user_id, "name": "Alice"}

fetch_user(42)

Configuration

OtelLogHandler Parameters

Parameter Type Default Description
service_name str (required) Logical name of the service emitting logs
endpoint str http://localhost:4317 OTLP receiver endpoint
protocol str "grpc" Transport protocol — "grpc" or "http"
insecure bool True Use plaintext (insecure) connection
headers Dict[str, str] None Metadata headers sent with every export request
resource_attributes Dict[str, str] None Extra OTel Resource attributes
log_level int logging.NOTSET Minimum severity forwarded to the OTel pipeline
export_timeout_millis int 30000 Timeout in ms for each export batch

Using HTTP Instead of gRPC

from simple_log_factory_ext_otel import OtelLogHandler

handler = OtelLogHandler(
    service_name="my-service",
    endpoint="http://localhost:4318/v1/logs",
    protocol="http",
)

Custom Resource Attributes

from simple_log_factory_ext_otel import OtelLogHandler

handler = OtelLogHandler(
    service_name="my-service",
    resource_attributes={
        "deployment.environment": "production",
        "service.version": "1.2.3",
    },
)

Authenticated Endpoints

from simple_log_factory_ext_otel import OtelLogHandler

handler = OtelLogHandler(
    service_name="my-service",
    endpoint="https://otel.example.com:4317",
    insecure=False,
    headers={"Authorization": "Bearer <token>"},
)

Level Filtering

Only export warnings and above to your OTel backend:

import logging

from simple_log_factory_ext_otel import OtelLogHandler

handler = OtelLogHandler(
    service_name="my-service",
    log_level=logging.WARNING,
)

TracedLogger API

TracedLogger wraps a standard logging.Logger and an OTel Tracer:

  • span(name, attributes) — context manager that creates an OTel span. Logs inside the block are auto-correlated.
  • trace(name, attributes) — decorator that wraps a function in a span. Exceptions are recorded on the span and re-raised.
  • debug/info/warning/error/exception/critical/log — proxy to the underlying logger.
  • logger / tracer properties — escape hatches for direct access.

setup_otel() Parameters

Parameter Type Default Description
service_name str (required) Logical name of the service
endpoint str http://localhost:4317 OTLP receiver endpoint
protocol str "grpc" Transport protocol — "grpc" or "http"
insecure bool True Use plaintext (insecure) connection
headers Dict[str, str] None Metadata headers sent with every export request
resource_attributes Dict[str, str] None Extra OTel Resource attributes
export_timeout_millis int 30000 Timeout in ms for each export batch
log_level int logging.NOTSET Minimum severity forwarded to the OTel pipeline

Returns a (OtelLogHandler, OtelTracer) tuple. The TracerProvider is registered globally so auto-instrumentation libraries (FastAPI, psycopg2, etc.) share the same provider.

How It Works

OtelLogHandler extends logging.Handler directly (not OTel's LoggingHandler) and internally composes the OTel pipeline:

Your code
  └─► log_factory (attaches handler, sets formatter & level)
        └─► OtelLogHandler.emit(record)
              └─► Internal OTel LoggingHandler (LogRecord → OTel translation)
                    └─► BatchLogRecordProcessor
                          └─► OTLPLogExporter (gRPC or HTTP)
                                └─► OTel Collector / Backend

This composition pattern is critical: log_factory calls setFormatter() and setLevel() on every handler it receives. If we inherited from OTel's LoggingHandler, the factory's formatter would overwrite OTel's internal translation. By wrapping it, both pipelines stay independent.

Trace Context Correlation

When using setup_otel() or sharing a Resource between OtelLogHandler and OtelTracer, span and trace IDs are automatically attached to log records emitted inside active spans. No extra configuration needed.

Lifecycle Management

The handler registers an atexit hook to flush and shut down on interpreter exit. For explicit control:

# Force-flush buffered records
otel_handler.flush()

# Graceful shutdown (idempotent, safe to call multiple times)
otel_handler.shutdown()

Local Development

Create a virtual environment with UV:

uv venv
uv pip install -e ".[dev]"

Run tests:

pytest --cov=simple_log_factory_ext_otel --cov-report=term-missing

Run linters:

black --check .
isort --check .
ruff check .
mypy simple_log_factory_ext_otel

License

GPL-3.0 — see LICENSE.md for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple_log_factory_ext_otel-1.1.0.tar.gz (29.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_log_factory_ext_otel-1.1.0-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file simple_log_factory_ext_otel-1.1.0.tar.gz.

File metadata

File hashes

Hashes for simple_log_factory_ext_otel-1.1.0.tar.gz
Algorithm Hash digest
SHA256 b92fd6230db036b12e5e32a13458dcd603c9ac6d951e90744cf3ea6a8d843fd7
MD5 af6bf03da04131e48d6b80fb31e1bf4a
BLAKE2b-256 b4b62c67978d0d5da2657b58b14d01744b7f7ebc7a9bbfd833e61131ddc36323

See more details on using hashes here.

Provenance

The following attestation bundles were made for simple_log_factory_ext_otel-1.1.0.tar.gz:

Publisher: publish.yml on brenordv/log-factory-package-ext-otel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file simple_log_factory_ext_otel-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_log_factory_ext_otel-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 27c15512c164123e842906707012081626841eb04203186f9dc7e6e266d50a8b
MD5 066d450e38065d4661a4e6ab28450c53
BLAKE2b-256 874046e5794333cbfae74b8cbd90d59f2161c139f0d5189d16b2300aa17a3a03

See more details on using hashes here.

Provenance

The following attestation bundles were made for simple_log_factory_ext_otel-1.1.0-py3-none-any.whl:

Publisher: publish.yml on brenordv/log-factory-package-ext-otel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page