2 releases
new 0.1.1 | Jan 16, 2025 |
---|---|
0.1.0 | Jan 16, 2025 |
#444 in Debugging
21 downloads per month
26KB
278 lines
otlp-stdout-span-exporter
A Rust 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
- Zero external HTTP dependencies
- Lightweight and fast
Installation
Add this to your Cargo.toml
:
[dependencies]
otlp-stdout-span-exporter = "0.1.0"
Usage
The recommended way to use this exporter is with batch export, which provides better performance by buffering and exporting spans in batches:
use opentelemetry::{trace::{Tracer, TracerProvider}, KeyValue};
use opentelemetry_sdk::{trace::TracerProvider as SdkTracerProvider, Resource, runtime};
use otlp_stdout_span_exporter::OtlpStdoutSpanExporter;
#[tokio::main]
async fn main() {
// Create a new stdout exporter
let exporter = OtlpStdoutSpanExporter::new();
// Create a new tracer provider with batch export
let provider = SdkTracerProvider::builder()
.with_batch_exporter(exporter, runtime::Tokio)
.with_resource(Resource::new(vec![KeyValue::new("service.name", "my-service")]))
.build();
// Create a tracer
let tracer = provider.tracer("my-service");
// Create spans
tracer.in_span("parent-operation", |_cx| {
println!("Doing work...");
// Create nested spans
tracer.in_span("child-operation", |_cx| {
println!("Doing more work...");
});
});
// Shut down the provider
let _ = provider.shutdown();
}
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 outputAWS_LAMBDA_FUNCTION_NAME
: Fallback service name (ifOTEL_SERVICE_NAME
not set)OTEL_EXPORTER_OTLP_HEADERS
: Global headers for OTLP exportOTEL_EXPORTER_OTLP_TRACES_HEADERS
: Trace-specific headers (takes precedence)
Header format examples:
# Single header
export OTEL_EXPORTER_OTLP_HEADERS="api-key=secret123"
# Multiple headers
export OTEL_EXPORTER_OTLP_HEADERS="api-key=secret123,custom-header=value"
# Headers with special characters
export OTEL_EXPORTER_OTLP_HEADERS="authorization=Basic dXNlcjpwYXNzd29yZA=="
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
}
Configuration
The exporter can be configured with different GZIP compression levels:
// Create exporter with custom GZIP level (0-9)
let exporter = OtlpStdoutSpanExporter::with_gzip_level(9);
Development
- Clone the repository:
git clone https://github.com/dev7a/serverless-otlp-forwarder
cd serverless-otlp-forwarder/packages/rust/otlp-stdout-span-exporter
- Run tests:
cargo test
- Run the example:
cargo run --example hello
License
Apache License 2.0
See Also
- serverless-otlp-forwarder - The main project repository
- Python Span Exporter - The Python version of this exporter
- TypeScript Span Exporter - The TypeScript version of this exporter
Dependencies
~8–16MB
~194K SLoC