From 51b542e34b788179795174f34bab8a02885a1b65 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Thu, 25 Feb 2016 18:00:53 +0800 Subject: [PATCH] Updates to latest zipkin, adding spring.zipkin.compression.enabled flag This updates to latest zipkin, adding a flag to gzip spans before posting them. Note this is set to false by default as it requires servers to also be latest. We can flip the default once we can assume that. Ex. `spring.zipkin.compression.enabled = true` --- spring-cloud-sleuth-dependencies/pom.xml | 2 +- spring-cloud-sleuth-samples/pom.xml | 4 +-- .../docker-compose.yml | 6 ++-- .../test/java/integration/ZipkinTests.java | 2 +- spring-cloud-sleuth-zipkin/docker-compose.yml | 6 ++-- .../sleuth/zipkin/HttpZipkinSpanReporter.java | 16 ++++++---- .../zipkin/ZipkinAutoConfiguration.java | 2 +- .../cloud/sleuth/zipkin/ZipkinProperties.java | 23 ++++++++++++++ .../zipkin/HttpZipkinSpanReporterTest.java | 30 +++++++++++++++++-- 9 files changed, 73 insertions(+), 18 deletions(-) diff --git a/spring-cloud-sleuth-dependencies/pom.xml b/spring-cloud-sleuth-dependencies/pom.xml index f189a865b..b7806f348 100644 --- a/spring-cloud-sleuth-dependencies/pom.xml +++ b/spring-cloud-sleuth-dependencies/pom.xml @@ -17,7 +17,7 @@ 3.4.0 1.1.0.BUILD-SNAPSHOT 1.8.4 - 0.5.5 + 0.6.0 diff --git a/spring-cloud-sleuth-samples/pom.xml b/spring-cloud-sleuth-samples/pom.xml index 95cc70d70..15fc88916 100644 --- a/spring-cloud-sleuth-samples/pom.xml +++ b/spring-cloud-sleuth-samples/pom.xml @@ -62,12 +62,12 @@ io.zipkin.java zipkin - 0.5.5 + 0.6.0 io.zipkin.java zipkin-server - 0.5.5 + 0.6.0 diff --git a/spring-cloud-sleuth-samples/spring-cloud-sleuth-sample-zipkin/docker-compose.yml b/spring-cloud-sleuth-samples/spring-cloud-sleuth-sample-zipkin/docker-compose.yml index 7a83209fb..39d546d2b 100644 --- a/spring-cloud-sleuth-samples/spring-cloud-sleuth-sample-zipkin/docker-compose.yml +++ b/spring-cloud-sleuth-samples/spring-cloud-sleuth-sample-zipkin/docker-compose.yml @@ -1,9 +1,9 @@ mysql: - image: openzipkin/zipkin-mysql:1.33.2 + image: openzipkin/zipkin-mysql:1.34.0 ports: - 3306:3306 query: - image: openzipkin/zipkin-java:0.5.3 + image: openzipkin/zipkin-java:0.6.0 environment: # Remove TRANSPORT_TYPE to disable tracing - TRANSPORT_TYPE=http @@ -16,7 +16,7 @@ query: links: - mysql:storage web: - image: openzipkin/zipkin-web:1.33.2 + image: openzipkin/zipkin-web:1.34.0 environment: # Remove TRANSPORT_TYPE to disable tracing - TRANSPORT_TYPE=http diff --git a/spring-cloud-sleuth-samples/spring-cloud-sleuth-sample-zipkin/src/test/java/integration/ZipkinTests.java b/spring-cloud-sleuth-samples/spring-cloud-sleuth-sample-zipkin/src/test/java/integration/ZipkinTests.java index b775e4431..6a9178018 100644 --- a/spring-cloud-sleuth-samples/spring-cloud-sleuth-sample-zipkin/src/test/java/integration/ZipkinTests.java +++ b/spring-cloud-sleuth-samples/spring-cloud-sleuth-sample-zipkin/src/test/java/integration/ZipkinTests.java @@ -102,7 +102,7 @@ public class ZipkinTests extends AbstractIntegrationTest { private ZipkinSpanReporter getSpanCollector(ZipkinProperties zipkin, SpanReporterService spanReporterService) { return new HttpZipkinSpanReporter(zipkin.getBaseUrl(), zipkin.getFlushInterval(), - spanReporterService); + zipkin.getCompression().isEnabled(), spanReporterService); } } diff --git a/spring-cloud-sleuth-zipkin/docker-compose.yml b/spring-cloud-sleuth-zipkin/docker-compose.yml index 7a83209fb..39d546d2b 100644 --- a/spring-cloud-sleuth-zipkin/docker-compose.yml +++ b/spring-cloud-sleuth-zipkin/docker-compose.yml @@ -1,9 +1,9 @@ mysql: - image: openzipkin/zipkin-mysql:1.33.2 + image: openzipkin/zipkin-mysql:1.34.0 ports: - 3306:3306 query: - image: openzipkin/zipkin-java:0.5.3 + image: openzipkin/zipkin-java:0.6.0 environment: # Remove TRANSPORT_TYPE to disable tracing - TRANSPORT_TYPE=http @@ -16,7 +16,7 @@ query: links: - mysql:storage web: - image: openzipkin/zipkin-web:1.33.2 + image: openzipkin/zipkin-web:1.34.0 environment: # Remove TRANSPORT_TYPE to disable tracing - TRANSPORT_TYPE=http diff --git a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/HttpZipkinSpanReporter.java b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/HttpZipkinSpanReporter.java index bf46da371..2e3ba3b11 100644 --- a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/HttpZipkinSpanReporter.java +++ b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/HttpZipkinSpanReporter.java @@ -20,6 +20,7 @@ import org.springframework.cloud.sleuth.metric.SpanReporterService; import zipkin.Codec; import zipkin.Span; +import zipkin.internal.Util; import static java.util.concurrent.TimeUnit.SECONDS; @@ -39,17 +40,20 @@ public final class HttpZipkinSpanReporter private final String url; private final BlockingQueue pending = new LinkedBlockingQueue<>(1000); private final Flusher flusher; // Nullable for testing + private final boolean compressionEnabled; private final SpanReporterService spanReporterService; /** * @param baseUrl URL of the zipkin query server instance. Like: http://localhost:9411/ * @param flushInterval in seconds. 0 implies spans are {@link #flush() flushed} externally. + * @param compressionEnabled compress spans using gzip before posting to the zipkin server. * @param spanReporterService service to count number of accepted / dropped spans */ - public HttpZipkinSpanReporter(String baseUrl, int flushInterval, + public HttpZipkinSpanReporter(String baseUrl, int flushInterval, boolean compressionEnabled, SpanReporterService spanReporterService) { this.url = baseUrl + (baseUrl.endsWith("/") ? "" : "/") + "api/v1/spans"; this.flusher = flushInterval > 0 ? new Flusher(this, flushInterval) : null; + this.compressionEnabled = compressionEnabled; this.spanReporterService = spanReporterService; } @@ -129,19 +133,21 @@ public final class HttpZipkinSpanReporter HttpURLConnection connection = (HttpURLConnection) new URL(this.url).openConnection(); connection.setRequestMethod("POST"); connection.addRequestProperty("Content-Type", "application/json"); + if (this.compressionEnabled) { + connection.addRequestProperty("Content-Encoding", "gzip"); + json = Util.gzip(json); + } connection.setDoOutput(true); connection.setFixedLengthStreamingMode(json.length); connection.getOutputStream().write(json); try (InputStream in = connection.getInputStream()) { - while (in.read() != -1) - ; // skip + while (in.read() != -1); // skip } catch (IOException e) { try (InputStream err = connection.getErrorStream()) { if (err != null) { // possible, if the connection was dropped - while (err.read() != -1) - ; // skip + while (err.read() != -1); // skip } } throw e; diff --git a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinAutoConfiguration.java b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinAutoConfiguration.java index b604f2b9a..1367ab14f 100644 --- a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinAutoConfiguration.java +++ b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinAutoConfiguration.java @@ -54,7 +54,7 @@ public class ZipkinAutoConfiguration { @ConditionalOnMissingBean(ZipkinSpanReporter.class) public ZipkinSpanReporter reporter(SpanReporterService spanReporterService, ZipkinProperties zipkin) { return new HttpZipkinSpanReporter(zipkin.getBaseUrl(), zipkin.getFlushInterval(), - spanReporterService); + zipkin.getCompression().isEnabled(), spanReporterService); } @Bean diff --git a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinProperties.java b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinProperties.java index eaa075beb..bcb37fef0 100644 --- a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinProperties.java +++ b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinProperties.java @@ -31,6 +31,7 @@ public class ZipkinProperties { private String baseUrl = "http://localhost:9411/"; private boolean enabled = true; private int flushInterval = 1; + private Compression compression = new Compression(); public String getBaseUrl() { return this.baseUrl; @@ -44,6 +45,10 @@ public class ZipkinProperties { return this.flushInterval; } + public Compression getCompression() { + return this.compression; + } + public void setBaseUrl(String baseUrl) { this.baseUrl = baseUrl; } @@ -55,4 +60,22 @@ public class ZipkinProperties { public void setFlushInterval(int flushInterval) { this.flushInterval = flushInterval; } + + public void setCompression(Compression compression) { + this.compression = compression; + } + + /** When enabled, spans are gzipped before sent to the zipkin server */ + public static class Compression { + + private boolean enabled = false; + + public boolean isEnabled() { + return this.enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + } } diff --git a/spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin/HttpZipkinSpanReporterTest.java b/spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin/HttpZipkinSpanReporterTest.java index 76796ae28..83e081323 100644 --- a/spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin/HttpZipkinSpanReporterTest.java +++ b/spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin/HttpZipkinSpanReporterTest.java @@ -19,9 +19,12 @@ public class HttpZipkinSpanReporterTest { SpanReporterService spanReporterService = new CounterServiceBasedSpanReporterService("accepted", "dropped", this.inMemorySpanCounter); - // set flush interval to 0 so that tests can drive flushing explicitly HttpZipkinSpanReporter reporter = new HttpZipkinSpanReporter( - this.zipkin.httpUrl(), 0, this.spanReporterService); + this.zipkin.httpUrl(), + 0, // so that tests can drive flushing explicitly + false, // disable compression + this.spanReporterService + ); @Test public void reportDoesntDoIO() throws Exception { @@ -64,6 +67,29 @@ public class HttpZipkinSpanReporterTest { } @Test + public void postsCompressedSpans() throws Exception { + this.reporter = new HttpZipkinSpanReporter( + this.zipkin.httpUrl(), + 0, // so that tests can drive flushing explicitly + false, // enable compression + this.spanReporterService + ); + + this.reporter.report(span(1L, "foo")); + this.reporter.report(span(2L, "bar")); + + this.reporter.flush(); // manually flush the spans + + // Ensure only one request was sent + assertThat(this.zipkin.httpRequestCount()).isEqualTo(1); + + assertThat(this.zipkin.getTraces()).containsExactly( + asList(span(1L, "foo")), + asList(span(2L, "bar")) + ); + } + + @Test public void incrementsDroppedSpansWhenServerErrors() throws Exception { this.zipkin.enqueueFailure(HttpFailure.sendErrorResponse(500, "Ouch"));