Skip to main content

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 output
  • AWS_LAMBDA_FUNCTION_NAME: Fallback service name (if OTEL_SERVICE_NAME not set)
  • OTEL_EXPORTER_OTLP_HEADERS: Global headers for OTLP export
  • OTEL_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

  1. Create a virtual environment:
uv venv && source .venv/bin/activate
  1. Install development dependencies:
uv pip install -e ".[dev]"
  1. Run tests:
pytest
  1. Run linting:
ruff check .
ruff format .

License

Apache License 2.0

See Also

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

otlp_stdout_span_exporter-0.1.2.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

otlp_stdout_span_exporter-0.1.2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file otlp_stdout_span_exporter-0.1.2.tar.gz.

File metadata

File hashes

Hashes for otlp_stdout_span_exporter-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9a0e41cf4b39750bda0ca8e3f54f4acbdc956c021e62f48ffda277e019140702
MD5 b651c1186d2d4327f73b7f72a4833edc
BLAKE2b-256 c1892daa4bb7bfaf3394c3f22c81ad0638dc5fa3f3e23413ecd324a914ffa7dc

See more details on using hashes here.

File details

Details for the file otlp_stdout_span_exporter-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for otlp_stdout_span_exporter-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 aaaea8415c39171bb7e8f0543d3e6ecd6001d0fe14893121b98440a1eb430c42
MD5 c3c50e10b7d0e4ae0c81557edfa3486e
BLAKE2b-256 9a0a812dbde45185b7761c02a8e3ada738eec71742f5e49f43404a23b4f344bc

See more details on using hashes here.

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