Crate otlp_stdout_span_exporter

Crate otlp_stdout_span_exporter 

Source
Expand description

A span exporter that writes OpenTelemetry spans to stdout in OTLP format.

This crate provides an implementation of OpenTelemetry’s SpanExporter that writes spans to stdout in OTLP (OpenTelemetry Protocol) format. It 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
  • Supports writing to stdout or named pipe
  • Consistent JSON output format

§Example

use opentelemetry::global;
use opentelemetry::trace::Tracer;
use opentelemetry_sdk::{trace::SdkTracerProvider, Resource};
use otlp_stdout_span_exporter::OtlpStdoutSpanExporter;

#[tokio::main]
async fn main() {
    // Create a new stdout exporter with default configuration (stdout output)
    let exporter = OtlpStdoutSpanExporter::default();

    // Or create one that writes to a named pipe
    let pipe_exporter = OtlpStdoutSpanExporter::builder()
        .pipe(true)  // Will write to /tmp/otlp-stdout-span-exporter.pipe
        .build();

    // Create a new tracer provider with batch export
    let provider = SdkTracerProvider::builder()
        .with_batch_exporter(exporter)
        .build();

    // Register the provider with the OpenTelemetry global API
    global::set_tracer_provider(provider.clone());

    // Create a tracer
    let tracer = global::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...");
        });
    });
     
    // Flush the provider to ensure all spans are exported
    if let Err(err) = provider.force_flush() {
        println!("Error flushing provider: {:?}", err);
    }
}

§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 if conflicting with OTEL_EXPORTER_OTLP_HEADERS)
  • OTLP_STDOUT_SPAN_EXPORTER_COMPRESSION_LEVEL: GZIP compression level (0-9, default: 6)
  • OTLP_STDOUT_SPAN_EXPORTER_OUTPUT_TYPE: Output type (“pipe” or “stdout”, default: “stdout”)

§Configuration Precedence

All configuration values follow this strict precedence order:

  1. Environment variables (highest precedence)
  2. Constructor parameters
  3. Default values (lowest precedence)

For example, when determining the output type:

use otlp_stdout_span_exporter::OtlpStdoutSpanExporter;

// This will use OTLP_STDOUT_SPAN_EXPORTER_OUTPUT_TYPE if set,
// otherwise it will write to a named pipe as specified in the constructor
let pipe_exporter = OtlpStdoutSpanExporter::builder()
    .pipe(true)
    .build();

// This will use the environment variable if set, or default to stdout
let default_exporter = OtlpStdoutSpanExporter::default();

§Output Format

The exporter writes each batch of spans as a JSON object to stdout or the named pipe:

{
  "__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
}

Modules§

consts
Constants used by the exporter.

Structs§

BufferOutput
An Output implementation that writes lines to an internal buffer.
ExporterOutput
Output format for the OTLP stdout exporter
OtlpStdoutSpanExporter
A span exporter that writes spans to stdout in OTLP format
OtlpStdoutSpanExporterBuilder
Use builder syntax to set the inputs and finish with build().

Enums§

LogLevel
Log level for the exported spans

Traits§

Output
Trait for output handling