OpenTelemetry span exporter that writes to stdout in OTLP format
Project description
OTLP Stdout Span Exporter for Python
A Python span exporter that writes OpenTelemetry spans to stdout, using a custom serialization format that embeds the spans serialized as OTLP protobuf in the payload field. The message envelope carries metadata about the spans, such as the service name, the OTLP endpoint, and the HTTP method:
{
"__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": {
"tenant-id": "tenant-12345",
"custom-header": "value"
},
"payload": "<base64-encoded-gzipped-protobuf>",
"base64": true
}
Outputting telemetry data in this format directly to stdout makes the library easily usable in network constrained environments, or in environments that are particularly sensitive to the overhead of HTTP connections, such as AWS Lambda.
[!IMPORTANT] This package is part of the serverless-otlp-forwarder project and is designed for AWS Lambda environments. While it can be used in other contexts, it's primarily tested with AWS Lambda.
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
- Zero external HTTP dependencies
- Lightweight and fast
Installation
pip install otlp-stdout-span-exporter
Usage
The recommended way to use this exporter is with the standard OpenTelemetry BatchSpanProcessor, which provides better performance by buffering and exporting spans in batches, or, in conjunction with the lambda-otel-lite package, with the LambdaSpanProcessor, which is particularly optimized for AWS Lambda.
You can create a simple tracer provider with the BatchSpanProcessor and the OTLPStdoutSpanExporter:
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")
Configuration
Constructor Options
OTLPStdoutSpanExporter(
# GZIP compression level (0-9, where 0 is no compression and 9 is maximum compression)
# Will be overridden by environment variable if set
gzip_level=9
)
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: Headers for OTLP export, used in theheadersfieldOTEL_EXPORTER_OTLP_TRACES_HEADERS: Trace-specific headers (which take precedence if conflicting withOTEL_EXPORTER_OTLP_HEADERS)OTLP_STDOUT_SPAN_EXPORTER_COMPRESSION_LEVEL: GZIP compression level (0-9). Defaults to 6.
[!IMPORTANT] Environment variables always take precedence over constructor parameters. If both are specified, the environment variable value will be used.
[!NOTE] For security best practices, avoid including authentication credentials or sensitive information in headers. The serverless-otlp-forwarder infrastructure is designed to handle authentication at the destination, rather than embedding credentials in your telemetry data.
Output Format
The exporter writes JSON objects to stdout with the following structure:
{
"__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": {
"tenant-id": "tenant-12345",
"custom-header": "value"
},
"base64": true,
"payload": "<base64-encoded-gzipped-protobuf>"
}
__otel_otlp_stdoutis a marker to identify the output of this exporter.sourceis the emittingservice name.endpointis the OTLP endpoint (defaults tohttp://localhost:4318/v1/tracesand just indicates the signal type. The actual endpoint is determined by the process that forwards the data).methodis the HTTP method (alwaysPOST).content-typeis the content type (alwaysapplication/x-protobuf).content-encodingis the content encoding (alwaysgzip).headersis the headers defined in theOTEL_EXPORTER_OTLP_HEADERSandOTEL_EXPORTER_OTLP_TRACES_HEADERSenvironment variables.payloadis the base64-encoded, gzipped, Protobuf-serialized span data in OTLP format.base64is a boolean flag to indicate if the payload is base64-encoded (alwaystrue).
License
MIT
See Also
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.11.0.tar.gz.
File metadata
- Download URL: otlp_stdout_span_exporter-0.11.0.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ebbd6bb1162fb08c1f4692c3c7026ec61c58e8997872c2351c07fdbee1210a01
|
|
| MD5 |
4aa9b3f67f6be56c7df6274c771d9a6e
|
|
| BLAKE2b-256 |
a866595fea450540707cc57889342b5b73057cc19d982570bc179b0f40342396
|
File details
Details for the file otlp_stdout_span_exporter-0.11.0-py3-none-any.whl.
File metadata
- Download URL: otlp_stdout_span_exporter-0.11.0-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f03fcceb6405502c44b787d587376cdf5f8bf04fa959ce806d1b5b3f3ac75136
|
|
| MD5 |
49e2cafe7a4cf260443b0d99dbbcdcf8
|
|
| BLAKE2b-256 |
0b6793f9c8adc8e59cefec3d5acdc051141bdea21935723329b9af86a299af8b
|