Skip to content

Commit

Permalink
Remove nullable from file config Factory contract (#6612)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg authored Aug 8, 2024
1 parent 04a2b0a commit 56e7ebe
Show file tree
Hide file tree
Showing 43 changed files with 96 additions and 358 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
import io.github.netmikey.logunit.api.LogCapturer;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator;
import io.opentelemetry.api.incubator.events.GlobalEventLoggerProvider;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.internal.testing.CleanupExtension;
import io.opentelemetry.sdk.OpenTelemetrySdk;
Expand Down Expand Up @@ -91,11 +87,6 @@ void configFile_Valid() {
Resource.getDefault().toBuilder().put("service.name", "test").build())
.addSpanProcessor(SimpleSpanProcessor.create(LoggingSpanExporter.create()))
.build())
.setPropagators(
ContextPropagators.create(
TextMapPropagator.composite(
W3CTraceContextPropagator.getInstance(),
W3CBaggagePropagator.getInstance())))
.build();
cleanup.addCloseable(expectedSdk);
AutoConfiguredOpenTelemetrySdkBuilder builder = spy(AutoConfiguredOpenTelemetrySdk.builder());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.ExplicitBucketHistogram;
import java.io.Closeable;
import java.util.List;
import javax.annotation.Nullable;

final class AggregationFactory
implements Factory<Aggregation, io.opentelemetry.sdk.metrics.Aggregation> {
Expand All @@ -27,11 +26,7 @@ static AggregationFactory getInstance() {

@Override
public io.opentelemetry.sdk.metrics.Aggregation create(
@Nullable Aggregation model, SpiHelper spiHelper, List<Closeable> closeables) {
if (model == null) {
return io.opentelemetry.sdk.metrics.Aggregation.defaultAggregation();
}

Aggregation model, SpiHelper spiHelper, List<Closeable> closeables) {
if (model.getDrop() != null) {
return io.opentelemetry.sdk.metrics.Aggregation.drop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.Attributes;
import java.io.Closeable;
import java.util.List;
import javax.annotation.Nullable;

final class AttributesFactory
implements Factory<Attributes, io.opentelemetry.api.common.Attributes> {
Expand All @@ -29,11 +28,7 @@ static AttributesFactory getInstance() {

@Override
public io.opentelemetry.api.common.Attributes create(
@Nullable Attributes model, SpiHelper spiHelper, List<Closeable> closeables) {
if (model == null) {
return io.opentelemetry.api.common.Attributes.empty();
}

Attributes model, SpiHelper spiHelper, List<Closeable> closeables) {
AttributesBuilder builder = io.opentelemetry.api.common.Attributes.builder();

String serviceName = model.getServiceName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import java.io.Closeable;
import java.util.List;
import javax.annotation.Nullable;

interface Factory<ModelT, ResultT> {

Expand All @@ -20,5 +19,5 @@ interface Factory<ModelT, ResultT> {
* @param closeables mutable list of closeables created
* @return the {@link ResultT}
*/
ResultT create(@Nullable ModelT model, SpiHelper spiHelper, List<Closeable> closeables);
ResultT create(ModelT model, SpiHelper spiHelper, List<Closeable> closeables);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ static <T> T assertNotNull(@Nullable T object, String description) {
return object;
}

static <T> T requireNonNull(@Nullable T object, String description) {
if (object == null) {
throw new ConfigurationException(description + " is required but is null");
}
return object;
}

/**
* Find a registered {@link ComponentProvider} which {@link ComponentProvider#getType()} matching
* {@code type}, {@link ComponentProvider#getName()} matching {@code name}, and call {@link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import io.opentelemetry.sdk.metrics.InstrumentType;
import java.io.Closeable;
import java.util.List;
import javax.annotation.Nullable;

final class InstrumentSelectorFactory implements Factory<Selector, InstrumentSelector> {

Expand All @@ -27,11 +26,7 @@ static InstrumentSelectorFactory getInstance() {

@Override
public InstrumentSelector create(
@Nullable Selector model, SpiHelper spiHelper, List<Closeable> closeables) {
if (model == null) {
throw new ConfigurationException("selector must not be null");
}

Selector model, SpiHelper spiHelper, List<Closeable> closeables) {
InstrumentSelectorBuilder builder = InstrumentSelector.builder();
if (model.getInstrumentName() != null) {
builder.setName(model.getInstrumentName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import io.opentelemetry.sdk.logs.LogLimitsBuilder;
import java.io.Closeable;
import java.util.List;
import javax.annotation.Nullable;

final class LogLimitsFactory implements Factory<LogRecordLimitsAndAttributeLimits, LogLimits> {

Expand All @@ -26,12 +25,7 @@ static LogLimitsFactory getInstance() {

@Override
public LogLimits create(
@Nullable LogRecordLimitsAndAttributeLimits model,
SpiHelper spiHelper,
List<Closeable> closeables) {
if (model == null) {
return LogLimits.getDefault();
}
LogRecordLimitsAndAttributeLimits model, SpiHelper spiHelper, List<Closeable> closeables) {
LogLimitsBuilder builder = LogLimits.builder();

AttributeLimits attributeLimitsModel = model.getAttributeLimits();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.io.Closeable;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;

final class LogRecordExporterFactory
implements Factory<
Expand All @@ -29,19 +28,11 @@ static LogRecordExporterFactory getInstance() {
return INSTANCE;
}

@SuppressWarnings("NullAway") // Override superclass non-null response
@Override
@Nullable
public LogRecordExporter create(
@Nullable
io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.LogRecordExporter
model,
io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.LogRecordExporter model,
SpiHelper spiHelper,
List<Closeable> closeables) {
if (model == null) {
return null;
}

Otlp otlpModel = model.getOtlp();
if (otlpModel != null) {
model.getAdditionalProperties().put("otlp", otlpModel);
Expand All @@ -67,8 +58,8 @@ public LogRecordExporter create(
exporterKeyValue.getKey(),
exporterKeyValue.getValue());
return FileConfigUtil.addAndReturn(closeables, logRecordExporter);
} else {
throw new ConfigurationException("log exporter must be set");
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.io.Closeable;
import java.time.Duration;
import java.util.List;
import javax.annotation.Nullable;

final class LogRecordProcessorFactory
implements Factory<
Expand All @@ -34,24 +33,18 @@ static LogRecordProcessorFactory getInstance() {

@Override
public LogRecordProcessor create(
@Nullable
io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.LogRecordProcessor
model,
io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.LogRecordProcessor model,
SpiHelper spiHelper,
List<Closeable> closeables) {
if (model == null) {
return LogRecordProcessor.composite();
}

io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.BatchLogRecordProcessor
batchModel = model.getBatch();
if (batchModel != null) {
LogRecordExporter exporterModel = batchModel.getExporter();
LogRecordExporter exporterModel =
FileConfigUtil.requireNonNull(
batchModel.getExporter(), "batch log record processor exporter");

io.opentelemetry.sdk.logs.export.LogRecordExporter logRecordExporter =
LogRecordExporterFactory.getInstance().create(exporterModel, spiHelper, closeables);
if (logRecordExporter == null) {
throw new ConfigurationException("exporter required for batch log record processor");
}
BatchLogRecordProcessorBuilder builder = BatchLogRecordProcessor.builder(logRecordExporter);
if (batchModel.getExportTimeout() != null) {
builder.setExporterTimeout(Duration.ofMillis(batchModel.getExportTimeout()));
Expand All @@ -71,12 +64,11 @@ public LogRecordProcessor create(
io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.SimpleLogRecordProcessor
simpleModel = model.getSimple();
if (simpleModel != null) {
LogRecordExporter exporterModel = simpleModel.getExporter();
LogRecordExporter exporterModel =
FileConfigUtil.requireNonNull(
simpleModel.getExporter(), "simple log record processor exporter");
io.opentelemetry.sdk.logs.export.LogRecordExporter logRecordExporter =
LogRecordExporterFactory.getInstance().create(exporterModel, spiHelper, closeables);
if (logRecordExporter == null) {
throw new ConfigurationException("exporter required for simple log record processor");
}
return FileConfigUtil.addAndReturn(
closeables, SimpleLogRecordProcessor.create(logRecordExporter));
}
Expand All @@ -86,8 +78,8 @@ public LogRecordProcessor create(
throw new ConfigurationException(
"Unrecognized log record processor(s): "
+ model.getAdditionalProperties().keySet().stream().collect(joining(",", "[", "]")));
} else {
throw new ConfigurationException("log processor must be set");
}

return LogRecordProcessor.composite();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;
import java.io.Closeable;
import java.util.List;
import javax.annotation.Nullable;

final class LoggerProviderFactory
implements Factory<LoggerProviderAndAttributeLimits, SdkLoggerProviderBuilder> {
Expand All @@ -28,13 +27,9 @@ static LoggerProviderFactory getInstance() {

@Override
public SdkLoggerProviderBuilder create(
@Nullable LoggerProviderAndAttributeLimits model,
SpiHelper spiHelper,
List<Closeable> closeables) {
LoggerProviderAndAttributeLimits model, SpiHelper spiHelper, List<Closeable> closeables) {
SdkLoggerProviderBuilder builder = SdkLoggerProvider.builder();
if (model == null) {
return builder;
}

LoggerProvider loggerProviderModel = model.getLoggerProvider();
if (loggerProviderModel == null) {
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@

package io.opentelemetry.sdk.extension.incubator.fileconfig;

import static io.opentelemetry.sdk.extension.incubator.fileconfig.FileConfigUtil.requireNonNull;

import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.MeterProvider;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.MetricReader;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.Selector;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.Stream;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.View;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
import java.io.Closeable;
import java.util.List;
import javax.annotation.Nullable;

final class MeterProviderFactory implements Factory<MeterProvider, SdkMeterProviderBuilder> {

Expand All @@ -27,11 +30,7 @@ static MeterProviderFactory getInstance() {

@Override
public SdkMeterProviderBuilder create(
@Nullable MeterProvider model, SpiHelper spiHelper, List<Closeable> closeables) {
if (model == null) {
return SdkMeterProvider.builder();
}

MeterProvider model, SpiHelper spiHelper, List<Closeable> closeables) {
SdkMeterProviderBuilder builder = SdkMeterProvider.builder();

List<MetricReader> readerModels = model.getReaders();
Expand All @@ -49,11 +48,13 @@ public SdkMeterProviderBuilder create(
List<View> viewModels = model.getViews();
if (viewModels != null) {
viewModels.forEach(
viewModel ->
builder.registerView(
InstrumentSelectorFactory.getInstance()
.create(viewModel.getSelector(), spiHelper, closeables),
ViewFactory.getInstance().create(viewModel.getStream(), spiHelper, closeables)));
viewModel -> {
Selector selector = requireNonNull(viewModel.getSelector(), "view selector");
Stream stream = requireNonNull(viewModel.getStream(), "view stream");
builder.registerView(
InstrumentSelectorFactory.getInstance().create(selector, spiHelper, closeables),
ViewFactory.getInstance().create(stream, spiHelper, closeables));
});
}

return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.io.Closeable;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;

final class MetricExporterFactory
implements Factory<
Expand All @@ -29,18 +28,11 @@ static MetricExporterFactory getInstance() {
return INSTANCE;
}

@SuppressWarnings("NullAway") // Override superclass non-null response
@Override
@Nullable
public MetricExporter create(
@Nullable
io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.MetricExporter model,
io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.MetricExporter model,
SpiHelper spiHelper,
List<Closeable> closeables) {
if (model == null) {
return null;
}

OtlpMetric otlpModel = model.getOtlp();
if (otlpModel != null) {
model.getAdditionalProperties().put("otlp", otlpModel);
Expand Down Expand Up @@ -74,8 +66,8 @@ public MetricExporter create(
exporterKeyValue.getKey(),
exporterKeyValue.getValue());
return FileConfigUtil.addAndReturn(closeables, metricExporter);
} else {
throw new ConfigurationException("metric exporter must be set");
}

return null;
}
}
Loading

0 comments on commit 56e7ebe

Please sign in to comment.