Move Builders as non inner classes in sdk-extension/logging (#2864)

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
This commit is contained in:
Bogdan Drutu 2021-02-17 15:05:33 -08:00 committed by GitHub
parent 6bb9894b2f
commit 904ca1d873
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 263 additions and 219 deletions

View File

@ -16,7 +16,16 @@ public final class LogSinkSdkProvider {
private final LogSink logSink = new SdkLogSink();
private final List<LogProcessor> processors = new ArrayList<>();
private LogSinkSdkProvider() {}
/**
* Returns a new {@link LogSinkSdkProviderBuilder} for this class.
*
* @return a new {@link LogSinkSdkProviderBuilder} for this class.
*/
static LogSinkSdkProviderBuilder builder() {
return new LogSinkSdkProviderBuilder();
}
LogSinkSdkProvider() {}
public LogSink get(String instrumentationName, String instrumentationVersion) {
// Currently there is no differentiation by instrumentation library
@ -61,10 +70,4 @@ public final class LogSinkSdkProvider {
}
}
}
public static class Builder {
public LogSinkSdkProvider build() {
return new LogSinkSdkProvider();
}
}
}

View File

@ -0,0 +1,14 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.logging;
public final class LogSinkSdkProviderBuilder {
LogSinkSdkProviderBuilder() {}
public LogSinkSdkProvider build() {
return new LogSinkSdkProvider();
}
}

View File

@ -7,8 +7,6 @@ package io.opentelemetry.sdk.logging.data;
import com.google.auto.value.AutoValue;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
/**
@ -19,6 +17,24 @@ import javax.annotation.Nullable;
@AutoValue
public abstract class LogRecord {
public static LogRecordBuilder builder() {
return new LogRecordBuilder();
}
static LogRecord create(
long timeUnixNano,
String traceId,
String spanId,
int flags,
Severity severity,
String severityText,
String name,
AnyValue body,
Attributes attributes) {
return new AutoValue_LogRecord(
timeUnixNano, traceId, spanId, flags, severity, severityText, name, body, attributes);
}
public abstract long getTimeUnixNano();
public abstract String getTraceId();
@ -77,94 +93,4 @@ public abstract class LogRecord {
return severityNumber;
}
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private long timeUnixNano;
private String traceId = "";
private String spanId = "";
private int flags;
private Severity severity = Severity.UNDEFINED_SEVERITY_NUMBER;
private String severityText;
private String name;
private AnyValue body = AnyValue.stringAnyValue("");
private final AttributesBuilder attributeBuilder = Attributes.builder();
public Builder setUnixTimeNano(long timestamp) {
this.timeUnixNano = timestamp;
return this;
}
public Builder setUnixTimeMillis(long timestamp) {
return setUnixTimeNano(TimeUnit.MILLISECONDS.toNanos(timestamp));
}
public Builder setTraceId(String traceId) {
this.traceId = traceId;
return this;
}
public Builder setSpanId(String spanId) {
this.spanId = spanId;
return this;
}
public Builder setFlags(int flags) {
this.flags = flags;
return this;
}
public Builder setSeverity(Severity severity) {
this.severity = severity;
return this;
}
public Builder setSeverityText(String severityText) {
this.severityText = severityText;
return this;
}
public Builder setName(String name) {
this.name = name;
return this;
}
public Builder setBody(AnyValue body) {
this.body = body;
return this;
}
public Builder setBody(String body) {
return setBody(AnyValue.stringAnyValue(body));
}
public Builder setAttributes(Attributes attributes) {
this.attributeBuilder.putAll(attributes);
return this;
}
/**
* Build a LogRecord instance.
*
* @return value object being built
*/
public LogRecord build() {
if (timeUnixNano == 0) {
timeUnixNano = TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis());
}
return new AutoValue_LogRecord(
timeUnixNano,
traceId,
spanId,
flags,
severity,
severityText,
name,
body,
attributeBuilder.build());
}
}
}

View File

