Skip to content

Commit

Permalink
Update dependency io.zipkin.reporter2:zipkin-reporter-bom to v3.2.1
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Adrian Cole committed Jan 14, 2024
1 parent dbaba2f commit 8905680
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 60 deletions.
2 changes: 1 addition & 1 deletion dependencyManagement/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a
Expand All @@ -40,15 +39,15 @@ public final class ZipkinSpanExporter implements SpanExporter {
private final AtomicBoolean isShutdown = new AtomicBoolean();
private final ZipkinSpanExporterBuilder builder;
private final BytesEncoder<Span> encoder;
private final Sender sender;
private final BytesMessageSender sender;
private final ExporterMetrics exporterMetrics;

private final OtelToZipkinSpanTransformer transformer;

ZipkinSpanExporter(
ZipkinSpanExporterBuilder builder,
BytesEncoder<Span> encoder,
Sender sender,
BytesMessageSender sender,
Supplier<MeterProvider> meterProviderSupplier,
OtelToZipkinSpanTransformer transformer) {
this.builder = builder;
Expand Down Expand Up @@ -76,25 +75,15 @@ public CompletableResultCode export(Collection<SpanData> spanDataList) {
encodedSpans.add(encoder.encode(zipkinSpan));
}

CompletableResultCode result = new CompletableResultCode();
sender
.sendSpans(encodedSpans)
.enqueue(
new Callback<Void>() {
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
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;

/** Builder class for {@link ZipkinSpanExporter}. */
public final class ZipkinSpanExporterBuilder {
private BytesEncoder<Span> encoder = SpanBytesEncoder.JSON_V2;
private Supplier<InetAddress> 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)
Expand All @@ -38,20 +38,20 @@ public final class ZipkinSpanExporterBuilder {
* Sets the Zipkin sender. Implements the client side of the span transport. An {@link
* OkHttpSender} is a good default.
*
* <p>The {@link Sender#close()} method will be called when the exporter is shut down.
* <p>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(Sender sender) {
public ZipkinSpanExporterBuilder setSender(BytesMessageSender sender) {
requireNonNull(sender, "sender");
this.sender = sender;
return this;
}

/**
* 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.
Expand All @@ -65,8 +65,8 @@ public ZipkinSpanExporterBuilder setEncoder(zipkin2.codec.BytesEncoder<Span> 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.
Expand Down Expand Up @@ -114,7 +114,7 @@ public ZipkinSpanExporterBuilder setEndpoint(String endpoint) {
* supported compression methods include "gzip" and "none".
*
* <p>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.
Expand Down Expand Up @@ -189,7 +189,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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,26 +30,23 @@
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<Void> mockZipkinCall;
@Mock private OtelToZipkinSpanTransformer mockTransformer;
@Mock private InetAddress localIp;

@RegisterExtension
LogCapturer logs = LogCapturer.create().captureForType(ZipkinSpanExporter.class);

@Test
void testExport() {
void testExport() throws IOException {
TestSpanData testSpanData = spanBuilder().build();

ZipkinSpanExporter zipkinSpanExporter =
Expand All @@ -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<Void> 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 =
Expand All @@ -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<Void> 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
Expand Down

0 comments on commit 8905680

Please sign in to comment.