@@ -217,7 +217,6 @@ use crate::trace::ExportResult;
217217/// .with_max_queue_size(1024) // Buffer up to 1024 spans.
218218/// .with_max_export_batch_size(256) // Export in batches of up to 256 spans.
219219/// .with_scheduled_delay(Duration::from_secs(5)) // Export every 5 seconds.
220- /// .with_max_export_timeout(Duration::from_secs(10)) // Timeout after 10 seconds.
221220/// .build(),
222221/// )
223222/// .build();
@@ -253,7 +252,38 @@ enum BatchMessage {
253252 SetResource ( Arc < Resource > ) ,
254253}
255254
256- /// A batch span processor with a dedicated background thread.
255+ /// The `BatchSpanProcessor` collects finished spans in a buffer and exports them
256+ /// in batches to the configured `SpanExporter`. This processor is ideal for
257+ /// high-throughput environments, as it minimizes the overhead of exporting spans
258+ /// individually. It uses a **dedicated background thread** to manage and export spans
259+ /// asynchronously, ensuring that the application's main execution flow is not blocked.
260+ ///
261+ /// This processor supports the following configurations:
262+ /// - **Queue size**: Maximum number of spans that can be buffered.
263+ /// - **Batch size**: Maximum number of spans to include in a single export.
264+ /// - **Scheduled delay**: Frequency at which the batch is exported.
265+ ///
266+ /// When using this processor with the OTLP Exporter, the following exporter
267+ /// features are supported:
268+ /// - `grpc-tonic`: Requires `TracerProvider` to be created within a tokio runtime.
269+ /// - `reqwest-blocking-client`: Works with a regular `main` or `tokio::main`.
270+ ///
271+ /// In other words, other clients like `reqwest` and `hyper` are not supported.
272+ ///
273+ /// `BatchSpanProcessor` buffers spans in memory and exports them in batches. An
274+ /// export is triggered when `max_export_batch_size` is reached or every
275+ /// `scheduled_delay` milliseconds. Users can explicitly trigger an export using
276+ /// the `force_flush` method. Shutdown also triggers an export of all buffered
277+ /// spans and is recommended to be called before the application exits to ensure
278+ /// all buffered spans are exported.
279+ ///
280+ /// **Warning**: When using tokio's current-thread runtime, `shutdown()`, which
281+ /// is a blocking call ,should not be called from your main thread. This can
282+ /// cause deadlock. Instead, call `shutdown()` from a separate thread or use
283+ /// tokio's `spawn_blocking`.
284+ ///
285+ /// [`shutdown()`]: crate::trace::TracerProvider::shutdown
286+ /// [`force_flush()`]: crate::trace::TracerProvider::force_flush
257287#[ derive( Debug ) ]
258288pub struct BatchSpanProcessor {
259289 span_sender : SyncSender < SpanData > , // Data channel to store spans
@@ -443,20 +473,14 @@ impl BatchSpanProcessor {
443473 }
444474
445475 let count_of_spans = spans. len ( ) ; // Count of spans that will be exported
446- let result = Self :: export_with_timeout_sync (
447- config. max_export_timeout ,
448- exporter,
449- spans,
450- last_export_time,
451- ) ; // This method clears the spans vec after exporting
476+ let result = Self :: export_batch_sync ( exporter, spans, last_export_time) ; // This method clears the spans vec after exporting
452477
453478 current_batch_size. fetch_sub ( count_of_spans, Ordering :: Relaxed ) ;
454479 result
455480 }
456481
457482 #[ allow( clippy:: vec_box) ]
458- fn export_with_timeout_sync < E > (
459- _: Duration , // TODO, enforcing timeout in exporter.
483+ fn export_batch_sync < E > (
460484 exporter : & mut E ,
461485 batch : & mut Vec < SpanData > ,
462486 last_export_time : & mut Instant ,
@@ -740,6 +764,7 @@ impl BatchConfigBuilder {
740764 /// Set max_export_timeout for [`BatchConfigBuilder`].
741765 /// It's the maximum duration to export a batch of data.
742766 /// The The default value is 30000 milliseconds.
767+ #[ cfg( feature = "experimental_trace_batch_span_processor_with_async_runtime" ) ]
743768 pub fn with_max_export_timeout ( mut self , max_export_timeout : Duration ) -> Self {
744769 self . max_export_timeout = max_export_timeout;
745770 self
@@ -960,10 +985,11 @@ mod tests {
960985 let batch = BatchConfigBuilder :: default ( )
961986 . with_max_export_batch_size ( 10 )
962987 . with_scheduled_delay ( Duration :: from_millis ( 10 ) )
963- . with_max_export_timeout ( Duration :: from_millis ( 10 ) )
964988 . with_max_queue_size ( 10 ) ;
965989 #[ cfg( feature = "experimental_trace_batch_span_processor_with_async_runtime" ) ]
966990 let batch = batch. with_max_concurrent_exports ( 10 ) ;
991+ #[ cfg( feature = "experimental_trace_batch_span_processor_with_async_runtime" ) ]
992+ let batch = batch. with_max_export_timeout ( Duration :: from_millis ( 10 ) ) ;
967993 let batch = batch. build ( ) ;
968994 assert_eq ! ( batch. max_export_batch_size, 10 ) ;
969995 assert_eq ! ( batch. scheduled_delay, Duration :: from_millis( 10 ) ) ;
@@ -1037,7 +1063,6 @@ mod tests {
10371063 . with_max_queue_size ( 10 )
10381064 . with_max_export_batch_size ( 10 )
10391065 . with_scheduled_delay ( Duration :: from_secs ( 5 ) )
1040- . with_max_export_timeout ( Duration :: from_secs ( 2 ) )
10411066 . build ( ) ;
10421067 let processor = BatchSpanProcessor :: new ( exporter, config) ;
10431068
@@ -1060,7 +1085,6 @@ mod tests {
10601085 . with_max_queue_size ( 10 )
10611086 . with_max_export_batch_size ( 10 )
10621087 . with_scheduled_delay ( Duration :: from_secs ( 5 ) )
1063- . with_max_export_timeout ( Duration :: from_secs ( 2 ) )
10641088 . build ( ) ;
10651089 let processor = BatchSpanProcessor :: new ( exporter, config) ;
10661090
@@ -1090,7 +1114,6 @@ mod tests {
10901114 . with_max_queue_size ( 10 )
10911115 . with_max_export_batch_size ( 10 )
10921116 . with_scheduled_delay ( Duration :: from_secs ( 5 ) )
1093- . with_max_export_timeout ( Duration :: from_secs ( 2 ) )
10941117 . build ( ) ;
10951118 let processor = BatchSpanProcessor :: new ( exporter, config) ;
10961119
@@ -1126,7 +1149,6 @@ mod tests {
11261149 let config = BatchConfigBuilder :: default ( )
11271150 . with_max_queue_size ( 2 ) // Small queue size to test span dropping
11281151 . with_scheduled_delay ( Duration :: from_secs ( 5 ) )
1129- . with_max_export_timeout ( Duration :: from_secs ( 2 ) )
11301152 . build ( ) ;
11311153 let processor = BatchSpanProcessor :: new ( exporter, config) ;
11321154
0 commit comments