@@ -13,7 +13,7 @@ use opentelemetry::{
13
13
global,
14
14
logs:: { LogError , LogResult } ,
15
15
} ;
16
- use std:: { env, sync:: Mutex } ;
16
+ use std:: { cmp :: min , env, sync:: Mutex } ;
17
17
use std:: {
18
18
fmt:: { self , Debug , Formatter } ,
19
19
str:: FromStr ,
@@ -246,7 +246,7 @@ impl<R: RuntimeChannel> BatchLogProcessor<R> {
246
246
{
247
247
BatchLogProcessorBuilder {
248
248
exporter,
249
- config : BatchConfig :: default ( ) ,
249
+ config : Default :: default ( ) ,
250
250
runtime,
251
251
}
252
252
}
@@ -276,7 +276,8 @@ where
276
276
}
277
277
}
278
278
279
- /// Batch log processor configuration
279
+ /// Batch log processor configuration.
280
+ /// Use [`BatchConfigBuilder`] to configure your own instance of [`BatchConfig`].
280
281
#[ derive( Debug ) ]
281
282
pub struct BatchConfig {
282
283
/// The maximum queue size to buffer logs for delayed processing. If the
@@ -299,55 +300,36 @@ pub struct BatchConfig {
299
300
300
301
impl Default for BatchConfig {
301
302
fn default ( ) -> Self {
302
- let mut config = BatchConfig {
303
+ BatchConfigBuilder :: default ( ) . build ( )
304
+ }
305
+ }
306
+
307
+ /// A builder for creating [`BatchConfig`] instances.
308
+ #[ derive( Debug ) ]
309
+ pub struct BatchConfigBuilder {
310
+ max_queue_size : usize ,
311
+ scheduled_delay : Duration ,
312
+ max_export_batch_size : usize ,
313
+ max_export_timeout : Duration ,
314
+ }
315
+
316
+ impl Default for BatchConfigBuilder {
317
+ /// Create a new [`BatchConfigBuilder`] initialized with default batch config values as per the specs.
318
+ /// The values are overriden by environment variables if set.
319
+ /// For a list of supported environment variables see [Batch LogRecord Processor](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md#batch-logrecord-processor).
320
+ fn default ( ) -> Self {
321
+ BatchConfigBuilder {
303
322
max_queue_size : OTEL_BLRP_MAX_QUEUE_SIZE_DEFAULT ,
304
323
scheduled_delay : Duration :: from_millis ( OTEL_BLRP_SCHEDULE_DELAY_DEFAULT ) ,
305
324
max_export_batch_size : OTEL_BLRP_MAX_EXPORT_BATCH_SIZE_DEFAULT ,
306
325
max_export_timeout : Duration :: from_millis ( OTEL_BLRP_EXPORT_TIMEOUT_DEFAULT ) ,
307
- } ;
308
-
309
- if let Some ( max_queue_size) = env:: var ( OTEL_BLRP_MAX_QUEUE_SIZE )
310
- . ok ( )
311
- . and_then ( |queue_size| usize:: from_str ( & queue_size) . ok ( ) )
312
- {
313
- config. max_queue_size = max_queue_size;
314
- }
315
-
316
- if let Some ( max_export_batch_size) = env:: var ( OTEL_BLRP_MAX_EXPORT_BATCH_SIZE )
317
- . ok ( )
318
- . and_then ( |batch_size| usize:: from_str ( & batch_size) . ok ( ) )
319
- {
320
- config. max_export_batch_size = max_export_batch_size;
321
326
}
322
-
323
- // max export batch size must be less or equal to max queue size.
324
- // we set max export batch size to max queue size if it's larger than max queue size.
325
- if config. max_export_batch_size > config. max_queue_size {
326
- config. max_export_batch_size = config. max_queue_size ;
327
- }
328
-
329
- if let Some ( scheduled_delay) = env:: var ( OTEL_BLRP_SCHEDULE_DELAY )
330
- . ok ( )
331
- . or_else ( || env:: var ( "OTEL_BLRP_SCHEDULE_DELAY_MILLIS" ) . ok ( ) )
332
- . and_then ( |delay| u64:: from_str ( & delay) . ok ( ) )
333
- {
334
- config. scheduled_delay = Duration :: from_millis ( scheduled_delay) ;
335
- }
336
-
337
- if let Some ( max_export_timeout) = env:: var ( OTEL_BLRP_EXPORT_TIMEOUT )
338
- . ok ( )
339
- . or_else ( || env:: var ( "OTEL_BLRP_EXPORT_TIMEOUT_MILLIS" ) . ok ( ) )
340
- . and_then ( |s| u64:: from_str ( & s) . ok ( ) )
341
- {
342
- config. max_export_timeout = Duration :: from_millis ( max_export_timeout) ;
343
- }
344
-
345
- config
327
+ . init_from_env_vars ( )
346
328
}
347
329
}
348
330
349
- impl BatchConfig {
350
- /// Set max_queue_size for [`BatchConfig `].
331
+ impl BatchConfigBuilder {
332
+ /// Set max_queue_size for [`BatchConfigBuilder `].
351
333
/// It's the maximum queue size to buffer logs for delayed processing.
352
334
/// If the queue gets full it will drop the logs.
353
335
/// The default value of is 2048.
@@ -356,23 +338,23 @@ impl BatchConfig {
356
338
self
357
339
}
358
340
359
- /// Set scheduled_delay for [`BatchConfig `].
341
+ /// Set scheduled_delay for [`BatchConfigBuilder `].
360
342
/// It's the delay interval in milliseconds between two consecutive processing of batches.
361
343
/// The default value is 1000 milliseconds.
362
344
pub fn with_scheduled_delay ( mut self , scheduled_delay : Duration ) -> Self {
363
345
self . scheduled_delay = scheduled_delay;
364
346
self
365
347
}
366
348
367
- /// Set max_export_timeout for [`BatchConfig `].
349
+ /// Set max_export_timeout for [`BatchConfigBuilder `].
368
350
/// It's the maximum duration to export a batch of data.
369
351
/// The default value is 30000 milliseconds.
370
352
pub fn with_max_export_timeout ( mut self , max_export_timeout : Duration ) -> Self {
371
353
self . max_export_timeout = max_export_timeout;
372
354
self
373
355
}
374
356
375
- /// Set max_export_batch_size for [`BatchConfig `].
357
+ /// Set max_export_batch_size for [`BatchConfigBuilder `].
376
358
/// It's the maximum number of logs to process in a single batch. If there are
377
359
/// more than one batch worth of logs then it processes multiple batches
378
360
/// of logs one batch after the other without any delay.
@@ -381,6 +363,55 @@ impl BatchConfig {
381
363
self . max_export_batch_size = max_export_batch_size;
382
364
self
383
365
}
366
+
367
+ /// Builds a `BatchConfig` enforcing the following invariants:
368
+ /// * `max_export_batch_size` must be less than or equal to `max_queue_size`.
369
+ pub fn build ( self ) -> BatchConfig {
370
+ // max export batch size must be less or equal to max queue size.
371
+ // we set max export batch size to max queue size if it's larger than max queue size.
372
+ let max_export_batch_size = min ( self . max_export_batch_size , self . max_queue_size ) ;
373
+
374
+ BatchConfig {
375
+ max_queue_size : self . max_queue_size ,
376
+ scheduled_delay : self . scheduled_delay ,
377
+ max_export_timeout : self . max_export_timeout ,
378
+ max_export_batch_size,
379
+ }
380
+ }
381
+
382
+ fn init_from_env_vars ( mut self ) -> Self {
383
+ if let Some ( max_queue_size) = env:: var ( OTEL_BLRP_MAX_QUEUE_SIZE )
384
+ . ok ( )
385
+ . and_then ( |queue_size| usize:: from_str ( & queue_size) . ok ( ) )
386
+ {
387
+ self . max_queue_size = max_queue_size;
388
+ }
389
+
390
+ if let Some ( max_export_batch_size) = env:: var ( OTEL_BLRP_MAX_EXPORT_BATCH_SIZE )
391
+ . ok ( )
392
+ . and_then ( |batch_size| usize:: from_str ( & batch_size) . ok ( ) )
393
+ {
394
+ self . max_export_batch_size = max_export_batch_size;
395
+ }
396
+
397
+ if let Some ( scheduled_delay) = env:: var ( OTEL_BLRP_SCHEDULE_DELAY )
398
+ . ok ( )
399
+ . or_else ( || env:: var ( "OTEL_BLRP_SCHEDULE_DELAY_MILLIS" ) . ok ( ) )
400
+ . and_then ( |delay| u64:: from_str ( & delay) . ok ( ) )
401
+ {
402
+ self . scheduled_delay = Duration :: from_millis ( scheduled_delay) ;
403
+ }
404
+
405
+ if let Some ( max_export_timeout) = env:: var ( OTEL_BLRP_EXPORT_TIMEOUT )
406
+ . ok ( )
407
+ . or_else ( || env:: var ( "OTEL_BLRP_EXPORT_TIMEOUT_MILLIS" ) . ok ( ) )
408
+ . and_then ( |s| u64:: from_str ( & s) . ok ( ) )
409
+ {
410
+ self . max_export_timeout = Duration :: from_millis ( max_export_timeout) ;
411
+ }
412
+
413
+ self
414
+ }
384
415
}
385
416
386
417
/// A builder for creating [`BatchLogProcessor`] instances.
@@ -397,44 +428,6 @@ where
397
428
E : LogExporter + ' static ,
398
429
R : RuntimeChannel ,
399
430
{
400
- /// Set max queue size for batches
401
- pub fn with_max_queue_size ( self , size : usize ) -> Self {
402
- let mut config = self . config ;
403
- config. max_queue_size = size;
404
-
405
- BatchLogProcessorBuilder { config, ..self }
406
- }
407
-
408
- /// Set scheduled delay for batches
409
- pub fn with_scheduled_delay ( self , delay : Duration ) -> Self {
410
- let mut config = self . config ;
411
- config. scheduled_delay = delay;
412
-
413
- BatchLogProcessorBuilder { config, ..self }
414
- }
415
-
416
- /// Set max timeout for exporting.
417
- pub fn with_max_timeout ( self , timeout : Duration ) -> Self {
418
- let mut config = self . config ;
419
- config. max_export_timeout = timeout;
420
-
421
- BatchLogProcessorBuilder { config, ..self }
422
- }
423
-
424
- /// Set max export size for batches, should always less than or equals to max queue size.
425
- ///
426
- /// If input is larger than max queue size, will lower it to be equal to max queue size
427
- pub fn with_max_export_batch_size ( self , size : usize ) -> Self {
428
- let mut config = self . config ;
429
- if size > config. max_queue_size {
430
- config. max_export_batch_size = config. max_queue_size ;
431
- } else {
432
- config. max_export_batch_size = size;
433
- }
434
-
435
- BatchLogProcessorBuilder { config, ..self }
436
- }
437
-
438
431
/// Set the BatchConfig for [`BatchLogProcessorBuilder`]
439
432
pub fn with_batch_config ( self , config : BatchConfig ) -> Self {
440
433
BatchLogProcessorBuilder { config, ..self }
@@ -471,7 +464,7 @@ mod tests {
471
464
OTEL_BLRP_EXPORT_TIMEOUT_DEFAULT , OTEL_BLRP_MAX_EXPORT_BATCH_SIZE_DEFAULT ,
472
465
OTEL_BLRP_MAX_QUEUE_SIZE_DEFAULT , OTEL_BLRP_SCHEDULE_DELAY_DEFAULT ,
473
466
} ,
474
- BatchConfig ,
467
+ BatchConfig , BatchConfigBuilder ,
475
468
} ,
476
469
runtime,
477
470
testing:: logs:: InMemoryLogsExporter ,
@@ -590,11 +583,12 @@ mod tests {
590
583
591
584
#[ test]
592
585
fn test_batch_config_with_fields ( ) {
593
- let batch = BatchConfig :: default ( )
586
+ let batch = BatchConfigBuilder :: default ( )
594
587
. with_max_export_batch_size ( 1 )
595
588
. with_scheduled_delay ( Duration :: from_millis ( 2 ) )
596
589
. with_max_export_timeout ( Duration :: from_millis ( 3 ) )
597
- . with_max_queue_size ( 4 ) ;
590
+ . with_max_queue_size ( 4 )
591
+ . build ( ) ;
598
592
599
593
assert_eq ! ( batch. max_export_batch_size, 1 ) ;
600
594
assert_eq ! ( batch. scheduled_delay, Duration :: from_millis( 2 ) ) ;
@@ -640,11 +634,12 @@ mod tests {
640
634
641
635
#[ test]
642
636
fn test_build_batch_log_processor_builder_with_custom_config ( ) {
643
- let expected = BatchConfig :: default ( )
637
+ let expected = BatchConfigBuilder :: default ( )
644
638
. with_max_export_batch_size ( 1 )
645
639
. with_scheduled_delay ( Duration :: from_millis ( 2 ) )
646
640
. with_max_export_timeout ( Duration :: from_millis ( 3 ) )
647
- . with_max_queue_size ( 4 ) ;
641
+ . with_max_queue_size ( 4 )
642
+ . build ( ) ;
648
643
649
644
let builder = BatchLogProcessor :: builder ( InMemoryLogsExporter :: default ( ) , runtime:: Tokio )
650
645
. with_batch_config ( expected) ;
0 commit comments