Skip to main content
This guide shows how to switch the span processor used by your OpenTelemetry Python instrumentation. It explains the trade-offs between SimpleSpanProcessor and BatchSpanProcessor, provides sample code you can copy, and includes tips to ensure spans are flushed at process exit. At a high level:
  • SimpleSpanProcessor exports each span synchronously when it ends — convenient for local debugging but adds latency and should not be used in production.
  • BatchSpanProcessor buffers spans and exports them from a background worker thread in batches — recommended for production to minimize impact on application latency.
Quick comparison
ProcessorBehaviorUse case
SimpleSpanProcessorExports each span synchronously when it endsLocal development, debugging
BatchSpanProcessorBuffers spans and exports in background batchesProduction (low-latency deployments)
Keywords: OpenTelemetry, span processor, BatchSpanProcessor, SimpleSpanProcessor, Python instrumentation, exporter, OTLP

Example: SimpleSpanProcessor (local testing)

Keep this minimal configuration for local debugging. It uses the ConsoleSpanExporter and SimpleSpanProcessor. Do not use this in production because exporting occurs on the same thread as your application code.
Explanation:
  • SimpleSpanProcessor synchronously invokes the exporter when each span ends, which can block the application thread and increase latency.
Use SimpleSpanProcessor only for local development or interactive debugging. For production deployments, prefer BatchSpanProcessor to avoid adding latency to your application’s main threads.

Switching to BatchSpanProcessor (production)

BatchSpanProcessor aggregates spans and exports them on a schedule from a separate worker thread. To switch, replace SimpleSpanProcessor with BatchSpanProcessor and pass the exporter instance to its constructor.
If you call BatchSpanProcessor() without passing an exporter you will see:
Always pass the exporter instance (for example, ConsoleSpanExporter() or an OTLP exporter).
Notes on behavior and best practices:
  • BatchSpanProcessor reduces application-thread latency by exporting spans on a worker thread in batches.
  • Because batching is asynchronous, console output or exported spans may appear slightly delayed compared to SimpleSpanProcessor.
  • Always call trace.get_tracer_provider().shutdown() (or otherwise shut down the provider) at process termination to flush any remaining spans.
  • For production, prefer an exporter suited to your backend (for example, OTLP exporter). See the References section below for exporter options and configuration details.

Sample console output (ConsoleSpanExporter)

Below is a representative example of a span exported by ConsoleSpanExporter (formatted JSON):
Summary
  • Use SimpleSpanProcessor for quick local debugging only.
  • Use BatchSpanProcessor in production to reduce the latency impact on your main application threads.
  • Always provide an exporter instance to BatchSpanProcessor and ensure the tracer provider is shut down at application exit to flush spans.

Watch Video