@ -0,0 +1,98 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.logging.data;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import java.util.concurrent.TimeUnit;
public final class LogRecordBuilder {
private long timeUnixNano;
private String traceId = "";
private String spanId = "";
private int flags;
private LogRecord.Severity severity = LogRecord.Severity.UNDEFINED_SEVERITY_NUMBER;
private String severityText;
private String name;
private AnyValue body = AnyValue.stringAnyValue("");
private final AttributesBuilder attributeBuilder = Attributes.builder();
LogRecordBuilder() {}
public LogRecordBuilder setUnixTimeNano(long timestamp) {
this.timeUnixNano = timestamp;
return this;
}
public LogRecordBuilder setUnixTimeMillis(long timestamp) {
return setUnixTimeNano(TimeUnit.MILLISECONDS.toNanos(timestamp));
}
public LogRecordBuilder setTraceId(String traceId) {
this.traceId = traceId;
return this;
}
public LogRecordBuilder setSpanId(String spanId) {
this.spanId = spanId;
return this;
}
public LogRecordBuilder setFlags(int flags) {
this.flags = flags;
return this;
}
public LogRecordBuilder setSeverity(LogRecord.Severity severity) {
this.severity = severity;
return this;
}
public LogRecordBuilder setSeverityText(String severityText) {
this.severityText = severityText;
return this;
}
public LogRecordBuilder setName(String name) {
this.name = name;
return this;
}
public LogRecordBuilder setBody(AnyValue body) {
this.body = body;
return this;
}
public LogRecordBuilder setBody(String body) {
return setBody(AnyValue.stringAnyValue(body));
}
public LogRecordBuilder setAttributes(Attributes attributes) {
this.attributeBuilder.putAll(attributes);
return this;
}
/**
* Build a LogRecord instance.
*
* @return value object being built
*/
public LogRecord build() {
if (timeUnixNano == 0) {
timeUnixNano = TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis());
}
return LogRecord.create(
timeUnixNano,
traceId,
spanId,
flags,
severity,
severityText,
name,
body,
attributeBuilder.build());
}
}

View File

