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
- Python >= 3.11
- AWS Lambda with the OpenTelemetry Lambda Layer attached (for trace context)
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file trace0_lambda_otel_logger-1.0.3.tar.gz.
File metadata
- Download URL: trace0_lambda_otel_logger-1.0.3.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab49c9a77adfd954134ae90aac3e2dfbb3311cd3ddcbb5e8d70245b39e9ba156
|
|
| MD5 |
839955910db1e92bd01a446f69a95919
|
|
| BLAKE2b-256 |
a1c4b1e67cda02c635aa47cf90401fedd6b374ef1457ea2d2f35dcca73beb89e
|
Provenance
The following attestation bundles were made for trace0_lambda_otel_logger-1.0.3.tar.gz:
Publisher:
publish.yml on Trace0-HQ/trace0-lambda-python-otel-logger
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
trace0_lambda_otel_logger-1.0.3.tar.gz -
Subject digest:
ab49c9a77adfd954134ae90aac3e2dfbb3311cd3ddcbb5e8d70245b39e9ba156 - Sigstore transparency entry: 1404824084
- Sigstore integration time:
-
Permalink:
Trace0-HQ/trace0-lambda-python-otel-logger@da51a04d9725dffc0779056e6a91f1a05a5ceb69 -
Branch / Tag:
refs/tags/1.0.3 - Owner: https://github.com/Trace0-HQ
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@da51a04d9725dffc0779056e6a91f1a05a5ceb69 -
Trigger Event:
release
-
Statement type:
File details
Details for the file trace0_lambda_otel_logger-1.0.3-py3-none-any.whl.
File metadata
- Download URL: trace0_lambda_otel_logger-1.0.3-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f43b0385ad940334a09a8b3d533d31ee2d6eaaa8c2f3cecf285989484381914d
|
|
| MD5 |
40094f3fe0b51f4f422964d22ae3a0dd
|
|
| BLAKE2b-256 |
44328748a5217b12351230d49f72896362e9921818f2c8db88ea5460ff07f3b4
|
Provenance
The following attestation bundles were made for trace0_lambda_otel_logger-1.0.3-py3-none-any.whl:
Publisher:
publish.yml on Trace0-HQ/trace0-lambda-python-otel-logger
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
trace0_lambda_otel_logger-1.0.3-py3-none-any.whl -
Subject digest:
f43b0385ad940334a09a8b3d533d31ee2d6eaaa8c2f3cecf285989484381914d - Sigstore transparency entry: 1404824388
- Sigstore integration time:
-
Permalink:
Trace0-HQ/trace0-lambda-python-otel-logger@da51a04d9725dffc0779056e6a91f1a05a5ceb69 -
Branch / Tag:
refs/tags/1.0.3 - Owner: https://github.com/Trace0-HQ
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@da51a04d9725dffc0779056e6a91f1a05a5ceb69 -
Trigger Event:
release
-
Statement type: