SpanProcessor Interface
Fromopentelemetry-sdk/src/trace/span_processor.rs:74-136:
Built-in Processors
OpenTelemetry SDK provides two main span processors:SimpleSpanProcessor
Exports spans synchronously as soon as they end (defined inopentelemetry-sdk/src/trace/span_processor.rs:138-212).
Use cases:
- Debugging and testing
- Low-throughput applications
- When you need immediate export
- Exports each span individually
- Blocks the thread that ends the span
- No batching overhead
- Higher export overhead per span
Exporter Compatibility
When using OTLP exporters withSimpleSpanProcessor:
grpc-tonic- RequiresTracerProvidercreated within a tokio runtime. Spans can be emitted from any thread.reqwest-blocking-client-TracerProvidermay be created anywhere, but spans must be emitted from non-tokio threads.reqwest-client-TracerProvidermay be created anywhere, but spans must be emitted from tokio runtime threads.
BatchSpanProcessor
Collects finished spans and exports them in batches (defined inopentelemetry-sdk/src/trace/span_processor.rs:214-300).
Use cases:
- Production applications
- High-throughput scenarios
- When minimizing export overhead is important
- Uses a dedicated background thread
- Batches spans before export
- Configurable queue and batch sizes
- Scheduled periodic exports
- Lower per-span overhead
Exporter Compatibility
When using OTLP exporters withBatchSpanProcessor:
grpc-tonic- RequiresTracerProvidercreated within a tokio runtimereqwest-blocking-client- Works with regularmainortokio::mainreqwest-clientandhyper- Not supported withBatchSpanProcessor
Batch Configuration
CustomizeBatchSpanProcessor behavior:
BatchConfig Options
Fromopentelemetry-sdk/src/trace/span_processor.rs:52-72:
Configuring Batch Processor
Environment Variables
Configure batch processor via environment variables:Complete Example
From the documentation inopentelemetry-sdk/src/trace/span_processor.rs:227-272:
Custom Span Processors
Implement custom processing logic:Filtering Processor
Filter spans before export:Multiple Processors
Register multiple processors for different purposes:Processors are invoked in the order they were registered.
Context in Processors
Fromopentelemetry-sdk/src/trace/span_processor.rs:87-118:
Shutdown and Force Flush
Graceful Shutdown
Always shutdown the provider before application exit:Force Flush
Flush pending spans without shutting down:Best Practices
Use BatchSpanProcessor in production
Use BatchSpanProcessor in production
BatchSpanProcessor minimizes export overhead and prevents blocking application threads. Use SimpleSpanProcessor only for debugging.
Configure appropriate batch sizes
Configure appropriate batch sizes
Balance between memory usage (queue size) and export frequency. Larger batches reduce overhead but increase memory usage and latency.
Always shutdown gracefully
Always shutdown gracefully
Call
shutdown() before application exit to ensure all spans are exported. Use sufficient timeout for batch processors to flush.Extract context in on_start
Extract context in on_start
Never rely on
Context::current() in on_end. Extract needed information in on_start and store as span attributes.Monitor queue sizes
Monitor queue sizes
In high-throughput scenarios, monitor dropped span counts. Increase
max_queue_size if spans are being dropped.Tuning for High Throughput
For applications generating many spans:Complete Production Example
Next Steps
Sampling
Control which spans are recorded
Overview
Return to tracing overview