From 6ea11c1bb206020b7da868ef9d609def60f0a687 Mon Sep 17 00:00:00 2001 From: Jack Berg Date: Thu, 3 Aug 2023 16:19:04 -0500 Subject: [PATCH] Add OtlpGrpc{Signal}Exporter#toBuilder() methods --- .../opentelemetry-exporter-otlp.txt | 9 +++ .../internal/grpc/GrpcExporterBuilder.java | 26 +++++++- .../otlp/logs/OtlpGrpcLogRecordExporter.java | 16 ++++- .../OtlpGrpcLogRecordExporterBuilder.java | 12 ++-- .../otlp/metrics/OtlpGrpcMetricExporter.java | 13 ++++ .../OtlpGrpcMetricExporterBuilder.java | 14 +++-- .../otlp/trace/OtlpGrpcSpanExporter.java | 16 ++++- .../trace/OtlpGrpcSpanExporterBuilder.java | 12 ++-- .../logs/OtlpGrpcLogRecordExporterTest.java | 8 +++ .../metrics/OtlpGrpcMetricExporterTest.java | 6 ++ .../otlp/trace/OtlpGrpcSpanExporterTest.java | 6 ++ .../OtlpGrpcNettyLogRecordExporterTest.java | 8 +++ .../OtlpGrpcNettyMetricExporterTest.java | 6 ++ .../trace/OtlpGrpcNettySpanExporterTest.java | 6 ++ ...pGrpcNettyShadedLogRecordExporterTest.java | 8 +++ ...OtlpGrpcNettyShadedMetricExporterTest.java | 6 ++ .../OtlpGrpcNettyShadedSpanExporterTest.java | 6 ++ ...pGrpcNettyOkHttpLogRecordExporterTest.java | 8 +++ .../OtlpGrpcOkHttpMetricExporterTest.java | 6 ++ .../trace/OtlpGrpcOkHttpSpanExporterTest.java | 6 ++ .../AbstractGrpcTelemetryExporterTest.java | 62 +++++++++++++++++++ 21 files changed, 244 insertions(+), 16 deletions(-) diff --git a/docs/apidiffs/current_vs_latest/opentelemetry-exporter-otlp.txt b/docs/apidiffs/current_vs_latest/opentelemetry-exporter-otlp.txt index f564c57cba3..c9ed6d05776 100644 --- a/docs/apidiffs/current_vs_latest/opentelemetry-exporter-otlp.txt +++ b/docs/apidiffs/current_vs_latest/opentelemetry-exporter-otlp.txt @@ -8,3 +8,12 @@ Comparing source compatibility of against *** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter (not serializable) === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 +++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder toBuilder() +*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter (not serializable) + === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporterBuilder toBuilder() +*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter (not serializable) + === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporterBuilder toBuilder() +*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter (not serializable) + === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder toBuilder() 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 e8aec1995ac..4220cd024ea 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 @@ -54,7 +54,7 @@ public class GrpcExporterBuilder { private URI endpoint; private boolean compressionEnabled = false; private final Map headers = new HashMap<>(); - private final TlsConfigHelper tlsConfigHelper = new TlsConfigHelper(); + private TlsConfigHelper tlsConfigHelper = new TlsConfigHelper(); @Nullable private RetryPolicy retryPolicy; private Supplier meterProviderSupplier = GlobalOpenTelemetry::getMeterProvider; @@ -132,6 +132,30 @@ public GrpcExporterBuilder setMeterProvider(MeterProvider meterProvider) { return this; } + @SuppressWarnings("BuilderReturnThis") + public GrpcExporterBuilder copy() { + GrpcExporterBuilder copy = + new GrpcExporterBuilder<>( + exporterName, + type, + TimeUnit.NANOSECONDS.toSeconds(timeoutNanos), + endpoint, + grpcStubFactory, + grpcEndpointPath); + + copy.timeoutNanos = timeoutNanos; + copy.endpoint = endpoint; + copy.compressionEnabled = compressionEnabled; + copy.headers.putAll(headers); + copy.tlsConfigHelper = tlsConfigHelper.copy(); + if (retryPolicy != null) { + copy.retryPolicy = retryPolicy.toBuilder().build(); + } + copy.meterProviderSupplier = meterProviderSupplier; + copy.grpcChannel = grpcChannel; + return copy; + } + public GrpcExporter build() { if (grpcChannel != null) { return new UpstreamGrpcExporterFactory().buildWithChannel((Channel) grpcChannel); diff --git a/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporter.java b/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporter.java index 448b427150d..1c555f3465b 100644 --- a/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporter.java +++ b/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporter.java @@ -6,6 +6,7 @@ package io.opentelemetry.exporter.otlp.logs; import io.opentelemetry.exporter.internal.grpc.GrpcExporter; +import io.opentelemetry.exporter.internal.grpc.GrpcExporterBuilder; import io.opentelemetry.exporter.internal.otlp.logs.LogsRequestMarshaler; import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.logs.data.LogRecordData; @@ -21,6 +22,7 @@ @ThreadSafe public final class OtlpGrpcLogRecordExporter implements LogRecordExporter { + private final GrpcExporterBuilder builder; private final GrpcExporter delegate; /** @@ -44,10 +46,22 @@ public static OtlpGrpcLogRecordExporterBuilder builder() { return new OtlpGrpcLogRecordExporterBuilder(); } - OtlpGrpcLogRecordExporter(GrpcExporter delegate) { + OtlpGrpcLogRecordExporter( + GrpcExporterBuilder builder, + GrpcExporter delegate) { + this.builder = builder; this.delegate = delegate; } + /** + * Returns a builder with configuration values equal to those for this exporter. + * + *

