From 738da03344204c0f2b1c198a11ac53082730e943 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Sun, 14 Jan 2024 17:32:34 +0800 Subject: [PATCH] Update dependency io.zipkin.reporter2:zipkin-reporter-bom to v3.2.1 This migrates to the new `BytesMessageSender` base class used in zipkin-reporter. The old `Sender` is deprecated for removal. Notably, this only supports synchronous invocation now. If you need async, you'll have to use your own reporting loop or otherwise. Signed-off-by: Adrian Cole --- dependencyManagement/build.gradle.kts | 2 +- .../opentelemetry-exporter-zipkin.txt | 3 ++ .../exporter/zipkin/ZipkinSpanExporter.java | 35 ++++++------------ .../zipkin/ZipkinSpanExporterBuilder.java | 33 ++++++++++++----- .../ZipkinSpanExporterEndToEndHttpTest.java | 5 ++- .../zipkin/ZipkinSpanExporterTest.java | 37 +++++-------------- 6 files changed, 54 insertions(+), 61 deletions(-) diff --git a/dependencyManagement/build.gradle.kts b/dependencyManagement/build.gradle.kts index d4c4ca11de3..af78eaea503 100644 --- a/dependencyManagement/build.gradle.kts +++ b/dependencyManagement/build.gradle.kts @@ -16,7 +16,7 @@ val DEPENDENCY_BOMS = listOf( "com.squareup.okio:okio-bom:3.7.0", // applies to transitive dependencies of okhttp "io.grpc:grpc-bom:1.60.1", "io.netty:netty-bom:4.1.104.Final", - "io.zipkin.reporter2:zipkin-reporter-bom:3.1.1", + "io.zipkin.reporter2:zipkin-reporter-bom:3.2.1", "io.zipkin.brave:brave-bom:5.17.0", "org.assertj:assertj-bom:3.25.1", "org.junit:junit-bom:5.10.1", diff --git a/docs/apidiffs/current_vs_latest/opentelemetry-exporter-zipkin.txt b/docs/apidiffs/current_vs_latest/opentelemetry-exporter-zipkin.txt index 73d401a04b9..b941adbcdd2 100644 --- a/docs/apidiffs/current_vs_latest/opentelemetry-exporter-zipkin.txt +++ b/docs/apidiffs/current_vs_latest/opentelemetry-exporter-zipkin.txt @@ -4,3 +4,6 @@ Comparing source compatibility of against === UNCHANGED METHOD: PUBLIC io.opentelemetry.exporter.zipkin.ZipkinSpanExporterBuilder setEncoder(zipkin2.codec.BytesEncoder) +++ NEW ANNOTATION: java.lang.Deprecated +++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.zipkin.ZipkinSpanExporterBuilder setEncoder(zipkin2.reporter.BytesEncoder) + === UNCHANGED METHOD: PUBLIC io.opentelemetry.exporter.zipkin.ZipkinSpanExporterBuilder setSender(zipkin2.reporter.Sender) + +++ NEW ANNOTATION: java.lang.Deprecated + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.zipkin.ZipkinSpanExporterBuilder setSender(zipkin2.reporter.BytesMessageSender) 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 840e53b58de..c92f70db3f5 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 @@ -21,9 +21,8 @@ import java.util.logging.Logger; import zipkin2.Span; import zipkin2.reporter.BytesEncoder; -import zipkin2.reporter.Callback; +import zipkin2.reporter.BytesMessageSender; import zipkin2.reporter.Encoding; -import zipkin2.reporter.Sender; /** * This class was based on the encoder; - private final Sender sender; + private final BytesMessageSender sender; private final ExporterMetrics exporterMetrics; private final OtelToZipkinSpanTransformer transformer; @@ -48,7 +47,7 @@ public final class ZipkinSpanExporter implements SpanExporter { ZipkinSpanExporter( ZipkinSpanExporterBuilder builder, BytesEncoder encoder, - Sender sender, + BytesMessageSender sender, Supplier meterProviderSupplier, OtelToZipkinSpanTransformer transformer) { this.builder = builder; @@ -76,25 +75,15 @@ public CompletableResultCode export(Collection spanDataList) { encodedSpans.add(encoder.encode(zipkinSpan)); } - CompletableResultCode result = new CompletableResultCode(); - sender - .sendSpans(encodedSpans) - .enqueue( - new Callback() { - @Override - public void onSuccess(Void value) { - exporterMetrics.addSuccess(numItems); - result.succeed(); - } - - @Override - public void onError(Throwable t) { - exporterMetrics.addFailed(numItems); - logger.log(Level.WARNING, "Failed to export spans", t); - result.fail(); - } - }); - return result; + try { + sender.send(encodedSpans); + exporterMetrics.addSuccess(numItems); + return CompletableResultCode.ofSuccess(); + } catch (IOException | RuntimeException t) { + exporterMetrics.addFailed(numItems); + logger.log(Level.WARNING, "Failed to export spans", t); + return CompletableResultCode.ofFailure(); + } } @Override 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 ad19cbc8d33..e73e4f73474 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 @@ -18,7 +18,7 @@ import javax.annotation.Nullable; import zipkin2.Span; import zipkin2.reporter.BytesEncoder; -import zipkin2.reporter.Sender; +import zipkin2.reporter.BytesMessageSender; import zipkin2.reporter.SpanBytesEncoder; import zipkin2.reporter.okhttp3.OkHttpSender; @@ -26,7 +26,7 @@ public final class ZipkinSpanExporterBuilder { private BytesEncoder encoder = SpanBytesEncoder.JSON_V2; private Supplier localIpAddressSupplier = LocalInetAddressSupplier.getInstance(); - @Nullable private Sender sender; + @Nullable private BytesMessageSender sender; private String endpoint = ZipkinSpanExporter.DEFAULT_ENDPOINT; // compression is enabled by default, because this is the default of OkHttpSender, // which is created when no custom sender is set (see OkHttpSender.Builder) @@ -38,12 +38,27 @@ public final class ZipkinSpanExporterBuilder { * Sets the Zipkin sender. Implements the client side of the span transport. An {@link * OkHttpSender} is a good default. * - *