@ -5,7 +5,6 @@
package io.opentelemetry.sdk.logging.export;
import io.opentelemetry.api.internal.Utils;
import io.opentelemetry.api.metrics.BoundLongCounter;
import io.opentelemetry.api.metrics.GlobalMetricsProvider;
import io.opentelemetry.api.metrics.LongCounter;
@ -16,7 +15,6 @@ import io.opentelemetry.sdk.internal.DaemonThreadFactory;
import io.opentelemetry.sdk.logging.LogProcessor;
import io.opentelemetry.sdk.logging.data.LogRecord;
import java.util.ArrayList;
import java.util.Objects;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
@ -29,7 +27,7 @@ public final class BatchLogProcessor implements LogProcessor {
private final Worker worker;
private final Thread workerThread;
private BatchLogProcessor(
BatchLogProcessor(
int maxQueueSize,
long scheduleDelayMillis,
int maxExportBatchSize,
@ -46,8 +44,8 @@ public final class BatchLogProcessor implements LogProcessor {
this.workerThread.start();
}
public static Builder builder(LogExporter logExporter) {
return new Builder(logExporter);
public static BatchLogProcessorBuilder builder(LogExporter logExporter) {
return new BatchLogProcessorBuilder(logExporter);
}
@Override
@ -216,114 +214,4 @@ public final class BatchLogProcessor implements LogProcessor {
}
}
}
public static final class Builder {
private static final long DEFAULT_SCHEDULE_DELAY_MILLIS = 200;
private static final int DEFAULT_MAX_QUEUE_SIZE = 2048;
private static final int DEFAULT_MAX_EXPORT_BATCH_SIZE = 512;
private static final long DEFAULT_EXPORT_TIMEOUT_MILLIS = 30_000;
private final LogExporter logExporter;
private long scheduleDelayMillis = DEFAULT_SCHEDULE_DELAY_MILLIS;
private int maxQueueSize = DEFAULT_MAX_QUEUE_SIZE;
private int maxExportBatchSize = DEFAULT_MAX_EXPORT_BATCH_SIZE;
private long exporterTimeoutMillis = DEFAULT_EXPORT_TIMEOUT_MILLIS;
private Builder(LogExporter logExporter) {
this.logExporter = Objects.requireNonNull(logExporter, "Exporter argument can not be null");
}
/**
* Build a BatchLogProcessor.
*
* @return configured processor
*/
public BatchLogProcessor build() {
return new BatchLogProcessor(
maxQueueSize,
scheduleDelayMillis,
maxExportBatchSize,
exporterTimeoutMillis,
logExporter);
}
/**
* Sets the delay interval between two consecutive exports. The actual interval may be shorter
* if the batch size is getting larger than {@code maxQueuedSpans / 2}.
*
* <p>Default value is {@code 250}ms.
*
* @param scheduleDelayMillis the delay interval between two consecutive exports.
* @return this.
* @see BatchLogProcessor.Builder#DEFAULT_SCHEDULE_DELAY_MILLIS
*/
public BatchLogProcessor.Builder setScheduleDelayMillis(long scheduleDelayMillis) {
this.scheduleDelayMillis = scheduleDelayMillis;
return this;
}
public long getScheduleDelayMillis() {
return scheduleDelayMillis;
}
/**
* Sets the maximum time an exporter will be allowed to run before being cancelled.
*
* <p>Default value is {@code 30000}ms
*
* @param exporterTimeoutMillis the timeout for exports in milliseconds.
* @return this
* @see BatchLogProcessor.Builder#DEFAULT_EXPORT_TIMEOUT_MILLIS
*/
public Builder setExporterTimeoutMillis(int exporterTimeoutMillis) {
this.exporterTimeoutMillis = exporterTimeoutMillis;
return this;
}
public long getExporterTimeoutMillis() {
return exporterTimeoutMillis;
}
/**
* Sets the maximum number of Spans that are kept in the queue before start dropping.
*
* <p>See the BatchSampledSpansProcessor class description for a high-level design description
* of this class.
*
* <p>Default value is {@code 2048}.
*
* @param maxQueueSize the maximum number of Spans that are kept in the queue before start
* dropping.
* @return this.
* @see BatchLogProcessor.Builder#DEFAULT_MAX_QUEUE_SIZE
*/
public Builder setMaxQueueSize(int maxQueueSize) {
this.maxQueueSize = maxQueueSize;
return this;
}
public int getMaxQueueSize() {
return maxQueueSize;
}
/**
* Sets the maximum batch size for every export. This must be smaller or equal to {@code
* maxQueuedSpans}.
*
* <p>Default value is {@code 512}.
*
* @param maxExportBatchSize the maximum batch size for every export.
* @return this.
* @see BatchLogProcessor.Builder#DEFAULT_MAX_EXPORT_BATCH_SIZE
*/
public Builder setMaxExportBatchSize(int maxExportBatchSize) {
Utils.checkArgument(maxExportBatchSize > 0, "maxExportBatchSize must be positive.");
this.maxExportBatchSize = maxExportBatchSize;
return this;
}
public int getMaxExportBatchSize() {
return maxExportBatchSize;
}
}
}

View File

@ -0,0 +1,115 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.logging.export;
import io.opentelemetry.api.internal.Utils;
import java.util.Objects;
public final class BatchLogProcessorBuilder {
private static final long DEFAULT_SCHEDULE_DELAY_MILLIS = 200;
private static final int DEFAULT_MAX_QUEUE_SIZE = 2048;
private static final int DEFAULT_MAX_EXPORT_BATCH_SIZE = 512;
private static final long DEFAULT_EXPORT_TIMEOUT_MILLIS = 30_000;
private final LogExporter logExporter;
private long scheduleDelayMillis = DEFAULT_SCHEDULE_DELAY_MILLIS;
private int maxQueueSize = DEFAULT_MAX_QUEUE_SIZE;
private int maxExportBatchSize = DEFAULT_MAX_EXPORT_BATCH_SIZE;
private long exporterTimeoutMillis = DEFAULT_EXPORT_TIMEOUT_MILLIS;
BatchLogProcessorBuilder(LogExporter logExporter) {
this.logExporter = Objects.requireNonNull(logExporter, "Exporter argument can not be null");
}
/**
* Build a BatchLogProcessor.
*
* @return configured processor
*/
public BatchLogProcessor build() {
return new BatchLogProcessor(
maxQueueSize, scheduleDelayMillis, maxExportBatchSize, exporterTimeoutMillis, logExporter);
}
/**
* Sets the delay interval between two consecutive exports. The actual interval may be shorter if
* the batch size is getting larger than {@code maxQueuedSpans / 2}.
*
* <p>Default value is {@code 250}ms.
*
* @param scheduleDelayMillis the delay interval between two consecutive exports.
* @return this.
* @see BatchLogProcessorBuilder#DEFAULT_SCHEDULE_DELAY_MILLIS
*/
public BatchLogProcessorBuilder setScheduleDelayMillis(long scheduleDelayMillis) {
this.scheduleDelayMillis = scheduleDelayMillis;
return this;
}
public long getScheduleDelayMillis() {
return scheduleDelayMillis;
}
/**
* Sets the maximum time an exporter will be allowed to run before being cancelled.
*
* <p>Default value is {@code 30000}ms
*
* @param exporterTimeoutMillis the timeout for exports in milliseconds.
* @return this
* @see BatchLogProcessorBuilder#DEFAULT_EXPORT_TIMEOUT_MILLIS
*/
public BatchLogProcessorBuilder setExporterTimeoutMillis(int exporterTimeoutMillis) {
this.exporterTimeoutMillis = exporterTimeoutMillis;
return this;
}
public long getExporterTimeoutMillis() {
return exporterTimeoutMillis;
}
/**
* Sets the maximum number of Spans that are kept in the queue before start dropping.
*
* <p>See the BatchSampledSpansProcessor class description for a high-level design description of
* this class.
*
* <p>Default value is {@code 2048}.
*
* @param maxQueueSize the maximum number of Spans that are kept in the queue before start
* dropping.
* @return this.
* @see BatchLogProcessorBuilder#DEFAULT_MAX_QUEUE_SIZE
*/
public BatchLogProcessorBuilder setMaxQueueSize(int maxQueueSize) {
this.maxQueueSize = maxQueueSize;
return this;
}
public int getMaxQueueSize() {
return maxQueueSize;
}
/**
* Sets the maximum batch size for every export. This must be smaller or equal to {@code
* maxQueuedSpans}.
*
* <p>Default value is {@code 512}.
*
* @param maxExportBatchSize the maximum batch size for every export.
* @return this.
* @see BatchLogProcessorBuilder#DEFAULT_MAX_EXPORT_BATCH_SIZE
*/
public BatchLogProcessorBuilder setMaxExportBatchSize(int maxExportBatchSize) {
Utils.checkArgument(maxExportBatchSize > 0, "maxExportBatchSize must be positive.");
this.maxExportBatchSize = maxExportBatchSize;
return this;
}
public int getMaxExportBatchSize() {
return maxExportBatchSize;
}
}

View File

@ -26,7 +26,7 @@ import org.junit.jupiter.api.Test;
class LogSinkSdkProviderTest {
private static LogRecord createLog(LogRecord.Severity severity, String message) {
return new LogRecord.Builder()
return LogRecord.builder()
.setUnixTimeMillis(System.currentTimeMillis())
.setTraceId(TraceId.getInvalid())
.setSpanId(SpanId.getInvalid())
@ -43,7 +43,7 @@ class LogSinkSdkProviderTest {
void testLogSinkSdkProvider() {
TestLogExporter exporter = new TestLogExporter();
LogProcessor processor = BatchLogProcessor.builder(exporter).build();
LogSinkSdkProvider provider = new LogSinkSdkProvider.Builder().build();
LogSinkSdkProvider provider = LogSinkSdkProvider.builder().build();
provider.addLogProcessor(processor);
LogSink sink = provider.get("test", "0.1a");
LogRecord log = createLog(LogRecord.Severity.ERROR, "test");
@ -64,7 +64,7 @@ class LogSinkSdkProviderTest {
.setMaxExportBatchSize(5)
.setMaxQueueSize(10)
.build();
LogSinkSdkProvider provider = new LogSinkSdkProvider.Builder().build();
LogSinkSdkProvider provider = LogSinkSdkProvider.builder().build();
provider.addLogProcessor(processor);
LogSink sink = provider.get("test", "0.1a");
@ -96,7 +96,7 @@ class LogSinkSdkProviderTest {
.setMaxExportBatchSize(5)
.setMaxQueueSize(10)
.build();
LogSinkSdkProvider provider = new LogSinkSdkProvider.Builder().build();
LogSinkSdkProvider provider = LogSinkSdkProvider.builder().build();
provider.addLogProcessor(processor);
LogSink sink = provider.get("test", "0.1a");
@ -115,7 +115,7 @@ class LogSinkSdkProviderTest {
void testMultipleProcessors() {
TestLogProcessor processorOne = new TestLogProcessor();
TestLogProcessor processorTwo = new TestLogProcessor();
LogSinkSdkProvider provider = new LogSinkSdkProvider.Builder().build();
LogSinkSdkProvider provider = LogSinkSdkProvider.builder().build();
provider.addLogProcessor(processorOne);
provider.addLogProcessor(processorTwo);
LogSink sink = provider.get("test", "0.1");