Skip to content

Commit

Permalink
open-telemetry#6454 log warning message for misconfigured batch and q…
Browse files Browse the repository at this point in the history
…ueue size.
  • Loading branch information
chukunx committed Jan 26, 2025
1 parent 14d714a commit 0744fa5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
15 changes: 15 additions & 0 deletions api/all/src/main/java/io/opentelemetry/api/internal/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package io.opentelemetry.api.internal;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.concurrent.Immutable;

/**
Expand All @@ -30,4 +32,17 @@ public static void checkArgument(boolean isValid, String errorMessage) {
throw new IllegalArgumentException(errorMessage);
}
}

/**
* Logs a warning message if the argument is false.
*
* @param logger the logger instance that writes message.
* @param isValid whether the argument check passed.
* @param warnMessage the message to use for the warning log.
*/
public static void warnOnArgument(Logger logger, boolean isValid, String warnMessage) {
if (!isValid) {
logger.log(Level.WARNING, warnMessage);
}
}
}
12 changes: 12 additions & 0 deletions api/all/src/test/java/io/opentelemetry/api/internal/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
package io.opentelemetry.api.internal;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.jupiter.api.Test;

class UtilsTest {
Expand All @@ -19,4 +24,11 @@ void checkArgument() {
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(TEST_MESSAGE);
}

@Test
void warnOnArgument() {
Logger logger = mock(Logger.class);
Utils.warnOnArgument(logger, false, TEST_MESSAGE);
verify(logger, times(1)).log(Level.WARNING, TEST_MESSAGE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@
package io.opentelemetry.sdk.logs.export;

import static io.opentelemetry.api.internal.Utils.checkArgument;
import static io.opentelemetry.api.internal.Utils.warnOnArgument;
import static java.util.Objects.requireNonNull;

import io.opentelemetry.api.metrics.MeterProvider;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

/**
* Builder class for {@link BatchLogRecordProcessor}.
*
* @since 1.27.0
*/
public final class BatchLogRecordProcessorBuilder {
private static final Logger logger =
Logger.getLogger(BatchLogRecordProcessorBuilder.class.getName());

// Visible for testing
static final long DEFAULT_SCHEDULE_DELAY_MILLIS = 1000;
Expand Down Expand Up @@ -101,6 +105,10 @@ long getExporterTimeoutNanos() {
* @see BatchLogRecordProcessorBuilder#DEFAULT_MAX_QUEUE_SIZE
*/
public BatchLogRecordProcessorBuilder setMaxQueueSize(int maxQueueSize) {
warnOnArgument(
logger,
maxExportBatchSize <= maxQueueSize,
"maxExportBatchSize should not exceed maxQueueSize.");
this.maxQueueSize = maxQueueSize;
return this;
}
Expand All @@ -122,6 +130,10 @@ int getMaxQueueSize() {
*/
public BatchLogRecordProcessorBuilder setMaxExportBatchSize(int maxExportBatchSize) {
checkArgument(maxExportBatchSize > 0, "maxExportBatchSize must be positive.");
warnOnArgument(
logger,
maxExportBatchSize <= maxQueueSize,
"maxExportBatchSize should not exceed maxQueueSize.");
this.maxExportBatchSize = maxExportBatchSize;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
package io.opentelemetry.sdk.trace.export;

import static io.opentelemetry.api.internal.Utils.checkArgument;
import static io.opentelemetry.api.internal.Utils.warnOnArgument;
import static java.util.Objects.requireNonNull;

import io.opentelemetry.api.metrics.MeterProvider;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

/** Builder class for {@link BatchSpanProcessor}. */
public final class BatchSpanProcessorBuilder {
private static final Logger logger = Logger.getLogger(BatchSpanProcessorBuilder.class.getName());

// Visible for testing
static final long DEFAULT_SCHEDULE_DELAY_MILLIS = 5000;
Expand Down Expand Up @@ -109,6 +112,10 @@ long getExporterTimeoutNanos() {
* @see BatchSpanProcessorBuilder#DEFAULT_MAX_QUEUE_SIZE
*/
public BatchSpanProcessorBuilder setMaxQueueSize(int maxQueueSize) {
warnOnArgument(
logger,
maxExportBatchSize <= maxQueueSize,
"maxExportBatchSize should not exceed maxQueueSize.");
this.maxQueueSize = maxQueueSize;
return this;
}
Expand All @@ -130,6 +137,10 @@ int getMaxQueueSize() {
*/
public BatchSpanProcessorBuilder setMaxExportBatchSize(int maxExportBatchSize) {
checkArgument(maxExportBatchSize > 0, "maxExportBatchSize must be positive.");
warnOnArgument(
logger,
maxExportBatchSize <= maxQueueSize,
"maxExportBatchSize should not exceed maxQueueSize.");
this.maxExportBatchSize = maxExportBatchSize;
return this;
}
Expand Down

0 comments on commit 0744fa5

Please sign in to comment.