@@ -181,6 +181,7 @@ enum BatchMessage {
181181pub struct BatchSpanProcessor {
182182 message_sender : SyncSender < BatchMessage > ,
183183 handle : Mutex < Option < thread:: JoinHandle < ( ) > > > ,
184+ forceflush_timeout : Duration ,
184185 shutdown_timeout : Duration ,
185186 is_shutdown : AtomicBool ,
186187 dropped_span_count : Arc < AtomicUsize > ,
@@ -190,14 +191,15 @@ impl BatchSpanProcessor {
190191 /// Creates a new instance of `BatchSpanProcessor`.
191192 pub fn new < E > (
192193 mut exporter : E ,
193- max_queue_size : usize ,
194- scheduled_delay : Duration ,
195- shutdown_timeout : Duration ,
194+ config : BatchConfig ,
195+ //ax_queue_size: usize,
196+ //scheduled_delay: Duration,
197+ //shutdown_timeout: Duration,
196198 ) -> Self
197199 where
198200 E : SpanExporter + Send + ' static ,
199201 {
200- let ( message_sender, message_receiver) = sync_channel ( max_queue_size) ;
202+ let ( message_sender, message_receiver) = sync_channel ( config . max_queue_size ) ;
201203
202204 let handle = thread:: Builder :: new ( )
203205 . name ( "BatchSpanProcessorDedicatedThread" . to_string ( ) )
@@ -206,13 +208,15 @@ impl BatchSpanProcessor {
206208 let mut last_export_time = Instant :: now ( ) ;
207209
208210 loop {
209- let timeout = scheduled_delay. saturating_sub ( last_export_time. elapsed ( ) ) ;
211+ let timeout = config
212+ . scheduled_delay
213+ . saturating_sub ( last_export_time. elapsed ( ) ) ;
210214 match message_receiver. recv_timeout ( timeout) {
211215 Ok ( message) => match message {
212216 BatchMessage :: ExportSpan ( span) => {
213217 spans. push ( span) ;
214- if spans. len ( ) >= max_queue_size
215- || last_export_time. elapsed ( ) >= scheduled_delay
218+ if spans. len ( ) >= config . max_queue_size
219+ || last_export_time. elapsed ( ) >= config . scheduled_delay
216220 {
217221 if let Err ( err) = block_on ( exporter. export ( spans. split_off ( 0 ) ) )
218222 {
@@ -232,7 +236,7 @@ impl BatchSpanProcessor {
232236 }
233237 } ,
234238 Err ( RecvTimeoutError :: Timeout ) => {
235- if last_export_time. elapsed ( ) >= scheduled_delay {
239+ if last_export_time. elapsed ( ) >= config . scheduled_delay {
236240 if let Err ( err) = block_on ( exporter. export ( spans. split_off ( 0 ) ) ) {
237241 eprintln ! ( "Export error: {:?}" , err) ;
238242 }
@@ -251,7 +255,8 @@ impl BatchSpanProcessor {
251255 Self {
252256 message_sender,
253257 handle : Mutex :: new ( Some ( handle) ) ,
254- shutdown_timeout,
258+ forceflush_timeout : Duration :: from_secs ( 5 ) , // TODO: make this configurable
259+ shutdown_timeout : Duration :: from_secs ( 5 ) , // TODO: make this configurable
255260 is_shutdown : AtomicBool :: new ( false ) ,
256261 dropped_span_count : Arc :: new ( AtomicUsize :: new ( 0 ) ) ,
257262 }
@@ -262,7 +267,10 @@ impl BatchSpanProcessor {
262267 where
263268 E : SpanExporter + Send + ' static ,
264269 {
265- BatchSpanProcessorBuilder :: new ( exporter)
270+ BatchSpanProcessorBuilder {
271+ exporter,
272+ config : BatchConfig :: default ( ) ,
273+ }
266274 }
267275}
268276
@@ -302,8 +310,8 @@ impl SpanProcessor for BatchSpanProcessor {
302310 . map_err ( |_| TraceError :: Other ( "Failed to send ForceFlush message" . into ( ) ) ) ?;
303311
304312 receiver
305- . recv_timeout ( self . shutdown_timeout )
306- . map_err ( |_| TraceError :: ExportTimedOut ( self . shutdown_timeout ) ) ?
313+ . recv_timeout ( self . forceflush_timeout )
314+ . map_err ( |_| TraceError :: ExportTimedOut ( self . forceflush_timeout ) ) ?
307315 }
308316
309317 /// Shuts down the processor.
@@ -333,51 +341,21 @@ where
333341 E : SpanExporter + Send + ' static ,
334342{
335343 exporter : E ,
336- max_queue_size : usize ,
337- scheduled_delay : Duration ,
338- shutdown_timeout : Duration ,
344+ config : BatchConfig ,
339345}
340346
341347impl < E > BatchSpanProcessorBuilder < E >
342348where
343349 E : SpanExporter + Send + ' static ,
344350{
345- /// Creates a new builder with default values.
346- pub fn new ( exporter : E ) -> Self {
347- Self {
348- exporter,
349- max_queue_size : 2048 ,
350- scheduled_delay : Duration :: from_secs ( 5 ) ,
351- shutdown_timeout : Duration :: from_secs ( 5 ) ,
352- }
351+ /// Set the BatchConfig for [BatchSpanProcessorBuilder]
352+ pub fn with_batch_config ( self , config : BatchConfig ) -> Self {
353+ BatchSpanProcessorBuilder { config, ..self }
353354 }
354355
355- /// Sets the maximum queue size for spans.
356- pub fn with_max_queue_size ( mut self , size : usize ) -> Self {
357- self . max_queue_size = size;
358- self
359- }
360-
361- /// Sets the delay between exports.
362- pub fn with_scheduled_delay ( mut self , delay : Duration ) -> Self {
363- self . scheduled_delay = delay;
364- self
365- }
366-
367- /// Sets the timeout for shutdown and flush operations.
368- pub fn with_shutdown_timeout ( mut self , timeout : Duration ) -> Self {
369- self . shutdown_timeout = timeout;
370- self
371- }
372-
373- /// Builds the `BatchSpanProcessorDedicatedThread` instance.
356+ /// Build a new instance of `BatchSpanProcessor`.
374357 pub fn build ( self ) -> BatchSpanProcessor {
375- BatchSpanProcessor :: new (
376- self . exporter ,
377- self . max_queue_size ,
378- self . scheduled_delay ,
379- self . shutdown_timeout ,
380- )
358+ BatchSpanProcessor :: new ( self . exporter , self . config )
381359 }
382360}
383361
@@ -773,12 +751,13 @@ mod tests {
773751 fn batchspanprocessor_handles_on_end ( ) {
774752 let exporter = MockSpanExporter :: new ( ) ;
775753 let exporter_shared = exporter. exported_spans . clone ( ) ;
776- let processor = BatchSpanProcessor :: new (
777- exporter,
778- 10 , // max queue size
779- Duration :: from_secs ( 5 ) ,
780- Duration :: from_secs ( 2 ) ,
781- ) ;
754+ let config = BatchConfigBuilder :: default ( )
755+ . with_max_queue_size ( 10 )
756+ . with_max_export_batch_size ( 10 )
757+ . with_scheduled_delay ( Duration :: from_secs ( 5 ) )
758+ . with_max_export_timeout ( Duration :: from_secs ( 2 ) )
759+ . build ( ) ;
760+ let processor = BatchSpanProcessor :: new ( exporter, config) ;
782761
783762 let test_span = create_test_span ( "test_span" ) ;
784763 processor. on_end ( test_span. clone ( ) ) ;
@@ -795,12 +774,13 @@ mod tests {
795774 fn batchspanprocessor_force_flush ( ) {
796775 let exporter = MockSpanExporter :: new ( ) ;
797776 let exporter_shared = exporter. exported_spans . clone ( ) ; // Shared access to verify exported spans
798- let processor = BatchSpanProcessor :: new (
799- exporter,
800- 10 , // max queue size
801- Duration :: from_secs ( 5 ) ,
802- Duration :: from_secs ( 2 ) ,
803- ) ;
777+ let config = BatchConfigBuilder :: default ( )
778+ . with_max_queue_size ( 10 )
779+ . with_max_export_batch_size ( 10 )
780+ . with_scheduled_delay ( Duration :: from_secs ( 5 ) )
781+ . with_max_export_timeout ( Duration :: from_secs ( 2 ) )
782+ . build ( ) ;
783+ let processor = BatchSpanProcessor :: new ( exporter, config) ;
804784
805785 // Create a test span and send it to the processor
806786 let test_span = create_test_span ( "force_flush_span" ) ;
@@ -824,12 +804,13 @@ mod tests {
824804 fn batchspanprocessor_shutdown ( ) {
825805 let exporter = MockSpanExporter :: new ( ) ;
826806 let exporter_shared = exporter. exported_spans . clone ( ) ; // Shared access to verify exported spans
827- let processor = BatchSpanProcessor :: new (
828- exporter,
829- 10 , // max queue size
830- Duration :: from_secs ( 5 ) ,
831- Duration :: from_secs ( 2 ) ,
832- ) ;
807+ let config = BatchConfigBuilder :: default ( )
808+ . with_max_queue_size ( 10 )
809+ . with_max_export_batch_size ( 10 )
810+ . with_scheduled_delay ( Duration :: from_secs ( 5 ) )
811+ . with_max_export_timeout ( Duration :: from_secs ( 2 ) )
812+ . build ( ) ;
813+ let processor = BatchSpanProcessor :: new ( exporter, config) ;
833814
834815 // Create a test span and send it to the processor
835816 let test_span = create_test_span ( "shutdown_span" ) ;
0 commit comments