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

Refactor SpiUtil to improve mocking #5679

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.OpenTelemetrySdkBuilder;
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
Expand All @@ -37,7 +38,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
Expand Down Expand Up @@ -86,8 +86,8 @@ public final class AutoConfiguredOpenTelemetrySdkBuilder implements AutoConfigur
private final List<Function<ConfigProperties, Map<String, String>>> propertiesCustomizers =
new ArrayList<>();

private ClassLoader serviceClassLoader =
AutoConfiguredOpenTelemetrySdkBuilder.class.getClassLoader();
private SpiHelper spiHelper =
SpiHelper.create(AutoConfiguredOpenTelemetrySdk.class.getClassLoader());

private boolean registerShutdownHook = true;

Expand Down Expand Up @@ -311,7 +311,7 @@ public AutoConfiguredOpenTelemetrySdkBuilder setResultAsGlobal() {
public AutoConfiguredOpenTelemetrySdkBuilder setServiceClassLoader(
ClassLoader serviceClassLoader) {
requireNonNull(serviceClassLoader, "serviceClassLoader");
this.serviceClassLoader = serviceClassLoader;
this.spiHelper = SpiHelper.create(serviceClassLoader);
return this;
}

Expand All @@ -324,15 +324,15 @@ public AutoConfiguredOpenTelemetrySdk build() {
customized = true;
mergeSdkTracerProviderConfigurer();
for (AutoConfigurationCustomizerProvider customizer :
SpiUtil.loadOrdered(AutoConfigurationCustomizerProvider.class, serviceClassLoader)) {
spiHelper.loadOrdered(AutoConfigurationCustomizerProvider.class)) {
customizer.customize(this);
}
}

ConfigProperties config = getConfig();

Resource resource =
ResourceConfiguration.configureResource(config, serviceClassLoader, resourceCustomizer);
ResourceConfiguration.configureResource(config, spiHelper, resourceCustomizer);

// Track any closeable resources created throughout configuration. If an exception short
// circuits configuration, partially configured components will be closed.
Expand All @@ -346,7 +346,7 @@ public AutoConfiguredOpenTelemetrySdk build() {
SdkMeterProviderBuilder meterProviderBuilder = SdkMeterProvider.builder();
meterProviderBuilder.setResource(resource);
MeterProviderConfiguration.configureMeterProvider(
meterProviderBuilder, config, serviceClassLoader, metricExporterCustomizer, closeables);
meterProviderBuilder, config, spiHelper, metricExporterCustomizer, closeables);
meterProviderBuilder = meterProviderCustomizer.apply(meterProviderBuilder, config);
SdkMeterProvider meterProvider = meterProviderBuilder.build();
closeables.add(meterProvider);
Expand All @@ -356,7 +356,7 @@ public AutoConfiguredOpenTelemetrySdk build() {
TracerProviderConfiguration.configureTracerProvider(
tracerProviderBuilder,
config,
serviceClassLoader,
spiHelper,
meterProvider,
spanExporterCustomizer,
samplerCustomizer,
Expand All @@ -370,7 +370,7 @@ public AutoConfiguredOpenTelemetrySdk build() {
LoggerProviderConfiguration.configureLoggerProvider(
loggerProviderBuilder,
config,
serviceClassLoader,
spiHelper,
meterProvider,
logRecordExporterCustomizer,
closeables);
Expand All @@ -379,8 +379,7 @@ public AutoConfiguredOpenTelemetrySdk build() {
closeables.add(loggerProvider);

ContextPropagators propagators =
PropagatorConfiguration.configurePropagators(
config, serviceClassLoader, propagatorCustomizer);
PropagatorConfiguration.configurePropagators(config, spiHelper, propagatorCustomizer);

OpenTelemetrySdkBuilder sdkBuilder =
OpenTelemetrySdk.builder()
Expand Down Expand Up @@ -428,9 +427,8 @@ public AutoConfiguredOpenTelemetrySdk build() {
@SuppressWarnings("deprecation") // Support deprecated SdkTracerProviderConfigurer
private void mergeSdkTracerProviderConfigurer() {
for (io.opentelemetry.sdk.autoconfigure.spi.traces.SdkTracerProviderConfigurer configurer :
ServiceLoader.load(
io.opentelemetry.sdk.autoconfigure.spi.traces.SdkTracerProviderConfigurer.class,
serviceClassLoader)) {
spiHelper.load(
io.opentelemetry.sdk.autoconfigure.spi.traces.SdkTracerProviderConfigurer.class)) {
addTracerProviderCustomizer(
(builder, config) -> {
configurer.configure(builder, config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package io.opentelemetry.sdk.autoconfigure;

import io.opentelemetry.sdk.autoconfigure.internal.NamedSpiManager;
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
Expand Down Expand Up @@ -33,7 +35,7 @@ final class LogRecordExporterConfiguration {
// Visible for test
static Map<String, LogRecordExporter> configureLogRecordExporters(
ConfigProperties config,
ClassLoader serviceClassLoader,
SpiHelper spiHelper,
BiFunction<? super LogRecordExporter, ConfigProperties, ? extends LogRecordExporter>
logRecordExporterCustomizer,
List<Closeable> closeables) {
Expand All @@ -57,7 +59,7 @@ static Map<String, LogRecordExporter> configureLogRecordExporters(
}

NamedSpiManager<LogRecordExporter> spiExportersManager =
logRecordExporterSpiManager(config, serviceClassLoader);
logRecordExporterSpiManager(config, spiHelper);

Map<String, LogRecordExporter> map = new HashMap<>();
for (String exporterName : exporterNames) {
Expand All @@ -75,13 +77,12 @@ static Map<String, LogRecordExporter> configureLogRecordExporters(

// Visible for testing
static NamedSpiManager<LogRecordExporter> logRecordExporterSpiManager(
ConfigProperties config, ClassLoader serviceClassLoader) {
return SpiUtil.loadConfigurable(
ConfigProperties config, SpiHelper spiHelper) {
return spiHelper.loadConfigurable(
ConfigurableLogRecordExporterProvider.class,
ConfigurableLogRecordExporterProvider::getName,
ConfigurableLogRecordExporterProvider::createExporter,
config,
serviceClassLoader);
config);
}

// Visible for testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static io.opentelemetry.sdk.autoconfigure.LogRecordExporterConfiguration.configureLogRecordExporters;

import io.opentelemetry.api.metrics.MeterProvider;
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.logs.LogLimits;
import io.opentelemetry.sdk.logs.LogLimitsBuilder;
Expand All @@ -30,7 +31,7 @@ final class LoggerProviderConfiguration {
static void configureLoggerProvider(
SdkLoggerProviderBuilder loggerProviderBuilder,
ConfigProperties config,
ClassLoader serviceClassLoader,
SpiHelper spiHelper,
MeterProvider meterProvider,
BiFunction<? super LogRecordExporter, ConfigProperties, ? extends LogRecordExporter>
logRecordExporterCustomizer,
Expand All @@ -39,8 +40,7 @@ static void configureLoggerProvider(
loggerProviderBuilder.setLogLimits(() -> configureLogLimits(config));

Map<String, LogRecordExporter> exportersByName =
configureLogRecordExporters(
config, serviceClassLoader, logRecordExporterCustomizer, closeables);
configureLogRecordExporters(config, spiHelper, logRecordExporterCustomizer, closeables);

configureLogRecordProcessors(config, exportersByName, meterProvider, closeables)
.forEach(loggerProviderBuilder::addLogRecordProcessor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.sdk.autoconfigure;

import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
Expand All @@ -27,7 +28,7 @@ final class MeterProviderConfiguration {
static void configureMeterProvider(
SdkMeterProviderBuilder meterProviderBuilder,
ConfigProperties config,
ClassLoader serviceClassLoader,
SpiHelper spiHelper,
BiFunction<? super MetricExporter, ConfigProperties, ? extends MetricExporter>
metricExporterCustomizer,
List<Closeable> closeables) {
Expand All @@ -48,13 +49,13 @@ static void configureMeterProvider(
break;
}

configureMetricReaders(config, serviceClassLoader, metricExporterCustomizer, closeables)
configureMetricReaders(config, spiHelper, metricExporterCustomizer, closeables)
.forEach(meterProviderBuilder::registerMetricReader);
}

static List<MetricReader> configureMetricReaders(
ConfigProperties config,
ClassLoader serviceClassLoader,
SpiHelper spiHelper,
BiFunction<? super MetricExporter, ConfigProperties, ? extends MetricExporter>
metricExporterCustomizer,
List<Closeable> closeables) {
Expand All @@ -74,7 +75,7 @@ static List<MetricReader> configureMetricReaders(
.map(
exporterName ->
MetricExporterConfiguration.configureReader(
exporterName, config, serviceClassLoader, metricExporterCustomizer, closeables))
exporterName, config, spiHelper, metricExporterCustomizer, closeables))
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package io.opentelemetry.sdk.autoconfigure;

import io.opentelemetry.sdk.autoconfigure.internal.NamedSpiManager;
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider;
Expand Down Expand Up @@ -35,7 +37,7 @@ final class MetricExporterConfiguration {
static MetricReader configureReader(
String name,
ConfigProperties config,
ClassLoader serviceClassLoader,
SpiHelper spiHelper,
BiFunction<? super MetricExporter, ConfigProperties, ? extends MetricExporter>
metricExporterCustomizer,
List<Closeable> closeables) {
Expand All @@ -53,7 +55,7 @@ static MetricReader configureReader(
}

NamedSpiManager<MetricExporter> spiExportersManager =
metricExporterSpiManager(config, serviceClassLoader);
metricExporterSpiManager(config, spiHelper);

MetricExporter metricExporter = configureExporter(name, spiExportersManager);
closeables.add(metricExporter);
Expand All @@ -73,13 +75,12 @@ static MetricReader configureReader(

// Visible for testing
static NamedSpiManager<MetricExporter> metricExporterSpiManager(
ConfigProperties config, ClassLoader serviceClassLoader) {
return SpiUtil.loadConfigurable(
ConfigProperties config, SpiHelper spiHelper) {
return spiHelper.loadConfigurable(
ConfigurableMetricExporterProvider.class,
ConfigurableMetricExporterProvider::getName,
ConfigurableMetricExporterProvider::createExporter,
config,
serviceClassLoader);
config);
}

// Visible for testing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.sdk.autoconfigure.internal.NamedSpiManager;
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurablePropagatorProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
Expand All @@ -24,19 +26,18 @@ final class PropagatorConfiguration {

static ContextPropagators configurePropagators(
ConfigProperties config,
ClassLoader serviceClassLoader,
SpiHelper spiHelper,
BiFunction<? super TextMapPropagator, ConfigProperties, ? extends TextMapPropagator>
propagatorCustomizer) {
Set<TextMapPropagator> propagators = new LinkedHashSet<>();
List<String> requestedPropagators = config.getList("otel.propagators", DEFAULT_PROPAGATORS);

NamedSpiManager<TextMapPropagator> spiPropagatorsManager =
SpiUtil.loadConfigurable(
spiHelper.loadConfigurable(
ConfigurablePropagatorProvider.class,
ConfigurablePropagatorProvider::getName,
ConfigurablePropagatorProvider::getPropagator,
config,
serviceClassLoader);
config);

if (requestedPropagators.contains("none")) {
if (requestedPropagators.size() > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
Expand Down Expand Up @@ -80,16 +81,15 @@ public static Resource createEnvironmentResource(ConfigProperties config) {

static Resource configureResource(
ConfigProperties config,
ClassLoader serviceClassLoader,
SpiHelper spiHelper,
BiFunction<? super Resource, ConfigProperties, ? extends Resource> resourceCustomizer) {
Resource result = Resource.getDefault();

Set<String> enabledProviders =
new HashSet<>(config.getList("otel.java.enabled.resource.providers"));
Set<String> disabledProviders =
new HashSet<>(config.getList("otel.java.disabled.resource.providers"));
for (ResourceProvider resourceProvider :
SpiUtil.loadOrdered(ResourceProvider.class, serviceClassLoader)) {
for (ResourceProvider resourceProvider : spiHelper.loadOrdered(ResourceProvider.class)) {
if (!enabledProviders.isEmpty()
&& !enabledProviders.contains(resourceProvider.getClass().getName())) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package io.opentelemetry.sdk.autoconfigure;

import io.opentelemetry.sdk.autoconfigure.internal.NamedSpiManager;
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
Expand Down Expand Up @@ -35,7 +37,7 @@ final class SpanExporterConfiguration {
// Visible for testing
static Map<String, SpanExporter> configureSpanExporters(
ConfigProperties config,
ClassLoader serviceClassLoader,
SpiHelper spiHelper,
BiFunction<? super SpanExporter, ConfigProperties, ? extends SpanExporter>
spanExporterCustomizer,
List<Closeable> closeables) {
Expand All @@ -58,8 +60,7 @@ static Map<String, SpanExporter> configureSpanExporters(
exporterNames = Collections.singleton("otlp");
}

NamedSpiManager<SpanExporter> spiExportersManager =
spanExporterSpiManager(config, serviceClassLoader);
NamedSpiManager<SpanExporter> spiExportersManager = spanExporterSpiManager(config, spiHelper);

Map<String, SpanExporter> map = new HashMap<>();
for (String exporterName : exporterNames) {
Expand All @@ -76,13 +77,12 @@ static Map<String, SpanExporter> configureSpanExporters(

// Visible for testing
static NamedSpiManager<SpanExporter> spanExporterSpiManager(
ConfigProperties config, ClassLoader serviceClassLoader) {
return SpiUtil.loadConfigurable(
ConfigProperties config, SpiHelper spiHelper) {
return spiHelper.loadConfigurable(
ConfigurableSpanExporterProvider.class,
ConfigurableSpanExporterProvider::getName,
ConfigurableSpanExporterProvider::createExporter,
config,
serviceClassLoader);
config);
}

// Visible for testing
Expand Down
Loading