diff --git a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/ExporterMetrics.java b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/ExporterMetrics.java index 34e00160e3f..0c12f9d6fc0 100644 --- a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/ExporterMetrics.java +++ b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/ExporterMetrics.java @@ -13,6 +13,8 @@ import io.opentelemetry.api.metrics.LongCounter; import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.api.metrics.MeterProvider; +import java.util.function.Supplier; +import javax.annotation.Nullable; /** * Helper for recording metrics from exporters. @@ -25,37 +27,69 @@ public class ExporterMetrics { private static final AttributeKey ATTRIBUTE_KEY_TYPE = stringKey("type"); private static final AttributeKey ATTRIBUTE_KEY_SUCCESS = booleanKey("success"); - private final LongCounter seen; - private final LongCounter exported; - + private final Supplier meterProviderSupplier; + private final String exporterName; + private final String transportName; private final Attributes seenAttrs; private final Attributes successAttrs; private final Attributes failedAttrs; + /** Access via {@link #seen()}. */ + @Nullable private volatile LongCounter seen; + + /** Access via {@link #exported()} . */ + @Nullable private volatile LongCounter exported; + private ExporterMetrics( - MeterProvider meterProvider, String exporterName, String type, String transportName) { - Meter meter = - meterProvider.get("io.opentelemetry.exporters." + exporterName + "-" + transportName); - seenAttrs = Attributes.builder().put(ATTRIBUTE_KEY_TYPE, type).build(); - seen = meter.counterBuilder(exporterName + ".exporter.seen").build(); - exported = meter.counterBuilder(exporterName + ".exporter.exported").build(); - successAttrs = seenAttrs.toBuilder().put(ATTRIBUTE_KEY_SUCCESS, true).build(); - failedAttrs = seenAttrs.toBuilder().put(ATTRIBUTE_KEY_SUCCESS, false).build(); + Supplier meterProviderSupplier, + String exporterName, + String type, + String transportName) { + this.meterProviderSupplier = meterProviderSupplier; + this.exporterName = exporterName; + this.transportName = transportName; + this.seenAttrs = Attributes.builder().put(ATTRIBUTE_KEY_TYPE, type).build(); + this.successAttrs = this.seenAttrs.toBuilder().put(ATTRIBUTE_KEY_SUCCESS, true).build(); + this.failedAttrs = this.seenAttrs.toBuilder().put(ATTRIBUTE_KEY_SUCCESS, false).build(); } /** Record number of records seen. */ public void addSeen(long value) { - seen.add(value, seenAttrs); + seen().add(value, seenAttrs); } /** Record number of records which successfully exported. */ public void addSuccess(long value) { - exported.add(value, successAttrs); + exported().add(value, successAttrs); } /** Record number of records which failed to export. */ public void addFailed(long value) { - exported.add(value, failedAttrs); + exported().add(value, failedAttrs); + } + + private LongCounter seen() { + LongCounter seen = this.seen; + if (seen == null) { + seen = meter().counterBuilder(exporterName + ".exporter.seen").build(); + this.seen = seen; + } + return seen; + } + + private LongCounter exported() { + LongCounter exported = this.exported; + if (exported == null) { + exported = meter().counterBuilder(exporterName + ".exporter.exported").build(); + this.exported = exported; + } + return exported; + } + + private Meter meter() { + return meterProviderSupplier + .get() + .get("io.opentelemetry.exporters." + exporterName + "-" + transportName); } /** @@ -63,7 +97,7 @@ public void addFailed(long value) { * "io.opentelemetry.exporters." + exporterName + "-grpc}". */ public static ExporterMetrics createGrpc( - String exporterName, String type, MeterProvider meterProvider) { + String exporterName, String type, Supplier meterProvider) { return new ExporterMetrics(meterProvider, exporterName, type, "grpc"); } @@ -72,7 +106,7 @@ public static ExporterMetrics createGrpc( * "io.opentelemetry.exporters." + exporterName + "-grpc-okhttp}". */ public static ExporterMetrics createGrpcOkHttp( - String exporterName, String type, MeterProvider meterProvider) { + String exporterName, String type, Supplier meterProvider) { return new ExporterMetrics(meterProvider, exporterName, type, "grpc-okhttp"); } @@ -81,7 +115,7 @@ public static ExporterMetrics createGrpcOkHttp( * "io.opentelemetry.exporters." + exporterName + "-http}". */ public static ExporterMetrics createHttpProtobuf( - String exporterName, String type, MeterProvider meterProvider) { + String exporterName, String type, Supplier meterProvider) { return new ExporterMetrics(meterProvider, exporterName, type, "http"); } @@ -90,7 +124,7 @@ public static ExporterMetrics createHttpProtobuf( * "io.opentelemetry.exporters." + exporterName + "-http-json}". */ public static ExporterMetrics createHttpJson( - String exporterName, String type, MeterProvider meterProvider) { + String exporterName, String type, Supplier meterProvider) { return new ExporterMetrics(meterProvider, exporterName, type, "http-json"); } } diff --git a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/GrpcExporterBuilder.java b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/GrpcExporterBuilder.java index 48fcbe470f1..b77d825e324 100644 --- a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/GrpcExporterBuilder.java +++ b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/GrpcExporterBuilder.java @@ -11,6 +11,7 @@ import io.grpc.ManagedChannel; import io.grpc.Metadata; import io.grpc.stub.MetadataUtils; +import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.exporter.internal.ExporterBuilderUtil; import io.opentelemetry.exporter.internal.TlsUtil; @@ -58,7 +59,7 @@ public class GrpcExporterBuilder { @Nullable private byte[] privateKeyPem; @Nullable private byte[] certificatePem; @Nullable private RetryPolicy retryPolicy; - private MeterProvider meterProvider = MeterProvider.noop(); + private Supplier meterProviderSupplier = GlobalOpenTelemetry::getMeterProvider; // Use Object type since gRPC may not be on the classpath. @Nullable private Object grpcChannel; @@ -124,7 +125,7 @@ public GrpcExporterBuilder setRetryPolicy(RetryPolicy retryPolicy) { } public GrpcExporterBuilder setMeterProvider(MeterProvider meterProvider) { - this.meterProvider = meterProvider; + this.meterProviderSupplier = () -> meterProvider; return this; } @@ -177,7 +178,7 @@ public GrpcExporter build() { exporterName, type, clientBuilder.build(), - meterProvider, + meterProviderSupplier, endpoint, headers.build(), compressionEnabled); @@ -209,7 +210,8 @@ private GrpcExporter buildWithChannel(Channel channel) { .get() .apply(channel, authorityOverride) .withCompression(codec.getMessageEncoding()); - return new UpstreamGrpcExporter<>(exporterName, type, stub, meterProvider, timeoutNanos); + return new UpstreamGrpcExporter<>( + exporterName, type, stub, meterProviderSupplier, timeoutNanos); } } } diff --git a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/OkHttpGrpcExporter.java b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/OkHttpGrpcExporter.java index 6a19265f053..73245a35b44 100644 --- a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/OkHttpGrpcExporter.java +++ b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/OkHttpGrpcExporter.java @@ -33,6 +33,7 @@ import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Supplier; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Nullable; @@ -75,12 +76,13 @@ public final class OkHttpGrpcExporter implements GrpcExport String exporterName, String type, OkHttpClient client, - MeterProvider meterProvider, + Supplier meterProviderSupplier, String endpoint, Headers headers, boolean compressionEnabled) { this.type = type; - this.exporterMetrics = ExporterMetrics.createGrpcOkHttp(exporterName, type, meterProvider); + this.exporterMetrics = + ExporterMetrics.createGrpcOkHttp(exporterName, type, meterProviderSupplier); this.client = client; this.url = HttpUrl.get(endpoint); this.headers = headers; diff --git a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/UpstreamGrpcExporter.java b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/UpstreamGrpcExporter.java index 31c86f20b58..cc114f01840 100644 --- a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/UpstreamGrpcExporter.java +++ b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/UpstreamGrpcExporter.java @@ -16,6 +16,7 @@ import io.opentelemetry.sdk.internal.ThrottlingLogger; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Supplier; import java.util.logging.Level; import java.util.logging.Logger; import org.checkerframework.checker.nullness.qual.Nullable; @@ -46,10 +47,10 @@ public final class UpstreamGrpcExporter implements GrpcExpo String exporterName, String type, MarshalerServiceStub stub, - MeterProvider meterProvider, + Supplier meterProviderSupplier, long timeoutNanos) { this.type = type; - this.exporterMetrics = ExporterMetrics.createGrpc(exporterName, type, meterProvider); + this.exporterMetrics = ExporterMetrics.createGrpc(exporterName, type, meterProviderSupplier); this.timeoutNanos = timeoutNanos; this.stub = stub; } diff --git a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/okhttp/OkHttpExporter.java b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/okhttp/OkHttpExporter.java index 985adaa4a22..ac9ab10422c 100644 --- a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/okhttp/OkHttpExporter.java +++ b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/okhttp/OkHttpExporter.java @@ -14,6 +14,7 @@ import io.opentelemetry.sdk.internal.ThrottlingLogger; import java.io.IOException; import java.util.function.Function; +import java.util.function.Supplier; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Nullable; @@ -57,7 +58,7 @@ public final class OkHttpExporter { String exporterName, String type, OkHttpClient client, - MeterProvider meterProvider, + Supplier meterProviderSupplier, String endpoint, @Nullable Headers headers, boolean compressionEnabled, @@ -70,8 +71,8 @@ public final class OkHttpExporter { this.requestBodyCreator = exportAsJson ? JsonRequestBody::new : ProtoRequestBody::new; this.exporterMetrics = exportAsJson - ? ExporterMetrics.createHttpJson(exporterName, type, meterProvider) - : ExporterMetrics.createHttpProtobuf(exporterName, type, meterProvider); + ? ExporterMetrics.createHttpJson(exporterName, type, meterProviderSupplier) + : ExporterMetrics.createHttpProtobuf(exporterName, type, meterProviderSupplier); } public CompletableResultCode export(T exportRequest, int numItems) { diff --git a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/okhttp/OkHttpExporterBuilder.java b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/okhttp/OkHttpExporterBuilder.java index f1fa38b4391..37c7090fc0e 100644 --- a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/okhttp/OkHttpExporterBuilder.java +++ b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/okhttp/OkHttpExporterBuilder.java @@ -5,6 +5,7 @@ package io.opentelemetry.exporter.internal.okhttp; +import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.exporter.internal.ExporterBuilderUtil; import io.opentelemetry.exporter.internal.TlsUtil; @@ -15,6 +16,7 @@ import java.net.URI; import java.time.Duration; import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; import javax.annotation.Nullable; import javax.net.ssl.SSLException; import javax.net.ssl.X509KeyManager; @@ -46,7 +48,7 @@ public final class OkHttpExporterBuilder { @Nullable private byte[] privateKeyPem; @Nullable private byte[] certificatePem; @Nullable private RetryPolicy retryPolicy; - private MeterProvider meterProvider = MeterProvider.noop(); + private Supplier meterProviderSupplier = GlobalOpenTelemetry::getMeterProvider; @Nullable private Authenticator authenticator; public OkHttpExporterBuilder(String exporterName, String type, String defaultEndpoint) { @@ -101,7 +103,7 @@ public OkHttpExporterBuilder setClientTls(byte[] privateKeyPem, byte[] certif } public OkHttpExporterBuilder setMeterProvider(MeterProvider meterProvider) { - this.meterProvider = meterProvider; + this.meterProviderSupplier = () -> meterProvider; return this; } @@ -158,7 +160,7 @@ public OkHttpExporter build() { exporterName, type, clientBuilder.build(), - meterProvider, + meterProviderSupplier, endpoint, headers, compressionEnabled, diff --git a/exporters/jaeger/src/main/java/io/opentelemetry/exporter/jaeger/JaegerGrpcSpanExporterBuilder.java b/exporters/jaeger/src/main/java/io/opentelemetry/exporter/jaeger/JaegerGrpcSpanExporterBuilder.java index 8d61b9590f3..c30ad9f1651 100644 --- a/exporters/jaeger/src/main/java/io/opentelemetry/exporter/jaeger/JaegerGrpcSpanExporterBuilder.java +++ b/exporters/jaeger/src/main/java/io/opentelemetry/exporter/jaeger/JaegerGrpcSpanExporterBuilder.java @@ -9,6 +9,7 @@ import static java.util.Objects.requireNonNull; import io.grpc.ManagedChannel; +import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.exporter.internal.grpc.GrpcExporter; import io.opentelemetry.exporter.internal.grpc.GrpcExporterBuilder; @@ -119,8 +120,8 @@ public JaegerGrpcSpanExporterBuilder setClientTls(byte[] privateKeyPem, byte[] c } /** - * Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, metrics - * will not be collected. + * Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, uses + * {@link GlobalOpenTelemetry#getMeterProvider()}. * * @since 1.15.0 */ diff --git a/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/http/metrics/OtlpHttpMetricExporterBuilder.java b/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/http/metrics/OtlpHttpMetricExporterBuilder.java index 03b1ea0e737..83c19c14534 100644 --- a/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/http/metrics/OtlpHttpMetricExporterBuilder.java +++ b/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/http/metrics/OtlpHttpMetricExporterBuilder.java @@ -8,6 +8,7 @@ import static io.opentelemetry.api.internal.Utils.checkArgument; import static java.util.Objects.requireNonNull; +import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.exporter.internal.okhttp.OkHttpExporterBuilder; import io.opentelemetry.exporter.internal.otlp.OtlpUserAgent; import io.opentelemetry.exporter.internal.otlp.metrics.MetricsRequestMarshaler; @@ -39,6 +40,7 @@ public final class OtlpHttpMetricExporterBuilder { OtlpHttpMetricExporterBuilder() { delegate = new OkHttpExporterBuilder<>("otlp", "metric", DEFAULT_ENDPOINT); + delegate.setMeterProvider(MeterProvider.noop()); OtlpUserAgent.addUserAgentHeader(delegate::addHeader); } diff --git a/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/http/trace/OtlpHttpSpanExporterBuilder.java b/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/http/trace/OtlpHttpSpanExporterBuilder.java index 0f268cbb83e..6ce05a36482 100644 --- a/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/http/trace/OtlpHttpSpanExporterBuilder.java +++ b/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/http/trace/OtlpHttpSpanExporterBuilder.java @@ -8,6 +8,7 @@ import static io.opentelemetry.api.internal.Utils.checkArgument; import static java.util.Objects.requireNonNull; +import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.exporter.internal.okhttp.OkHttpExporterBuilder; import io.opentelemetry.exporter.internal.otlp.OtlpUserAgent; @@ -100,8 +101,8 @@ public OtlpHttpSpanExporterBuilder setClientTls(byte[] privateKeyPem, byte[] cer } /** - * Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, metrics - * will not be collected. + * Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, uses + * {@link GlobalOpenTelemetry#getMeterProvider()}. */ public OtlpHttpSpanExporterBuilder setMeterProvider(MeterProvider meterProvider) { requireNonNull(meterProvider, "meterProvider"); diff --git a/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcMetricExporterBuilder.java b/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcMetricExporterBuilder.java index d87a79c76ad..3bc796b36e9 100644 --- a/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcMetricExporterBuilder.java +++ b/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcMetricExporterBuilder.java @@ -9,6 +9,7 @@ import static java.util.Objects.requireNonNull; import io.grpc.ManagedChannel; +import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.exporter.internal.grpc.GrpcExporter; import io.opentelemetry.exporter.internal.grpc.GrpcExporterBuilder; import io.opentelemetry.exporter.internal.otlp.OtlpUserAgent; @@ -57,6 +58,7 @@ public final class OtlpGrpcMetricExporterBuilder { DEFAULT_ENDPOINT, () -> MarshalerMetricsServiceGrpc::newFutureStub, GRPC_ENDPOINT_PATH); + delegate.setMeterProvider(MeterProvider.noop()); OtlpUserAgent.addUserAgentHeader(delegate::addHeader); } diff --git a/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcSpanExporterBuilder.java b/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcSpanExporterBuilder.java index b8e376326a8..505f8e56dcc 100644 --- a/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcSpanExporterBuilder.java +++ b/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcSpanExporterBuilder.java @@ -9,6 +9,7 @@ import static java.util.Objects.requireNonNull; import io.grpc.ManagedChannel; +import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.exporter.internal.grpc.GrpcExporter; import io.opentelemetry.exporter.internal.grpc.GrpcExporterBuilder; @@ -141,8 +142,8 @@ public OtlpGrpcSpanExporterBuilder addHeader(String key, String value) { } /** - * Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, metrics - * will not be collected. + * Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, uses + * {@link GlobalOpenTelemetry#getMeterProvider()}. */ public OtlpGrpcSpanExporterBuilder setMeterProvider(MeterProvider meterProvider) { requireNonNull(meterProvider, "meterProvider"); diff --git a/exporters/otlp/logs/src/main/java/io/opentelemetry/exporter/otlp/http/logs/OtlpHttpLogRecordExporterBuilder.java b/exporters/otlp/logs/src/main/java/io/opentelemetry/exporter/otlp/http/logs/OtlpHttpLogRecordExporterBuilder.java index 3357c8f9e5a..4a201104e8d 100644 --- a/exporters/otlp/logs/src/main/java/io/opentelemetry/exporter/otlp/http/logs/OtlpHttpLogRecordExporterBuilder.java +++ b/exporters/otlp/logs/src/main/java/io/opentelemetry/exporter/otlp/http/logs/OtlpHttpLogRecordExporterBuilder.java @@ -8,6 +8,7 @@ import static io.opentelemetry.api.internal.Utils.checkArgument; import static java.util.Objects.requireNonNull; +import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.exporter.internal.okhttp.OkHttpExporterBuilder; import io.opentelemetry.exporter.internal.otlp.OtlpUserAgent; @@ -97,8 +98,8 @@ public OtlpHttpLogRecordExporterBuilder setClientTls( } /** - * Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, metrics - * will not be collected. + * Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, uses + * {@link GlobalOpenTelemetry#getMeterProvider()}. */ public OtlpHttpLogRecordExporterBuilder setMeterProvider(MeterProvider meterProvider) { requireNonNull(meterProvider, "meterProvider"); diff --git a/exporters/otlp/logs/src/main/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporterBuilder.java b/exporters/otlp/logs/src/main/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporterBuilder.java index b9ee0ac7f0c..2802bbdc5f0 100644 --- a/exporters/otlp/logs/src/main/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporterBuilder.java +++ b/exporters/otlp/logs/src/main/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporterBuilder.java @@ -9,6 +9,7 @@ import static java.util.Objects.requireNonNull; import io.grpc.ManagedChannel; +import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.exporter.internal.grpc.GrpcExporter; import io.opentelemetry.exporter.internal.grpc.GrpcExporterBuilder; @@ -142,8 +143,8 @@ public OtlpGrpcLogRecordExporterBuilder addHeader(String key, String value) { } /** - * Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, metrics - * will not be collected. + * Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, uses + * {@link GlobalOpenTelemetry#getMeterProvider()}. */ public OtlpGrpcLogRecordExporterBuilder setMeterProvider(MeterProvider meterProvider) { requireNonNull(meterProvider, "meterProvider"); diff --git a/exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporter.java b/exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporter.java index 36596c9f105..1df90b98b62 100644 --- a/exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporter.java +++ b/exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporter.java @@ -15,6 +15,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.function.Supplier; import java.util.logging.Level; import java.util.logging.Logger; import zipkin2.Callback; @@ -44,14 +45,14 @@ public final class ZipkinSpanExporter implements SpanExporter { ZipkinSpanExporter( BytesEncoder encoder, Sender sender, - MeterProvider meterProvider, + Supplier meterProviderSupplier, OtelToZipkinSpanTransformer transformer) { this.encoder = encoder; this.sender = sender; this.exporterMetrics = sender.encoding() == Encoding.JSON - ? ExporterMetrics.createHttpJson("zipkin", "span", meterProvider) - : ExporterMetrics.createHttpProtobuf("zipkin", "span", meterProvider); + ? ExporterMetrics.createHttpJson("zipkin", "span", meterProviderSupplier) + : ExporterMetrics.createHttpProtobuf("zipkin", "span", meterProviderSupplier); this.transformer = transformer; } diff --git a/exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporterBuilder.java b/exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporterBuilder.java index a1fbf06f64e..c47745b1351 100644 --- a/exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporterBuilder.java +++ b/exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporterBuilder.java @@ -8,6 +8,7 @@ import static io.opentelemetry.api.internal.Utils.checkArgument; import static java.util.Objects.requireNonNull; +import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.metrics.MeterProvider; import java.net.InetAddress; import java.time.Duration; @@ -30,7 +31,7 @@ public final class ZipkinSpanExporterBuilder { // which is created when no custom sender is set (see OkHttpSender.Builder) private boolean compressionEnabled = true; private long readTimeoutMillis = TimeUnit.SECONDS.toMillis(10); - private MeterProvider meterProvider = MeterProvider.noop(); + private Supplier meterProviderSupplier = GlobalOpenTelemetry::getMeterProvider; /** * Sets the Zipkin sender. Implements the client side of the span transport. An {@link @@ -138,15 +139,15 @@ public ZipkinSpanExporterBuilder setReadTimeout(Duration timeout) { } /** - * Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, metrics - * will not be collected. + * Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, uses + * {@link GlobalOpenTelemetry#getMeterProvider()}. * * @return this. * @since 1.17.0 */ public ZipkinSpanExporterBuilder setMeterProvider(MeterProvider meterProvider) { requireNonNull(meterProvider, "meterProvider"); - this.meterProvider = meterProvider; + this.meterProviderSupplier = () -> meterProvider; return this; } @@ -167,6 +168,6 @@ public ZipkinSpanExporter build() { } OtelToZipkinSpanTransformer transformer = OtelToZipkinSpanTransformer.create(localIpAddressSupplier); - return new ZipkinSpanExporter(encoder, sender, meterProvider, transformer); + return new ZipkinSpanExporter(encoder, sender, meterProviderSupplier, transformer); } } diff --git a/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporterTest.java b/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporterTest.java index 56ad4ff81cc..0237b6c112c 100644 --- a/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporterTest.java +++ b/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporterTest.java @@ -46,7 +46,7 @@ void testExport() { TestSpanData testSpanData = spanBuilder().build(); ZipkinSpanExporter zipkinSpanExporter = - new ZipkinSpanExporter(mockEncoder, mockSender, MeterProvider.noop(), mockTransformer); + new ZipkinSpanExporter(mockEncoder, mockSender, MeterProvider::noop, mockTransformer); byte[] someBytes = new byte[0]; Span zipkinSpan = @@ -77,7 +77,7 @@ void testExport_failed() { TestSpanData testSpanData = spanBuilder().build(); ZipkinSpanExporter zipkinSpanExporter = - new ZipkinSpanExporter(mockEncoder, mockSender, MeterProvider.noop(), mockTransformer); + new ZipkinSpanExporter(mockEncoder, mockSender, MeterProvider::noop, mockTransformer); byte[] someBytes = new byte[0]; Span zipkinSpan = diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/FullConfigTest.java b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/FullConfigTest.java index e2a797c940b..82e09af46f2 100644 --- a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/FullConfigTest.java +++ b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/FullConfigTest.java @@ -155,6 +155,7 @@ void setUp() { System.setProperty("otel.exporter.otlp.timeout", "10000"); // Initialize here so we can shutdown when done + GlobalOpenTelemetry.resetForTest(); autoConfiguredOpenTelemetrySdk = AutoConfiguredOpenTelemetrySdk.initialize(); }