Rust OTLP Stdout Span Exporter
A Rust 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 some metadata about the spans, such as the service name, the OTLP endpoint, and the HTTP method:
Outputting the telemetry data in this format directly to stdout makes the library easily usable in network constrained environments, or in enviroments that are particularly sensitive to the overhead of HTTP connections, such as AWS Lambda.
Part of the serverless-otlp-forwarder project, that implements a forwarder for OTLP telemetry data from serverless environments to OTLP compliant collectors.
Features
- Uses OTLP Protobuf serialization for efficient encoding
- Applies GZIP compression with configurable levels
- Detects service name from environment variables or AWS Lambda function name
- Supports custom headers via standard OTEL environment variables
- Supports writing to stdout or named pipe
- Consistent JSON output format
- Zero external HTTP dependencies
- Lightweight and fast
Installation
Run cargo add otlp-stdout-span-exporter to add the crate to your project.
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 crate, with the LambdaSpanProcessor strategy, which is particularly optimized for AWS Lambda.
You can create a simple tracer provider with the default configuration:
use global;
use Tracer;
use SdkTracerProvider;
use OtlpStdoutSpanExporter;
async
This setup ensures that:
- Spans are batched together for efficient export
- Parent-child relationships are preserved
- System resources are used efficiently
- Spans are properly flushed on shutdown
Environment Variables
The exporter respects the following environment variables:
OTEL_SERVICE_NAME: Service name to use in output, used in thesourcefieldAWS_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 (takes precedence if conflicting withOTEL_EXPORTER_OTLP_HEADERS)OTLP_STDOUT_SPAN_EXPORTER_COMPRESSION_LEVEL: GZIP compression level (0-9, default: 6)OTLP_STDOUT_SPAN_EXPORTER_LOG_LEVEL: Log level for filtering (debug, info, warn, error)OTLP_STDOUT_SPAN_EXPORTER_OUTPUT_TYPE: Output type ("pipe" or "stdout", default: "stdout")
Configuration
The exporter can be configured in multiple ways, with a strict precedence order:
- Environment variables (highest precedence)
- Builder methods (medium precedence)
- Default values (lowest precedence)
Using Environment Variables
Environment variables always take precedence over any programmatic configuration:
# Set GZIP compression level to 9 (maximum compression)
# Write to a named pipe instead of stdout
Using default or builder methods
The exporter provides two main ways to create and configure it:
use ;
// Create with default options (compression level 6, stdout output)
let default_exporter = default;
// Create with specific compression level
let max_compression_exporter = builder
.compression_level
.build;
// Create with a specific log level
let debug_level_exporter = builder
.level
.build;
// Create with pipe output
let pipe_exporter = builder
.pipe // Will write to /tmp/otlp-stdout-span-exporter.pipe
.build;
// Create with multiple options
let configured_exporter = builder
.compression_level
.level
.pipe
.build;
Note that even when using these constructor parameters, environment variables will still take precedence if they are set.
Default Values
When neither environment variables nor constructor parameters are provided, the following defaults are used:
- Compression level: 6 (good balance between speed and compression)
- Service name: "unknown-service" (unless AWS_LAMBDA_FUNCTION_NAME is available)
- Endpoint: "http://localhost:4318/v1/traces"
- Output type: stdout
- Log level: None (no filtering)
Development
- Clone the repository:
- Run tests:
- Run the example:
License
Apache License 2.0
See Also
- serverless-otlp-forwarder - The main project repository
- Python OTLP Stdout Span Exporter - The Python version of this exporter
- TypeScript OTLP Stdout Span Exporter - The TypeScript version of this exporter