Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add test to confirm programmatic config wins over env in BatchConfig #2781

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion opentelemetry-sdk/src/logs/batch_log_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ impl Default for BatchConfigBuilder {
/// * `OTEL_BLRP_SCHEDULE_DELAY`
/// * `OTEL_BLRP_MAX_EXPORT_BATCH_SIZE`
/// * `OTEL_BLRP_EXPORT_TIMEOUT`
///
/// Note: Programmatic configuration overrides any value set via the environment variable.
fn default() -> Self {
BatchConfigBuilder {
max_queue_size: OTEL_BLRP_MAX_QUEUE_SIZE_DEFAULT,
Expand All @@ -630,7 +632,11 @@ impl BatchConfigBuilder {
/// Set max_queue_size for [`BatchConfigBuilder`].
/// It's the maximum queue size to buffer logs for delayed processing.
/// If the queue gets full it will drop the logs.
/// The default value of is 2048.
/// The default value is 2048.
///
/// Corresponding environment variable: `OTEL_BLRP_MAX_QUEUE_SIZE`.
///
/// Note: Programmatically setting this will override any value set via the environment variable.
pub fn with_max_queue_size(mut self, max_queue_size: usize) -> Self {
self.max_queue_size = max_queue_size;
self
Expand All @@ -639,6 +645,10 @@ impl BatchConfigBuilder {
/// Set scheduled_delay for [`BatchConfigBuilder`].
/// It's the delay interval in milliseconds between two consecutive processing of batches.
/// The default value is 1000 milliseconds.
///
/// Corresponding environment variable: `OTEL_BLRP_SCHEDULE_DELAY`.
///
/// Note: Programmatically setting this will override any value set via the environment variable.
pub fn with_scheduled_delay(mut self, scheduled_delay: Duration) -> Self {
self.scheduled_delay = scheduled_delay;
self
Expand All @@ -647,6 +657,10 @@ impl BatchConfigBuilder {
/// Set max_export_timeout for [`BatchConfigBuilder`].
/// It's the maximum duration to export a batch of data.
/// The default value is 30000 milliseconds.
///
/// Corresponding environment variable: `OTEL_BLRP_EXPORT_TIMEOUT`.
///
/// Note: Programmatically setting this will override any value set via the environment variable.
#[cfg(feature = "experimental_logs_batch_log_processor_with_async_runtime")]
pub fn with_max_export_timeout(mut self, max_export_timeout: Duration) -> Self {
self.max_export_timeout = max_export_timeout;
Expand All @@ -658,6 +672,10 @@ impl BatchConfigBuilder {
/// more than one batch worth of logs then it processes multiple batches
/// of logs one batch after the other without any delay.
/// The default value is 512.
///
/// Corresponding environment variable: `OTEL_BLRP_MAX_EXPORT_BATCH_SIZE`.
///
/// Note: Programmatically setting this will override any value set via the environment variable.
pub fn with_max_export_batch_size(mut self, max_export_batch_size: usize) -> Self {
self.max_export_batch_size = max_export_batch_size;
self
Expand Down Expand Up @@ -774,6 +792,27 @@ mod tests {
);
}

#[test]
fn test_code_based_config_overrides_env_vars() {
let env_vars = vec![
(OTEL_BLRP_SCHEDULE_DELAY, Some("2000")),
(OTEL_BLRP_MAX_QUEUE_SIZE, Some("4096")),
(OTEL_BLRP_MAX_EXPORT_BATCH_SIZE, Some("1024")),
];

temp_env::with_vars(env_vars, || {
let config = BatchConfigBuilder::default()
.with_max_queue_size(2048)
.with_scheduled_delay(Duration::from_millis(1000))
.with_max_export_batch_size(512)
.build();

assert_eq!(config.scheduled_delay, Duration::from_millis(1000));
assert_eq!(config.max_queue_size, 2048);
assert_eq!(config.max_export_batch_size, 512);
});
}

#[test]
fn test_batch_config_configurable_by_env_vars() {
let env_vars = vec![
Expand Down
13 changes: 10 additions & 3 deletions opentelemetry-sdk/src/logs/logger_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,22 @@ impl LoggerProviderBuilder {
LoggerProviderBuilder { processors, ..self }
}

/// Adds a [BatchLogProcessor] with the configured exporter to the pipeline.
/// Adds a [BatchLogProcessor] with the configured exporter to the pipeline,
/// using the default [super::BatchConfig].
///
/// The following environment variables can be used to configure the batching configuration:
///
/// * `OTEL_BSP_SCHEDULE_DELAY` - Corresponds to `with_scheduled_delay`.
/// * `OTEL_BSP_MAX_QUEUE_SIZE` - Corresponds to `with_max_queue_size`.
/// * `OTEL_BSP_MAX_EXPORT_BATCH_SIZE` - Corresponds to `with_max_export_batch_size`.
///
/// # Arguments
///
/// * `exporter` - The exporter to be used by the BatchLogProcessor.
/// * `exporter` - The exporter to be used by the `BatchLogProcessor`.
///
/// # Returns
///
/// A new `Builder` instance with the BatchLogProcessor added to the pipeline.
/// A new `LoggerProviderBuilder` instance with the `BatchLogProcessor` added to the pipeline.
///
/// Processors are invoked in the order they are added.
pub fn with_batch_exporter<T: LogExporter + 'static>(self, exporter: T) -> Self {
Expand Down
Loading