Skip to content

Commit 52cd0e9

Browse files
authored
test: Add test to confirm programmatic config wins over env in BatchConfig (#2781)
1 parent 51dda2f commit 52cd0e9

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

opentelemetry-sdk/src/logs/batch_log_processor.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,8 @@ impl Default for BatchConfigBuilder {
614614
/// * `OTEL_BLRP_SCHEDULE_DELAY`
615615
/// * `OTEL_BLRP_MAX_EXPORT_BATCH_SIZE`
616616
/// * `OTEL_BLRP_EXPORT_TIMEOUT`
617+
///
618+
/// Note: Programmatic configuration overrides any value set via the environment variable.
617619
fn default() -> Self {
618620
BatchConfigBuilder {
619621
max_queue_size: OTEL_BLRP_MAX_QUEUE_SIZE_DEFAULT,
@@ -630,7 +632,11 @@ impl BatchConfigBuilder {
630632
/// Set max_queue_size for [`BatchConfigBuilder`].
631633
/// It's the maximum queue size to buffer logs for delayed processing.
632634
/// If the queue gets full it will drop the logs.
633-
/// The default value of is 2048.
635+
/// The default value is 2048.
636+
///
637+
/// Corresponding environment variable: `OTEL_BLRP_MAX_QUEUE_SIZE`.
638+
///
639+
/// Note: Programmatically setting this will override any value set via the environment variable.
634640
pub fn with_max_queue_size(mut self, max_queue_size: usize) -> Self {
635641
self.max_queue_size = max_queue_size;
636642
self
@@ -639,6 +645,10 @@ impl BatchConfigBuilder {
639645
/// Set scheduled_delay for [`BatchConfigBuilder`].
640646
/// It's the delay interval in milliseconds between two consecutive processing of batches.
641647
/// The default value is 1000 milliseconds.
648+
///
649+
/// Corresponding environment variable: `OTEL_BLRP_SCHEDULE_DELAY`.
650+
///
651+
/// Note: Programmatically setting this will override any value set via the environment variable.
642652
pub fn with_scheduled_delay(mut self, scheduled_delay: Duration) -> Self {
643653
self.scheduled_delay = scheduled_delay;
644654
self
@@ -647,6 +657,10 @@ impl BatchConfigBuilder {
647657
/// Set max_export_timeout for [`BatchConfigBuilder`].
648658
/// It's the maximum duration to export a batch of data.
649659
/// The default value is 30000 milliseconds.
660+
///
661+
/// Corresponding environment variable: `OTEL_BLRP_EXPORT_TIMEOUT`.
662+
///
663+
/// Note: Programmatically setting this will override any value set via the environment variable.
650664
#[cfg(feature = "experimental_logs_batch_log_processor_with_async_runtime")]
651665
pub fn with_max_export_timeout(mut self, max_export_timeout: Duration) -> Self {
652666
self.max_export_timeout = max_export_timeout;
@@ -658,6 +672,10 @@ impl BatchConfigBuilder {
658672
/// more than one batch worth of logs then it processes multiple batches
659673
/// of logs one batch after the other without any delay.
660674
/// The default value is 512.
675+
///
676+
/// Corresponding environment variable: `OTEL_BLRP_MAX_EXPORT_BATCH_SIZE`.
677+
///
678+
/// Note: Programmatically setting this will override any value set via the environment variable.
661679
pub fn with_max_export_batch_size(mut self, max_export_batch_size: usize) -> Self {
662680
self.max_export_batch_size = max_export_batch_size;
663681
self
@@ -774,6 +792,27 @@ mod tests {
774792
);
775793
}
776794

795+
#[test]
796+
fn test_code_based_config_overrides_env_vars() {
797+
let env_vars = vec![
798+
(OTEL_BLRP_SCHEDULE_DELAY, Some("2000")),
799+
(OTEL_BLRP_MAX_QUEUE_SIZE, Some("4096")),
800+
(OTEL_BLRP_MAX_EXPORT_BATCH_SIZE, Some("1024")),
801+
];
802+
803+
temp_env::with_vars(env_vars, || {
804+
let config = BatchConfigBuilder::default()
805+
.with_max_queue_size(2048)
806+
.with_scheduled_delay(Duration::from_millis(1000))
807+
.with_max_export_batch_size(512)
808+
.build();
809+
810+
assert_eq!(config.scheduled_delay, Duration::from_millis(1000));
811+
assert_eq!(config.max_queue_size, 2048);
812+
assert_eq!(config.max_export_batch_size, 512);
813+
});
814+
}
815+
777816
#[test]
778817
fn test_batch_config_configurable_by_env_vars() {
779818
let env_vars = vec![

opentelemetry-sdk/src/logs/logger_provider.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,22 @@ impl LoggerProviderBuilder {
200200
LoggerProviderBuilder { processors, ..self }
201201
}
202202

203-
/// Adds a [BatchLogProcessor] with the configured exporter to the pipeline.
203+
/// Adds a [BatchLogProcessor] with the configured exporter to the pipeline,
204+
/// using the default [super::BatchConfig].
205+
///
206+
/// The following environment variables can be used to configure the batching configuration:
207+
///
208+
/// * `OTEL_BLRP_SCHEDULE_DELAY` - Corresponds to `with_scheduled_delay`.
209+
/// * `OTEL_BLRP_MAX_QUEUE_SIZE` - Corresponds to `with_max_queue_size`.
210+
/// * `OTEL_BLRP_MAX_EXPORT_BATCH_SIZE` - Corresponds to `with_max_export_batch_size`.
204211
///
205212
/// # Arguments
206213
///
207-
/// * `exporter` - The exporter to be used by the BatchLogProcessor.
214+
/// * `exporter` - The exporter to be used by the `BatchLogProcessor`.
208215
///
209216
/// # Returns
210217
///
211-
/// A new `Builder` instance with the BatchLogProcessor added to the pipeline.
218+
/// A new `LoggerProviderBuilder` instance with the `BatchLogProcessor` added to the pipeline.
212219
///
213220
/// Processors are invoked in the order they are added.
214221
pub fn with_batch_exporter<T: LogExporter + 'static>(self, exporter: T) -> Self {

0 commit comments

Comments
 (0)