OpenTelemetry span exporter that writes to stdout in OTLP format
Project description
OpenTelemetry Stdout Span Exporter
A Python span exporter that writes OpenTelemetry spans to stdout in OTLP format. Part of the serverless-otlp-forwarder project.
This exporter is particularly useful in serverless environments like AWS Lambda where writing to stdout is a common pattern for exporting telemetry data.
Features
- Uses OTLP Protobuf serialization for efficient encoding
- Applies GZIP compression with configurable levels
- Detects service name from environment variables
- Supports custom headers via environment variables
- Consistent JSON output format
Installation
pip install otlp-stdout-span-exporter
Usage
Basic usage:
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from otlp_stdout_span_exporter import OTLPStdoutSpanExporter
# Create and set the tracer provider
provider = TracerProvider()
trace.set_tracer_provider(provider)
# Create and register the exporter with optional GZIP compression level
exporter = OTLPStdoutSpanExporter(gzip_level=6)
provider.add_span_processor(BatchSpanProcessor(exporter))
# Your instrumentation code here
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("my-operation") as span:
span.set_attribute("my.attribute", "value")
For a more comprehensive example including nested spans, custom attributes, events, and environment variable configuration:
# Clone the repository
git clone https://github.com/dev7a/serverless-otlp-forwarder
cd serverless-otlp-forwarder/packages/python/otlp-stdout-span-exporter
# Install the package
pip install -e .
# Run the example
python examples/basic_usage.py
Environment Variables
The exporter respects the following environment variables:
OTEL_SERVICE_NAME: Service name to use in outputAWS_LAMBDA_FUNCTION_NAME: Fallback service name (ifOTEL_SERVICE_NAMEnot set)OTEL_EXPORTER_OTLP_HEADERS: Global headers for OTLP exportOTEL_EXPORTER_OTLP_TRACES_HEADERS: Trace-specific headers (takes precedence)
Output Format
The exporter writes each batch of spans as a JSON object to stdout:
{
"__otel_otlp_stdout": "0.1.0",
"source": "my-service",
"endpoint": "http://localhost:4318/v1/traces",
"method": "POST",
"content-type": "application/x-protobuf",
"content-encoding": "gzip",
"headers": {
"api-key": "secret123",
"custom-header": "value"
},
"payload": "<base64-encoded-gzipped-protobuf>",
"base64": true
}
Development
- Create a virtual environment:
uv venv && source .venv/bin/activate
- Install development dependencies:
uv pip install -e ".[dev]"
- Run tests:
pytest
- Run linting:
ruff check .
ruff format .
License
Apache License 2.0
See Also
- serverless-otlp-forwarder - The main project repository
- TypeScript Span Exporter - The TypeScript version of this exporter
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 otlp_stdout_span_exporter-0.1.2.tar.gz.
File metadata
- Download URL: otlp_stdout_span_exporter-0.1.2.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a0e41cf4b39750bda0ca8e3f54f4acbdc956c021e62f48ffda277e019140702
|
|
| MD5 |
b651c1186d2d4327f73b7f72a4833edc
|
|
| BLAKE2b-256 |
c1892daa4bb7bfaf3394c3f22c81ad0638dc5fa3f3e23413ecd324a914ffa7dc
|
File details
Details for the file otlp_stdout_span_exporter-0.1.2-py3-none-any.whl.
File metadata
- Download URL: otlp_stdout_span_exporter-0.1.2-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aaaea8415c39171bb7e8f0543d3e6ecd6001d0fe14893121b98440a1eb430c42
|
|
| MD5 |
c3c50e10b7d0e4ae0c81557edfa3486e
|
|
| BLAKE2b-256 |
9a0a812dbde45185b7761c02a8e3ada738eec71742f5e49f43404a23b4f344bc
|