The {@link Sender#close()} method will be called when the exporter is shut down. + *

The {@link BytesMessageSender#close()} method will be called when the exporter is shut down. * * @param sender the Zipkin sender implementation. * @return this. + * @deprecated Use {@link #setSender(BytesMessageSender)} insteead. */ - public ZipkinSpanExporterBuilder setSender(Sender sender) { + @Deprecated + public ZipkinSpanExporterBuilder setSender(zipkin2.reporter.Sender sender) { + return setSender((BytesMessageSender) sender); + } + + /** + * Sets the Zipkin sender. Implements the client side of the span transport. An {@link + * OkHttpSender} is a good default. + * + *

The {@link BytesMessageSender#close()} method will be called when the exporter is shut down. + * + * @param sender the Zipkin sender implementation. + * @return this. + */ + public ZipkinSpanExporterBuilder setSender(BytesMessageSender sender) { requireNonNull(sender, "sender"); this.sender = sender; return this; @@ -51,7 +66,7 @@ public ZipkinSpanExporterBuilder setSender(Sender sender) { /** * Sets the {@link zipkin2.codec.BytesEncoder}, which controls the format used by the {@link - * Sender}. Defaults to the {@link zipkin2.codec.SpanBytesEncoder#JSON_V2}. + * BytesMessageSender}. Defaults to the {@link zipkin2.codec.SpanBytesEncoder#JSON_V2}. * * @param encoder the {@code BytesEncoder} to use. * @return this. @@ -65,8 +80,8 @@ public ZipkinSpanExporterBuilder setEncoder(zipkin2.codec.BytesEncoder enc } /** - * Sets the {@link BytesEncoder}, which controls the format used by the {@link Sender}. Defaults - * to the {@link SpanBytesEncoder#JSON_V2}. + * Sets the {@link BytesEncoder}, which controls the format used by the {@link + * BytesMessageSender}. Defaults to the {@link SpanBytesEncoder#JSON_V2}. * * @param encoder the {@code BytesEncoder} to use. * @return this. @@ -114,7 +129,7 @@ public ZipkinSpanExporterBuilder setEndpoint(String endpoint) { * supported compression methods include "gzip" and "none". * *

The compression method is ignored when a custom Zipkin sender is set via {@link - * #setSender(Sender)}. + * #setSender(BytesMessageSender)}. * * @param compressionMethod The compression method, ex. "gzip". * @return this. @@ -189,7 +204,7 @@ String toString(boolean includePrefixAndSuffix) { * @return a {@code ZipkinSpanExporter}. */ public ZipkinSpanExporter build() { - Sender sender = this.sender; + BytesMessageSender sender = this.sender; if (sender == null) { sender = OkHttpSender.newBuilder() diff --git a/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporterEndToEndHttpTest.java b/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporterEndToEndHttpTest.java index c55d2c01c67..4ad9bdfb106 100644 --- a/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporterEndToEndHttpTest.java +++ b/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporterEndToEndHttpTest.java @@ -45,6 +45,7 @@ import zipkin2.Endpoint; import zipkin2.Span; import zipkin2.codec.SpanBytesDecoder; +import zipkin2.reporter.BytesMessageSender; import zipkin2.reporter.Encoding; import zipkin2.reporter.SpanBytesEncoder; import zipkin2.reporter.okhttp3.OkHttpSender; @@ -175,8 +176,10 @@ void testExportFailedAsWrongEncoderUsed() { private static ZipkinSpanExporter buildZipkinExporter( String endpoint, Encoding encoding, SpanBytesEncoder encoder, MeterProvider meterProvider) { + BytesMessageSender sender = + OkHttpSender.newBuilder().endpoint(endpoint).encoding(encoding).build(); return ZipkinSpanExporter.builder() - .setSender(OkHttpSender.newBuilder().endpoint(endpoint).encoding(encoding).build()) + .setSender(sender) .setEncoder(encoder) .setMeterProvider(meterProvider) .setLocalIpAddressSupplier(() -> localIp) 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 38e4534c71a..2917cfbefe6 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 @@ -9,8 +9,7 @@ import static io.opentelemetry.exporter.zipkin.ZipkinTestUtil.zipkinSpanBuilder; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -31,18 +30,15 @@ import org.mockito.junit.jupiter.MockitoExtension; import zipkin2.Span; import zipkin2.reporter.BytesEncoder; -import zipkin2.reporter.Call; -import zipkin2.reporter.Callback; +import zipkin2.reporter.BytesMessageSender; import zipkin2.reporter.Encoding; -import zipkin2.reporter.Sender; import zipkin2.reporter.SpanBytesEncoder; @ExtendWith(MockitoExtension.class) class ZipkinSpanExporterTest { - @Mock private Sender mockSender; + @Mock private BytesMessageSender mockSender; @Mock private SpanBytesEncoder mockEncoder; - @Mock private Call mockZipkinCall; @Mock private OtelToZipkinSpanTransformer mockTransformer; @Mock private InetAddress localIp; @@ -50,7 +46,7 @@ class ZipkinSpanExporterTest { LogCapturer logs = LogCapturer.create().captureForType(ZipkinSpanExporter.class); @Test - void testExport() { + void testExport() throws IOException { TestSpanData testSpanData = spanBuilder().build(); ZipkinSpanExporter zipkinSpanExporter = @@ -68,25 +64,18 @@ void testExport() { .build(); when(mockTransformer.generateSpan(testSpanData)).thenReturn(zipkinSpan); when(mockEncoder.encode(zipkinSpan)).thenReturn(someBytes); - when(mockSender.sendSpans(Collections.singletonList(someBytes))).thenReturn(mockZipkinCall); - doAnswer( - invocation -> { - Callback callback = invocation.getArgument(0); - callback.onSuccess(null); - return null; - }) - .when(mockZipkinCall) - .enqueue(any()); CompletableResultCode resultCode = zipkinSpanExporter.export(Collections.singleton(testSpanData)); assertThat(resultCode.isSuccess()).isTrue(); + + verify(mockSender).send(Collections.singletonList(someBytes)); } @Test @SuppressLogger(ZipkinSpanExporter.class) - void testExport_failed() { + void testExport_failed() throws IOException { TestSpanData testSpanData = spanBuilder().build(); ZipkinSpanExporter zipkinSpanExporter = @@ -104,20 +93,14 @@ void testExport_failed() { .build(); when(mockTransformer.generateSpan(testSpanData)).thenReturn(zipkinSpan); when(mockEncoder.encode(zipkinSpan)).thenReturn(someBytes); - when(mockSender.sendSpans(Collections.singletonList(someBytes))).thenReturn(mockZipkinCall); - doAnswer( - invocation -> { - Callback callback = invocation.getArgument(0); - callback.onError(new IOException()); - return null; - }) - .when(mockZipkinCall) - .enqueue(any()); + doThrow(new IOException()).when(mockSender).send(Collections.singletonList(someBytes)); CompletableResultCode resultCode = zipkinSpanExporter.export(Collections.singleton(testSpanData)); assertThat(resultCode.isSuccess()).isFalse(); + + verify(mockSender).send(Collections.singletonList(someBytes)); } @Test