Merge pull request #185 from spring-cloud/latest-zipkin

Updates to latest zipkin, adding spring.zipkin.compression.enabled flag
This commit is contained in:
Adrian Cole
2016-02-25 23:17:53 +08:00
9 changed files with 73 additions and 18 deletions

View File

@@ -17,7 +17,7 @@
<brave.version>3.4.0</brave.version>
<spring-cloud-netflix.version>1.1.0.BUILD-SNAPSHOT</spring-cloud-netflix.version>
<aspectj.version>1.8.4</aspectj.version>
<zipkin-java.version>0.5.5</zipkin-java.version>
<zipkin-java.version>0.6.0</zipkin-java.version>
</properties>
<dependencyManagement>
<dependencies>

View File

@@ -62,12 +62,12 @@
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin</artifactId>
<version>0.5.5</version>
<version>0.6.0</version>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>0.5.5</version>
<version>0.6.0</version>
</dependency>
</dependencies>
</dependencyManagement>

View File

@@ -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

View File

@@ -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);
}
}

View File

@@ -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

View File

@@ -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<Span> 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;

View File

@@ -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

View File

@@ -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;
}
}
}

View File

@@ -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"));