IMPORTANT: Be sure to {@link #shutdown()} this instance if it will no longer be used. + */ + public OtlpGrpcLogRecordExporterBuilder toBuilder() { + return new OtlpGrpcLogRecordExporterBuilder(builder.copy()); + } + /** * Submits all the given logs in a single batch to the OpenTelemetry collector. * diff --git a/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporterBuilder.java b/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporterBuilder.java index a6eda4fbad5..073926f317a 100644 --- a/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporterBuilder.java +++ b/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporterBuilder.java @@ -41,16 +41,20 @@ public final class OtlpGrpcLogRecordExporterBuilder { // Visible for testing final GrpcExporterBuilder delegate; + OtlpGrpcLogRecordExporterBuilder(GrpcExporterBuilder delegate) { + this.delegate = delegate; + OtlpUserAgent.addUserAgentHeader(delegate::addHeader); + } + OtlpGrpcLogRecordExporterBuilder() { - delegate = + this( GrpcExporter.builder( "otlp", "log", DEFAULT_TIMEOUT_SECS, DEFAULT_ENDPOINT, () -> MarshalerLogsServiceGrpc::newFutureStub, - GRPC_ENDPOINT_PATH); - OtlpUserAgent.addUserAgentHeader(delegate::addHeader); + GRPC_ENDPOINT_PATH)); } /** @@ -186,6 +190,6 @@ public OtlpGrpcLogRecordExporterBuilder setMeterProvider(MeterProvider meterProv * @return a new exporter's instance */ public OtlpGrpcLogRecordExporter build() { - return new OtlpGrpcLogRecordExporter(delegate.build()); + return new OtlpGrpcLogRecordExporter(delegate, delegate.build()); } } diff --git a/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcMetricExporter.java b/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcMetricExporter.java index 13facea234b..4c932f59aa0 100644 --- a/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcMetricExporter.java +++ b/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcMetricExporter.java @@ -6,6 +6,7 @@ package io.opentelemetry.exporter.otlp.metrics; import io.opentelemetry.exporter.internal.grpc.GrpcExporter; +import io.opentelemetry.exporter.internal.grpc.GrpcExporterBuilder; import io.opentelemetry.exporter.internal.otlp.metrics.MetricsRequestMarshaler; import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.metrics.Aggregation; @@ -26,6 +27,7 @@ @ThreadSafe public final class OtlpGrpcMetricExporter implements MetricExporter { + private final GrpcExporterBuilder builder; private final GrpcExporter delegate; private final AggregationTemporalitySelector aggregationTemporalitySelector; private final DefaultAggregationSelector defaultAggregationSelector; @@ -52,14 +54,25 @@ public static OtlpGrpcMetricExporterBuilder builder() { } OtlpGrpcMetricExporter( + GrpcExporterBuilder builder, GrpcExporter delegate, AggregationTemporalitySelector aggregationTemporalitySelector, DefaultAggregationSelector defaultAggregationSelector) { + this.builder = builder; this.delegate = delegate; this.aggregationTemporalitySelector = aggregationTemporalitySelector; this.defaultAggregationSelector = defaultAggregationSelector; } + /** + * Returns a builder with configuration values equal to those for this exporter. + * + *

IMPORTANT: Be sure to {@link #shutdown()} this instance if it will no longer be used. + */ + public OtlpGrpcMetricExporterBuilder toBuilder() { + return new OtlpGrpcMetricExporterBuilder(builder.copy()); + } + @Override public AggregationTemporality getAggregationTemporality(InstrumentType instrumentType) { return aggregationTemporalitySelector.getAggregationTemporality(instrumentType); 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 3046956e8a3..2009c72bba9 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 @@ -52,17 +52,21 @@ public final class OtlpGrpcMetricExporterBuilder { private DefaultAggregationSelector defaultAggregationSelector = DefaultAggregationSelector.getDefault(); + OtlpGrpcMetricExporterBuilder(GrpcExporterBuilder delegate) { + this.delegate = delegate; + delegate.setMeterProvider(MeterProvider.noop()); + OtlpUserAgent.addUserAgentHeader(delegate::addHeader); + } + OtlpGrpcMetricExporterBuilder() { - delegate = + this( GrpcExporter.builder( "otlp", "metric", DEFAULT_TIMEOUT_SECS, DEFAULT_ENDPOINT, () -> MarshalerMetricsServiceGrpc::newFutureStub, - GRPC_ENDPOINT_PATH); - delegate.setMeterProvider(MeterProvider.noop()); - OtlpUserAgent.addUserAgentHeader(delegate::addHeader); + GRPC_ENDPOINT_PATH)); } /** @@ -221,6 +225,6 @@ public OtlpGrpcMetricExporterBuilder setRetryPolicy(RetryPolicy retryPolicy) { */ public OtlpGrpcMetricExporter build() { return new OtlpGrpcMetricExporter( - delegate.build(), aggregationTemporalitySelector, defaultAggregationSelector); + delegate, delegate.build(), aggregationTemporalitySelector, defaultAggregationSelector); } } diff --git a/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcSpanExporter.java b/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcSpanExporter.java index 9e6b6981b13..f8caef8027f 100644 --- a/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcSpanExporter.java +++ b/exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcSpanExporter.java @@ -6,6 +6,7 @@ package io.opentelemetry.exporter.otlp.trace; import io.opentelemetry.exporter.internal.grpc.GrpcExporter; +import io.opentelemetry.exporter.internal.grpc.GrpcExporterBuilder; import io.opentelemetry.exporter.internal.otlp.traces.TraceRequestMarshaler; import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.trace.data.SpanData; @@ -17,6 +18,7 @@ @ThreadSafe public final class OtlpGrpcSpanExporter implements SpanExporter { + private final GrpcExporterBuilder builder; private final GrpcExporter delegate; /** @@ -40,10 +42,22 @@ public static OtlpGrpcSpanExporterBuilder builder() { return new OtlpGrpcSpanExporterBuilder(); } - OtlpGrpcSpanExporter(GrpcExporter delegate) { + OtlpGrpcSpanExporter( + GrpcExporterBuilder builder, + GrpcExporter delegate) { + this.builder = builder; this.delegate = delegate; } + /** + * Returns a builder with configuration values equal to those for this exporter. + * + *

IMPORTANT: Be sure to {@link #shutdown()} this instance if it will no longer be used. + */ + public OtlpGrpcSpanExporterBuilder toBuilder() { + return new OtlpGrpcSpanExporterBuilder(builder.copy()); + } + /** * Submits all the given spans in a single batch to the OpenTelemetry collector. * 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 a9d2380f5a5..e22fc3d4951 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 @@ -37,16 +37,20 @@ public final class OtlpGrpcSpanExporterBuilder { // Visible for testing final GrpcExporterBuilder delegate; + OtlpGrpcSpanExporterBuilder(GrpcExporterBuilder delegate) { + this.delegate = delegate; + OtlpUserAgent.addUserAgentHeader(delegate::addHeader); + } + OtlpGrpcSpanExporterBuilder() { - delegate = + this( GrpcExporter.builder( "otlp", "span", DEFAULT_TIMEOUT_SECS, DEFAULT_ENDPOINT, () -> MarshalerTraceServiceGrpc::newFutureStub, - GRPC_ENDPOINT_PATH); - OtlpUserAgent.addUserAgentHeader(delegate::addHeader); + GRPC_ENDPOINT_PATH)); } /** @@ -183,6 +187,6 @@ public OtlpGrpcSpanExporterBuilder setMeterProvider(MeterProvider meterProvider) * @return a new exporter's instance */ public OtlpGrpcSpanExporter build() { - return new OtlpGrpcSpanExporter(delegate.build()); + return new OtlpGrpcSpanExporter(delegate, delegate.build()); } } diff --git a/exporters/otlp/all/src/test/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporterTest.java b/exporters/otlp/all/src/test/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporterTest.java index 9f70c438d32..d468a46245d 100644 --- a/exporters/otlp/all/src/test/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporterTest.java +++ b/exporters/otlp/all/src/test/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporterTest.java @@ -12,6 +12,7 @@ import io.opentelemetry.exporter.internal.otlp.logs.ResourceLogsMarshaler; import io.opentelemetry.exporter.otlp.testing.internal.AbstractGrpcTelemetryExporterTest; import io.opentelemetry.exporter.otlp.testing.internal.FakeTelemetryUtil; +import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporter; import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporterBuilder; import io.opentelemetry.proto.logs.v1.ResourceLogs; import io.opentelemetry.sdk.logs.data.LogRecordData; @@ -38,6 +39,13 @@ protected TelemetryExporterBuilder exporterBuilder() { return TelemetryExporterBuilder.wrap(OtlpGrpcLogRecordExporter.builder()); } + @Override + protected TelemetryExporterBuilder toBuilder( + TelemetryExporter exporter) { + return TelemetryExporterBuilder.wrap( + ((OtlpGrpcLogRecordExporter) exporter.unwrap()).toBuilder()); + } + @Override protected LogRecordData generateFakeTelemetry() { return FakeTelemetryUtil.generateFakeLogRecordData(); diff --git a/exporters/otlp/all/src/test/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcMetricExporterTest.java b/exporters/otlp/all/src/test/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcMetricExporterTest.java index 0c110ae5831..ec02a2b5657 100644 --- a/exporters/otlp/all/src/test/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcMetricExporterTest.java +++ b/exporters/otlp/all/src/test/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcMetricExporterTest.java @@ -14,6 +14,7 @@ import io.opentelemetry.exporter.internal.otlp.metrics.ResourceMetricsMarshaler; import io.opentelemetry.exporter.otlp.testing.internal.AbstractGrpcTelemetryExporterTest; import io.opentelemetry.exporter.otlp.testing.internal.FakeTelemetryUtil; +import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporter; import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporterBuilder; import io.opentelemetry.proto.metrics.v1.ResourceMetrics; import io.opentelemetry.sdk.metrics.Aggregation; @@ -89,6 +90,11 @@ protected TelemetryExporterBuilder exporterBuilder() { return TelemetryExporterBuilder.wrap(OtlpGrpcMetricExporter.builder()); } + @Override + protected TelemetryExporterBuilder toBuilder(TelemetryExporter exporter) { + return TelemetryExporterBuilder.wrap(((OtlpGrpcMetricExporter) exporter.unwrap()).toBuilder()); + } + @Override protected MetricData generateFakeTelemetry() { return FakeTelemetryUtil.generateFakeMetricData(); diff --git a/exporters/otlp/all/src/test/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcSpanExporterTest.java b/exporters/otlp/all/src/test/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcSpanExporterTest.java index d35312d5f2f..149e668f8fd 100644 --- a/exporters/otlp/all/src/test/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcSpanExporterTest.java +++ b/exporters/otlp/all/src/test/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcSpanExporterTest.java @@ -12,6 +12,7 @@ import io.opentelemetry.exporter.internal.otlp.traces.ResourceSpansMarshaler; import io.opentelemetry.exporter.otlp.testing.internal.AbstractGrpcTelemetryExporterTest; import io.opentelemetry.exporter.otlp.testing.internal.FakeTelemetryUtil; +import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporter; import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporterBuilder; import io.opentelemetry.proto.trace.v1.ResourceSpans; import io.opentelemetry.sdk.trace.data.SpanData; @@ -37,6 +38,11 @@ protected TelemetryExporterBuilder exporterBuilder() { return TelemetryExporterBuilder.wrap(OtlpGrpcSpanExporter.builder()); } + @Override + protected TelemetryExporterBuilder toBuilder(TelemetryExporter exporter) { + return TelemetryExporterBuilder.wrap(((OtlpGrpcSpanExporter) exporter.unwrap()).toBuilder()); + } + @Override protected SpanData generateFakeTelemetry() { return FakeTelemetryUtil.generateFakeSpanData(); diff --git a/exporters/otlp/all/src/testGrpcNetty/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcNettyLogRecordExporterTest.java b/exporters/otlp/all/src/testGrpcNetty/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcNettyLogRecordExporterTest.java index cbee682cc92..98ed795bae6 100644 --- a/exporters/otlp/all/src/testGrpcNetty/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcNettyLogRecordExporterTest.java +++ b/exporters/otlp/all/src/testGrpcNetty/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcNettyLogRecordExporterTest.java @@ -15,6 +15,7 @@ import io.opentelemetry.exporter.otlp.testing.internal.AbstractGrpcTelemetryExporterTest; import io.opentelemetry.exporter.otlp.testing.internal.FakeTelemetryUtil; import io.opentelemetry.exporter.otlp.testing.internal.ManagedChannelTelemetryExporterBuilder; +import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporter; import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporterBuilder; import io.opentelemetry.proto.logs.v1.ResourceLogs; import io.opentelemetry.sdk.logs.data.LogRecordData; @@ -46,6 +47,13 @@ protected TelemetryExporterBuilder exporterBuilder() { TelemetryExporterBuilder.wrap(OtlpGrpcLogRecordExporter.builder())); } + @Override + protected TelemetryExporterBuilder toBuilder( + TelemetryExporter exporter) { + return TelemetryExporterBuilder.wrap( + ((OtlpGrpcLogRecordExporter) exporter.unwrap()).toBuilder()); + } + @Override protected LogRecordData generateFakeTelemetry() { return FakeTelemetryUtil.generateFakeLogRecordData(); diff --git a/exporters/otlp/all/src/testGrpcNetty/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcNettyMetricExporterTest.java b/exporters/otlp/all/src/testGrpcNetty/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcNettyMetricExporterTest.java index 03cba4cc871..509c1b5a154 100644 --- a/exporters/otlp/all/src/testGrpcNetty/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcNettyMetricExporterTest.java +++ b/exporters/otlp/all/src/testGrpcNetty/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcNettyMetricExporterTest.java @@ -15,6 +15,7 @@ import io.opentelemetry.exporter.otlp.testing.internal.AbstractGrpcTelemetryExporterTest; import io.opentelemetry.exporter.otlp.testing.internal.FakeTelemetryUtil; import io.opentelemetry.exporter.otlp.testing.internal.ManagedChannelTelemetryExporterBuilder; +import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporter; import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporterBuilder; import io.opentelemetry.proto.metrics.v1.ResourceMetrics; import io.opentelemetry.sdk.metrics.data.MetricData; @@ -46,6 +47,11 @@ protected TelemetryExporterBuilder exporterBuilder() { TelemetryExporterBuilder.wrap(OtlpGrpcMetricExporter.builder())); } + @Override + protected TelemetryExporterBuilder toBuilder(TelemetryExporter exporter) { + return TelemetryExporterBuilder.wrap(((OtlpGrpcMetricExporter) exporter.unwrap()).toBuilder()); + } + @Override protected MetricData generateFakeTelemetry() { return FakeTelemetryUtil.generateFakeMetricData(); diff --git a/exporters/otlp/all/src/testGrpcNetty/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcNettySpanExporterTest.java b/exporters/otlp/all/src/testGrpcNetty/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcNettySpanExporterTest.java index 9ecfdbbf0f2..883440c4ceb 100644 --- a/exporters/otlp/all/src/testGrpcNetty/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcNettySpanExporterTest.java +++ b/exporters/otlp/all/src/testGrpcNetty/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcNettySpanExporterTest.java @@ -15,6 +15,7 @@ import io.opentelemetry.exporter.otlp.testing.internal.AbstractGrpcTelemetryExporterTest; import io.opentelemetry.exporter.otlp.testing.internal.FakeTelemetryUtil; import io.opentelemetry.exporter.otlp.testing.internal.ManagedChannelTelemetryExporterBuilder; +import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporter; import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporterBuilder; import io.opentelemetry.proto.trace.v1.ResourceSpans; import io.opentelemetry.sdk.trace.data.SpanData; @@ -46,6 +47,11 @@ protected TelemetryExporterBuilder exporterBuilder() { TelemetryExporterBuilder.wrap(OtlpGrpcSpanExporter.builder())); } + @Override + protected TelemetryExporterBuilder toBuilder(TelemetryExporter exporter) { + return TelemetryExporterBuilder.wrap(((OtlpGrpcSpanExporter) exporter.unwrap()).toBuilder()); + } + @Override protected SpanData generateFakeTelemetry() { return FakeTelemetryUtil.generateFakeSpanData(); diff --git a/exporters/otlp/all/src/testGrpcNettyShaded/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcNettyShadedLogRecordExporterTest.java b/exporters/otlp/all/src/testGrpcNettyShaded/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcNettyShadedLogRecordExporterTest.java index 5789d33653f..c6a982bb12b 100644 --- a/exporters/otlp/all/src/testGrpcNettyShaded/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcNettyShadedLogRecordExporterTest.java +++ b/exporters/otlp/all/src/testGrpcNettyShaded/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcNettyShadedLogRecordExporterTest.java @@ -15,6 +15,7 @@ import io.opentelemetry.exporter.otlp.testing.internal.AbstractGrpcTelemetryExporterTest; import io.opentelemetry.exporter.otlp.testing.internal.FakeTelemetryUtil; import io.opentelemetry.exporter.otlp.testing.internal.ManagedChannelTelemetryExporterBuilder; +import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporter; import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporterBuilder; import io.opentelemetry.proto.logs.v1.ResourceLogs; import io.opentelemetry.sdk.logs.data.LogRecordData; @@ -46,6 +47,13 @@ protected TelemetryExporterBuilder exporterBuilder() { TelemetryExporterBuilder.wrap(OtlpGrpcLogRecordExporter.builder())); } + @Override + protected TelemetryExporterBuilder toBuilder( + TelemetryExporter exporter) { + return TelemetryExporterBuilder.wrap( + ((OtlpGrpcLogRecordExporter) exporter.unwrap()).toBuilder()); + } + @Override protected LogRecordData generateFakeTelemetry() { return FakeTelemetryUtil.generateFakeLogRecordData(); diff --git a/exporters/otlp/all/src/testGrpcNettyShaded/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcNettyShadedMetricExporterTest.java b/exporters/otlp/all/src/testGrpcNettyShaded/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcNettyShadedMetricExporterTest.java index 26d2d509ba7..2e6734a7bbd 100644 --- a/exporters/otlp/all/src/testGrpcNettyShaded/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcNettyShadedMetricExporterTest.java +++ b/exporters/otlp/all/src/testGrpcNettyShaded/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcNettyShadedMetricExporterTest.java @@ -15,6 +15,7 @@ import io.opentelemetry.exporter.otlp.testing.internal.AbstractGrpcTelemetryExporterTest; import io.opentelemetry.exporter.otlp.testing.internal.FakeTelemetryUtil; import io.opentelemetry.exporter.otlp.testing.internal.ManagedChannelTelemetryExporterBuilder; +import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporter; import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporterBuilder; import io.opentelemetry.proto.metrics.v1.ResourceMetrics; import io.opentelemetry.sdk.metrics.data.MetricData; @@ -46,6 +47,11 @@ protected TelemetryExporterBuilder exporterBuilder() { TelemetryExporterBuilder.wrap(OtlpGrpcMetricExporter.builder())); } + @Override + protected TelemetryExporterBuilder toBuilder(TelemetryExporter exporter) { + return TelemetryExporterBuilder.wrap(((OtlpGrpcMetricExporter) exporter.unwrap()).toBuilder()); + } + @Override protected MetricData generateFakeTelemetry() { return FakeTelemetryUtil.generateFakeMetricData(); diff --git a/exporters/otlp/all/src/testGrpcNettyShaded/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcNettyShadedSpanExporterTest.java b/exporters/otlp/all/src/testGrpcNettyShaded/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcNettyShadedSpanExporterTest.java index 191708af242..013066f20cd 100644 --- a/exporters/otlp/all/src/testGrpcNettyShaded/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcNettyShadedSpanExporterTest.java +++ b/exporters/otlp/all/src/testGrpcNettyShaded/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcNettyShadedSpanExporterTest.java @@ -15,6 +15,7 @@ import io.opentelemetry.exporter.otlp.testing.internal.AbstractGrpcTelemetryExporterTest; import io.opentelemetry.exporter.otlp.testing.internal.FakeTelemetryUtil; import io.opentelemetry.exporter.otlp.testing.internal.ManagedChannelTelemetryExporterBuilder; +import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporter; import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporterBuilder; import io.opentelemetry.proto.trace.v1.ResourceSpans; import io.opentelemetry.sdk.trace.data.SpanData; @@ -46,6 +47,11 @@ protected TelemetryExporterBuilder exporterBuilder() { TelemetryExporterBuilder.wrap(OtlpGrpcSpanExporter.builder())); } + @Override + protected TelemetryExporterBuilder toBuilder(TelemetryExporter exporter) { + return TelemetryExporterBuilder.wrap(((OtlpGrpcSpanExporter) exporter.unwrap()).toBuilder()); + } + @Override protected SpanData generateFakeTelemetry() { return FakeTelemetryUtil.generateFakeSpanData(); diff --git a/exporters/otlp/all/src/testGrpcOkhttp/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcNettyOkHttpLogRecordExporterTest.java b/exporters/otlp/all/src/testGrpcOkhttp/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcNettyOkHttpLogRecordExporterTest.java index 9923df2b1ea..f7597c29d20 100644 --- a/exporters/otlp/all/src/testGrpcOkhttp/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcNettyOkHttpLogRecordExporterTest.java +++ b/exporters/otlp/all/src/testGrpcOkhttp/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcNettyOkHttpLogRecordExporterTest.java @@ -15,6 +15,7 @@ import io.opentelemetry.exporter.otlp.testing.internal.AbstractGrpcTelemetryExporterTest; import io.opentelemetry.exporter.otlp.testing.internal.FakeTelemetryUtil; import io.opentelemetry.exporter.otlp.testing.internal.ManagedChannelTelemetryExporterBuilder; +import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporter; import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporterBuilder; import io.opentelemetry.proto.logs.v1.ResourceLogs; import io.opentelemetry.sdk.logs.data.LogRecordData; @@ -46,6 +47,13 @@ protected TelemetryExporterBuilder exporterBuilder() { TelemetryExporterBuilder.wrap(OtlpGrpcLogRecordExporter.builder())); } + @Override + protected TelemetryExporterBuilder toBuilder( + TelemetryExporter exporter) { + return TelemetryExporterBuilder.wrap( + ((OtlpGrpcLogRecordExporter) exporter.unwrap()).toBuilder()); + } + @Override protected LogRecordData generateFakeTelemetry() { return FakeTelemetryUtil.generateFakeLogRecordData(); diff --git a/exporters/otlp/all/src/testGrpcOkhttp/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcOkHttpMetricExporterTest.java b/exporters/otlp/all/src/testGrpcOkhttp/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcOkHttpMetricExporterTest.java index f18a5e715d2..62a961f14b2 100644 --- a/exporters/otlp/all/src/testGrpcOkhttp/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcOkHttpMetricExporterTest.java +++ b/exporters/otlp/all/src/testGrpcOkhttp/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcOkHttpMetricExporterTest.java @@ -15,6 +15,7 @@ import io.opentelemetry.exporter.otlp.testing.internal.AbstractGrpcTelemetryExporterTest; import io.opentelemetry.exporter.otlp.testing.internal.FakeTelemetryUtil; import io.opentelemetry.exporter.otlp.testing.internal.ManagedChannelTelemetryExporterBuilder; +import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporter; import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporterBuilder; import io.opentelemetry.proto.metrics.v1.ResourceMetrics; import io.opentelemetry.sdk.metrics.data.MetricData; @@ -46,6 +47,11 @@ protected TelemetryExporterBuilder exporterBuilder() { TelemetryExporterBuilder.wrap(OtlpGrpcMetricExporter.builder())); } + @Override + protected TelemetryExporterBuilder toBuilder(TelemetryExporter exporter) { + return TelemetryExporterBuilder.wrap(((OtlpGrpcMetricExporter) exporter.unwrap()).toBuilder()); + } + @Override protected MetricData generateFakeTelemetry() { return FakeTelemetryUtil.generateFakeMetricData(); diff --git a/exporters/otlp/all/src/testGrpcOkhttp/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcOkHttpSpanExporterTest.java b/exporters/otlp/all/src/testGrpcOkhttp/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcOkHttpSpanExporterTest.java index e41bfc5660a..c8959518393 100644 --- a/exporters/otlp/all/src/testGrpcOkhttp/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcOkHttpSpanExporterTest.java +++ b/exporters/otlp/all/src/testGrpcOkhttp/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcOkHttpSpanExporterTest.java @@ -15,6 +15,7 @@ import io.opentelemetry.exporter.otlp.testing.internal.AbstractGrpcTelemetryExporterTest; import io.opentelemetry.exporter.otlp.testing.internal.FakeTelemetryUtil; import io.opentelemetry.exporter.otlp.testing.internal.ManagedChannelTelemetryExporterBuilder; +import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporter; import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporterBuilder; import io.opentelemetry.proto.trace.v1.ResourceSpans; import io.opentelemetry.sdk.trace.data.SpanData; @@ -46,6 +47,11 @@ protected TelemetryExporterBuilder exporterBuilder() { TelemetryExporterBuilder.wrap(OtlpGrpcSpanExporter.builder())); } + @Override + protected TelemetryExporterBuilder toBuilder(TelemetryExporter exporter) { + return TelemetryExporterBuilder.wrap(((OtlpGrpcSpanExporter) exporter.unwrap()).toBuilder()); + } + @Override protected SpanData generateFakeTelemetry() { return FakeTelemetryUtil.generateFakeSpanData(); diff --git a/exporters/otlp/testing-internal/src/main/java/io/opentelemetry/exporter/otlp/testing/internal/AbstractGrpcTelemetryExporterTest.java b/exporters/otlp/testing-internal/src/main/java/io/opentelemetry/exporter/otlp/testing/internal/AbstractGrpcTelemetryExporterTest.java index 2ccb554a512..832dd3afd03 100644 --- a/exporters/otlp/testing-internal/src/main/java/io/opentelemetry/exporter/otlp/testing/internal/AbstractGrpcTelemetryExporterTest.java +++ b/exporters/otlp/testing-internal/src/main/java/io/opentelemetry/exporter/otlp/testing/internal/AbstractGrpcTelemetryExporterTest.java @@ -39,8 +39,10 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UncheckedIOException; +import java.lang.reflect.Field; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.security.cert.CertificateEncodingException; import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; @@ -723,8 +725,68 @@ void invalidConfig() { "Unsupported compression method. Supported compression methods include: gzip, none."); } + @Test + void toBuilderEquality() + throws CertificateEncodingException, + IOException, + NoSuchFieldException, + IllegalAccessException { + TelemetryExporter exporter = + exporterBuilder() + .setTimeout(Duration.ofSeconds(5)) + .setEndpoint("http://localhost:4317") + .setCompression("gzip") + .addHeader("foo", "bar") + .setTrustedCertificates(certificate.certificate().getEncoded()) + .setClientTls( + Files.readAllBytes(clientCertificate.privateKeyFile().toPath()), + Files.readAllBytes(clientCertificate.certificateFile().toPath())) + .setRetryPolicy( + RetryPolicy.builder() + .setMaxAttempts(2) + .setMaxBackoff(Duration.ofSeconds(3)) + .setInitialBackoff(Duration.ofMillis(50)) + .setBackoffMultiplier(1.3) + .build()) + .build(); + + Object unwrapped = exporter.unwrap(); + Field builderField = unwrapped.getClass().getDeclaredField("builder"); + builderField.setAccessible(true); + + try { + // Builder copy should be equal to original when unchanged + TelemetryExporter copy = toBuilder(exporter).build(); + try { + assertThat(copy.unwrap()) + .extracting("builder") + .usingRecursiveComparison() + .ignoringFields("tlsConfigHelper") + .isEqualTo(builderField.get(unwrapped)); + } finally { + copy.shutdown(); + } + + // Builder copy should NOT be equal when changed + copy = toBuilder(exporter).addHeader("baz", "qux").build(); + try { + assertThat(copy.unwrap()) + .extracting("builder") + .usingRecursiveComparison() + .ignoringFields("tlsConfigHelper") + .isNotEqualTo(builderField.get(unwrapped)); + } finally { + copy.shutdown(); + } + } finally { + exporter.shutdown(); + } + } + protected abstract TelemetryExporterBuilder exporterBuilder(); + protected abstract TelemetryExporterBuilder toBuilder(TelemetryExporter exporter); + protected abstract T generateFakeTelemetry(); protected abstract Marshaler[] toMarshalers(List telemetry);