Skip to content

Commit

Permalink
Merge pull request #87 from openzipkin/gzip-on-get
Browse files Browse the repository at this point in the history
Compresses GET responses
  • Loading branch information
adriancole committed Mar 2, 2016
2 parents 9dc6c8b + dca03a5 commit 08c889f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
18 changes: 17 additions & 1 deletion zipkin-junit/src/main/java/zipkin/junit/ZipkinRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import okio.Buffer;
import okio.GzipSink;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
Expand Down Expand Up @@ -51,7 +53,21 @@ public ZipkinRule() {
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
MockResponse maybeFailure = failureQueue.poll();
if (maybeFailure != null) return maybeFailure;
return successDispatch.dispatch(request);
MockResponse result = successDispatch.dispatch(request);
String encoding = request.getHeaders().get("Accept-Encoding");
if (result.getBody() != null && encoding != null && encoding.contains("gzip")) {
try {
Buffer sink = new Buffer();
GzipSink gzipSink = new GzipSink(sink);
gzipSink.write(result.getBody(), result.getBody().size());
gzipSink.close();
result.setBody(sink);
} catch (IOException e) {
throw new AssertionError(e);
}
result.setHeader("Content-Encoding", "gzip");
}
return result;
}

@Override
Expand Down
26 changes: 26 additions & 0 deletions zipkin-junit/src/test/java/zipkin/junit/ZipkinRuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import java.io.IOException;
import java.net.ConnectException;
import java.util.Arrays;
import java.util.List;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand All @@ -26,6 +28,7 @@
import zipkin.Codec;
import zipkin.Endpoint;
import zipkin.Span;
import zipkin.internal.Util;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -151,4 +154,27 @@ public void gzippedSpans_invalidIs400() throws IOException {

assertThat(response.code()).isEqualTo(400);
}

@Test
public void readSpans_gzippedResponse() throws Exception {
char[] annotation2K = new char[2048];
Arrays.fill(annotation2K, 'a');

List<Span> trace = asList(new Span.Builder(span)
.addAnnotation(Annotation.create(ann.timestamp, new String(annotation2K), null)).build());

zipkin.storeSpans(trace);

Response response = client.newCall(new Request.Builder()
.url(zipkin.httpUrl() + "/api/v1/trace/" + span.traceId)
.addHeader("Accept-Encoding", "gzip").build()
).execute();

assertThat(response.code()).isEqualTo(200);
assertThat(response.body().contentLength()).isLessThan(annotation2K.length);

byte[] unzipped = Util.gunzip(response.body().bytes());

assertThat(Codec.JSON.readSpans(unzipped)).isEqualTo(trace);
}
}
2 changes: 2 additions & 0 deletions zipkin-server/src/main/resources/zipkin-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ server:
port: ${QUERY_PORT:9411}
compression:
enabled: true
# compresses any response over min-response-size (default is 2KiB)
mime-types: application/json
spring:
datasource:
driver-class-name: org.mariadb.jdbc.Driver
Expand Down

0 comments on commit 08c889f

Please sign in to comment.