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

Autoconfigure listener #5931

Merged
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
@@ -1,2 +1,13 @@
Comparing source compatibility of against
No changes.
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporterBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporterBuilder setMeterProvider(java.util.function.Supplier<io.opentelemetry.api.metrics.MeterProvider>)
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder setMeterProvider(java.util.function.Supplier<io.opentelemetry.api.metrics.MeterProvider>)
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporterBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporterBuilder setMeterProvider(java.util.function.Supplier<io.opentelemetry.api.metrics.MeterProvider>)
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder setMeterProvider(java.util.function.Supplier<io.opentelemetry.api.metrics.MeterProvider>)
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ public GrpcExporterBuilder<T> setRetryPolicy(RetryPolicy retryPolicy) {
return this;
}

public GrpcExporterBuilder<T> setMeterProvider(MeterProvider meterProvider) {
this.meterProviderSupplier = () -> meterProvider;
public GrpcExporterBuilder<T> setMeterProvider(Supplier<MeterProvider> meterProviderSupplier) {
this.meterProviderSupplier = meterProviderSupplier;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ public HttpExporterBuilder<T> setSslContext(
return this;
}

public HttpExporterBuilder<T> setMeterProvider(MeterProvider meterProvider) {
this.meterProviderSupplier = () -> meterProvider;
public HttpExporterBuilder<T> setMeterProvider(Supplier<MeterProvider> meterProviderSupplier) {
this.meterProviderSupplier = meterProviderSupplier;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public JaegerGrpcSpanExporterBuilder setSslContext(
*/
public JaegerGrpcSpanExporterBuilder setMeterProvider(MeterProvider meterProvider) {
requireNonNull(meterProvider, "meterProvider");
delegate.setMeterProvider(meterProvider);
delegate.setMeterProvider(() -> meterProvider);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.opentelemetry.sdk.common.export.RetryPolicy;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;

Expand Down Expand Up @@ -135,7 +136,18 @@
*/
public OtlpHttpLogRecordExporterBuilder setMeterProvider(MeterProvider meterProvider) {
requireNonNull(meterProvider, "meterProvider");
delegate.setMeterProvider(meterProvider);
setMeterProvider(() -> meterProvider);
return this;

Check warning on line 140 in exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/http/logs/OtlpHttpLogRecordExporterBuilder.java

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/http/logs/OtlpHttpLogRecordExporterBuilder.java#L139-L140

Added lines #L139 - L140 were not covered by tests
}

/**
* Sets the {@link MeterProvider} supplier used to collect metrics related to export. If not set,
* uses {@link GlobalOpenTelemetry#getMeterProvider()}.
*/
public OtlpHttpLogRecordExporterBuilder setMeterProvider(
Supplier<MeterProvider> meterProviderSupplier) {
requireNonNull(meterProviderSupplier, "meterProviderSupplier");
delegate.setMeterProvider(meterProviderSupplier);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final class OtlpHttpMetricExporterBuilder {

OtlpHttpMetricExporterBuilder(HttpExporterBuilder<MetricsRequestMarshaler> delegate) {
this.delegate = delegate;
delegate.setMeterProvider(MeterProvider.noop());
delegate.setMeterProvider(MeterProvider::noop);
OtlpUserAgent.addUserAgentHeader(delegate::addHeader);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.opentelemetry.sdk.common.export.RetryPolicy;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;

Expand Down Expand Up @@ -136,7 +137,18 @@
*/
public OtlpHttpSpanExporterBuilder setMeterProvider(MeterProvider meterProvider) {
requireNonNull(meterProvider, "meterProvider");
delegate.setMeterProvider(meterProvider);
setMeterProvider(() -> meterProvider);
return this;

Check warning on line 141 in exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/http/trace/OtlpHttpSpanExporterBuilder.java

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/http/trace/OtlpHttpSpanExporterBuilder.java#L140-L141

Added lines #L140 - L141 were not covered by tests
}

/**
* Sets the {@link MeterProvider} supplier to use to collect metrics related to export. If not
* set, uses {@link GlobalOpenTelemetry#getMeterProvider()}.
*/
public OtlpHttpSpanExporterBuilder setMeterProvider(
Supplier<MeterProvider> meterProviderSupplier) {
requireNonNull(meterProviderSupplier, "meterProviderSupplier");
delegate.setMeterProvider(meterProviderSupplier);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
import static io.opentelemetry.exporter.otlp.internal.OtlpConfigUtil.PROTOCOL_GRPC;
import static io.opentelemetry.exporter.otlp.internal.OtlpConfigUtil.PROTOCOL_HTTP_PROTOBUF;

import io.opentelemetry.api.metrics.MeterProvider;
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter;
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporterBuilder;
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter;
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporterBuilder;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.autoconfigure.spi.internal.AutoConfigureListener;
import io.opentelemetry.sdk.autoconfigure.spi.logs.ConfigurableLogRecordExporterProvider;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import java.util.concurrent.atomic.AtomicReference;

/**
* {@link LogRecordExporter} SPI implementation for {@link OtlpGrpcLogRecordExporter} and {@link
Expand All @@ -25,7 +29,12 @@
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public class OtlpLogRecordExporterProvider implements ConfigurableLogRecordExporterProvider {
public class OtlpLogRecordExporterProvider
implements ConfigurableLogRecordExporterProvider, AutoConfigureListener {

private final AtomicReference<MeterProvider> meterProviderRef =
new AtomicReference<>(MeterProvider.noop());

@Override
public LogRecordExporter createExporter(ConfigProperties config) {
String protocol = OtlpConfigUtil.getOtlpProtocol(DATA_TYPE_LOGS, config);
Expand All @@ -43,6 +52,7 @@ public LogRecordExporter createExporter(ConfigProperties config) {
builder::setTrustedCertificates,
builder::setClientTls,
builder::setRetryPolicy);
builder.setMeterProvider(meterProviderRef::get);

return builder.build();
} else if (protocol.equals(PROTOCOL_GRPC)) {
Expand All @@ -58,6 +68,7 @@ public LogRecordExporter createExporter(ConfigProperties config) {
builder::setTrustedCertificates,
builder::setClientTls,
builder::setRetryPolicy);
builder.setMeterProvider(meterProviderRef::get);

return builder.build();
}
Expand All @@ -78,4 +89,9 @@ OtlpHttpLogRecordExporterBuilder httpBuilder() {
OtlpGrpcLogRecordExporterBuilder grpcBuilder() {
return OtlpGrpcLogRecordExporter.builder();
}

@Override
public void afterAutoConfigure(OpenTelemetrySdk sdk) {
meterProviderRef.set(sdk.getMeterProvider());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* at any time.
*/
public class OtlpMetricExporterProvider implements ConfigurableMetricExporterProvider {

@Override
public MetricExporter createExporter(ConfigProperties config) {
String protocol = OtlpConfigUtil.getOtlpProtocol(DATA_TYPE_METRICS, config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
import static io.opentelemetry.exporter.otlp.internal.OtlpConfigUtil.PROTOCOL_GRPC;
import static io.opentelemetry.exporter.otlp.internal.OtlpConfigUtil.PROTOCOL_HTTP_PROTOBUF;

import io.opentelemetry.api.metrics.MeterProvider;
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.autoconfigure.spi.internal.AutoConfigureListener;
import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import java.util.concurrent.atomic.AtomicReference;

/**
* {@link SpanExporter} SPI implementation for {@link OtlpGrpcSpanExporter} and {@link
Expand All @@ -25,7 +29,12 @@
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public class OtlpSpanExporterProvider implements ConfigurableSpanExporterProvider {
public class OtlpSpanExporterProvider
implements ConfigurableSpanExporterProvider, AutoConfigureListener {

private final AtomicReference<MeterProvider> meterProviderRef =
new AtomicReference<>(MeterProvider.noop());

@Override
public SpanExporter createExporter(ConfigProperties config) {
String protocol = OtlpConfigUtil.getOtlpProtocol(DATA_TYPE_TRACES, config);
Expand All @@ -42,6 +51,7 @@ public SpanExporter createExporter(ConfigProperties config) {
builder::setTrustedCertificates,
builder::setClientTls,
builder::setRetryPolicy);
builder.setMeterProvider(meterProviderRef::get);

return builder.build();
} else if (protocol.equals(PROTOCOL_GRPC)) {
Expand All @@ -57,6 +67,7 @@ public SpanExporter createExporter(ConfigProperties config) {
builder::setTrustedCertificates,
builder::setClientTls,
builder::setRetryPolicy);
builder.setMeterProvider(meterProviderRef::get);

return builder.build();
}
Expand All @@ -77,4 +88,9 @@ OtlpHttpSpanExporterBuilder httpBuilder() {
OtlpGrpcSpanExporterBuilder grpcBuilder() {
return OtlpGrpcSpanExporter.builder();
}

@Override
public void afterAutoConfigure(OpenTelemetrySdk sdk) {
meterProviderRef.set(sdk.getMeterProvider());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.net.URI;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;

Expand Down Expand Up @@ -179,7 +180,18 @@
*/
public OtlpGrpcLogRecordExporterBuilder setMeterProvider(MeterProvider meterProvider) {
requireNonNull(meterProvider, "meterProvider");
delegate.setMeterProvider(meterProvider);
setMeterProvider(() -> meterProvider);
return this;

Check warning on line 184 in exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporterBuilder.java

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporterBuilder.java#L183-L184

Added lines #L183 - L184 were not covered by tests
}

/**
* Sets the {@link MeterProvider} supplier used to collect metrics related to export. If not set,
* uses {@link GlobalOpenTelemetry#getMeterProvider()}.
*/
public OtlpGrpcLogRecordExporterBuilder setMeterProvider(
Supplier<MeterProvider> meterProviderSupplier) {
requireNonNull(meterProviderSupplier, "meterProviderSupplier");
delegate.setMeterProvider(meterProviderSupplier);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public final class OtlpGrpcMetricExporterBuilder {

OtlpGrpcMetricExporterBuilder(GrpcExporterBuilder<MetricsRequestMarshaler> delegate) {
this.delegate = delegate;
delegate.setMeterProvider(MeterProvider.noop());
delegate.setMeterProvider(MeterProvider::noop);
OtlpUserAgent.addUserAgentHeader(delegate::addHeader);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.net.URI;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;

Expand Down Expand Up @@ -176,7 +177,18 @@
*/
public OtlpGrpcSpanExporterBuilder setMeterProvider(MeterProvider meterProvider) {
requireNonNull(meterProvider, "meterProvider");
delegate.setMeterProvider(meterProvider);
setMeterProvider(() -> meterProvider);
return this;

Check warning on line 181 in exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcSpanExporterBuilder.java

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcSpanExporterBuilder.java#L180-L181

Added lines #L180 - L181 were not covered by tests
}

/**
* Sets the {@link MeterProvider} supplier used to collect metrics related to export. If not set,
* uses {@link GlobalOpenTelemetry#getMeterProvider()}.
*/
public OtlpGrpcSpanExporterBuilder setMeterProvider(
Supplier<MeterProvider> meterProviderSupplier) {
requireNonNull(meterProviderSupplier, "meterProviderSupplier");
delegate.setMeterProvider(meterProviderSupplier);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private void setupSdk() {
// set up the span exporter and wire it into the SDK
OtlpGrpcSpanExporter spanExporter =
OtlpGrpcSpanExporter.builder()
.setMeterProvider(meterProvider)
.setMeterProvider(() -> meterProvider)
.setEndpoint(
"http://"
+ toxiproxyContainer.getHost()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.autoconfigure.spi.internal;

import io.opentelemetry.sdk.OpenTelemetrySdk;

/**
* Interface to be extended by SPIs that require access to the autoconfigured {@link
* OpenTelemetrySdk} instance.
*
* <p>This is not a standalone SPI. Instead, implementations of other SPIs can also implement this
* interface to receive a callback with the configured SDK.
*/
public interface AutoConfigureListener {

void afterAutoConfigure(OpenTelemetrySdk sdk);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.autoconfigure.spi.internal.AutoConfigureListener;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;
Expand Down Expand Up @@ -405,6 +406,7 @@

maybeRegisterShutdownHook(openTelemetrySdk);
maybeSetAsGlobal(openTelemetrySdk);
callAutoConfigureListeners(spiHelper, openTelemetrySdk);

return AutoConfiguredOpenTelemetrySdk.create(openTelemetrySdk, resource, config);
} catch (RuntimeException e) {
Expand Down Expand Up @@ -478,6 +480,18 @@
Level.FINE, "Global OpenTelemetry set to {0} by autoconfiguration", openTelemetrySdk);
}

// Visible for testing
void callAutoConfigureListeners(SpiHelper spiHelper, OpenTelemetrySdk openTelemetrySdk) {
for (AutoConfigureListener listener : spiHelper.getListeners()) {
try {
listener.afterAutoConfigure(openTelemetrySdk);
} catch (Throwable throwable) {
logger.log(
Level.WARNING, "Error invoking listener " + listener.getClass().getName(), throwable);

Check warning on line 490 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkBuilder.java

View check run for this annotation

Codecov / codecov/patch

sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkBuilder.java#L488-L490

Added lines #L488 - L490 were not covered by tests
}
}
}

@SuppressWarnings("deprecation") // Support deprecated SdkTracerProviderConfigurer
private void mergeSdkTracerProviderConfigurer() {
for (io.opentelemetry.sdk.autoconfigure.spi.traces.SdkTracerProviderConfigurer configurer :
Expand Down
Loading