Skip to main content

OpenTelemetry-based logger for AWS Lambda Python functions

Project description

trace0-lambda-otel-logger

OpenTelemetry-based logger for AWS Lambda Python functions.

Automatically captures all logging output, injects OTel trace context (traceId, spanId), and exports logs as OTLP JSON to the Trace0 ingest endpoint.

See the Background section for more details on why this library is needed.

How it works

logging.info() 
  → Trace0LogHandler (logging.Handler bridge)
    → BatchLogProcessor (buffers during invocation)
      → OTLPJsonLogExporter (POST to Trace0 ingest endpoint)

Based on the OpenTelemetry Logs API specification.

Installation

pip install trace0-lambda-otel-logger

Usage

1. Add as the first import in your Lambda handler module:

import trace0_lambda_otel_logger  # must be first
from trace0_lambda_otel_logger import flush

2. Call flush() at the end of every invocation:

import logging

logger = logging.getLogger(__name__)

def handler(event, context):
    try:
        return your_handler(event, context)
    finally:
        flush()  # always flush before Lambda freezes

3. Set environment variables:

OTEL_EXPORTER_OTLP_ENDPOINT=https://app.trace0hq.com/api
OTEL_EXPORTER_OTLP_HEADERS=X-API-KEY=YOUR_TRACE0_ENV_API_KEY

That's it. No changes to your existing logging calls required.

Environment Variables

Variable Required Default Description
OTEL_EXPORTER_OTLP_ENDPOINT Yes Your Trace0 ingest endpoint
OTEL_EXPORTER_OTLP_HEADERS Yes Your Trace0 API key

Log Record Format

Each log record is exported as OTLP JSON with the following fields:

{
  "timeUnixNano": "1234567890000000000",
  "severityNumber": 9,
  "severityText": "INFO",
  "body": { "stringValue": "User created" },
  "attributes": [
    { "key": "logger.name", "value": { "stringValue": "app.users" } },
    { "key": "code.function", "value": { "stringValue": "create_user" } },
    { "key": "code.lineno", "value": { "intValue": 42 } }
  ],
  "traceId": "abc123...",
  "spanId": "def456..."
}

Requirements

Background

To correlate logs with traces in Trace0, this library must be installed in your Lambda function. This is necessary because the OpenTelemetry Python Logs SDK is currently in an experimental state. The APIs within opentelemetry.sdk._logs are subject to change in minor/patch releases and make no backward compatibility guarantees at this time.

trace0-lambda-otel-logger solves this gap by installing a logging.Handler on the root logger that automatically injects the active OTel span's traceId and spanId into every log record, and exporting them directly to the Trace0 ingest endpoint. This enables full log-trace correlation with minimal integration effort.

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

trace0_lambda_otel_logger-1.0.4.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

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

trace0_lambda_otel_logger-1.0.4-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file trace0_lambda_otel_logger-1.0.4.tar.gz.

File metadata

File hashes

Hashes for trace0_lambda_otel_logger-1.0.4.tar.gz
Algorithm Hash digest
SHA256 d7cfb289bdde2912a1bd56b5d4ff43f0718d313832db6c6dfca96593066c1aa1
MD5 274ac7becf4b5dde329b0e0b5fbece19
BLAKE2b-256 009be46b11a34aaebe71eab9cd72d63b9156e4e457888e811ac5728f152e5133

See more details on using hashes here.

Provenance

The following attestation bundles were made for trace0_lambda_otel_logger-1.0.4.tar.gz:

Publisher: publish.yml on Trace0-HQ/trace0-lambda-python-otel-logger

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

File details

Details for the file trace0_lambda_otel_logger-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for trace0_lambda_otel_logger-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2688242687c90af52876ac3e9849863468ad570be0972bb2a96a21f7fcba1d6b
MD5 f456f708b0d59bcf9197c80ec927c319
BLAKE2b-256 d43b8ebf38effb95c65f56a86af15ad8fce1fb7b9eb63dff7b0c2db9399ed280

See more details on using hashes here.

Provenance

The following attestation bundles were made for trace0_lambda_otel_logger-1.0.4-py3-none-any.whl:

Publisher: publish.yml on Trace0-HQ/trace0-lambda-python-otel-logger

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