Skip to content

Commit

Permalink
Tolerates 128bit trace ids in ZipkinRule (#1299)
Browse files Browse the repository at this point in the history
See #1298
  • Loading branch information
adriancole authored Sep 15, 2016
1 parent 90342b4 commit 2b17d95
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import zipkin.storage.SpanStore;
import zipkin.storage.StorageComponent;

import static zipkin.internal.Util.lowerHexToUnsignedLong;

final class ZipkinDispatcher extends Dispatcher {
private final SpanStore store;
private final Collector consumer;
Expand Down Expand Up @@ -66,7 +68,7 @@ public MockResponse dispatch(RecordedRequest request) {
return jsonResponse(Codec.JSON.writeTraces(store.getTraces(queryRequest)));
} else if (url.encodedPath().startsWith("/api/v1/trace/")) {
String traceId = url.encodedPath().replace("/api/v1/trace/", "");
long id = new Buffer().writeUtf8(traceId).readHexadecimalUnsignedLong();
long id = lowerHexToUnsignedLong(traceId);
List<Span> trace = url.queryParameterNames().contains("raw")
? store.getRawTrace(id) : store.getTrace(id);
if (trace != null) return jsonResponse(Codec.JSON.writeSpans(trace));
Expand Down
22 changes: 17 additions & 5 deletions zipkin-junit/src/test/java/zipkin/junit/ZipkinRuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import zipkin.Codec;
import zipkin.Span;

import static java.lang.String.format;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
Expand Down Expand Up @@ -58,7 +59,7 @@ public void getTraces_storedViaPost() throws IOException {
@Test
public void healthIsOK() throws IOException {
Response getResponse = client.newCall(new Request.Builder()
.url(zipkin.httpUrl() +"/health").build()
.url(zipkin.httpUrl() + "/health").build()
).execute();

assertThat(getResponse.code()).isEqualTo(200);
Expand All @@ -72,7 +73,7 @@ public void storeSpans_readbackHttp() throws IOException {

// read trace id using the the http api
Response getResponse = client.newCall(new Request.Builder()
.url(String.format("%s/api/v1/trace/%016x", zipkin.httpUrl(), traceId)).build()
.url(format("%s/api/v1/trace/%016x", zipkin.httpUrl(), traceId)).build()
).execute();

assertThat(getResponse.code()).isEqualTo(200);
Expand All @@ -87,19 +88,30 @@ public void storeSpans_readbackRaw() throws IOException {

// Default will merge by span id
Response defaultResponse = client.newCall(new Request.Builder()
.url(String.format("%s/api/v1/trace/%016x", zipkin.httpUrl(), traceId)).build()
.url(format("%s/api/v1/trace/%016x", zipkin.httpUrl(), traceId)).build()
).execute();

assertThat(Codec.JSON.readSpans(defaultResponse.body().bytes())).hasSize(TRACE.size());

// In the in-memory (or cassandra) stores, a raw read will show duplicate span rows.
Response rawResponse = client.newCall(new Request.Builder()
.url(String.format("%s/api/v1/trace/%016x?raw", zipkin.httpUrl(), traceId)).build()
.url(format("%s/api/v1/trace/%016x?raw", zipkin.httpUrl(), traceId)).build()
).execute();

assertThat(Codec.JSON.readSpans(rawResponse.body().bytes())).hasSize(TRACE.size() * 2);
}

@Test
public void downgrades128BitTraceIdToLower64Bits() throws Exception {
zipkin.storeSpans(TRACE);

Response getResponse = client.newCall(new Request.Builder()
.url(format("%s/api/v1/trace/48485a3953bb6124%016x", zipkin.httpUrl(), traceId)).build()
).execute();

assertThat(getResponse.code()).isEqualTo(200);
}

@Test
public void httpRequestCountIncrements() throws IOException {
postSpans(TRACE);
Expand Down Expand Up @@ -195,7 +207,7 @@ public void readSpans_gzippedResponse() throws Exception {
zipkin.storeSpans(trace);

Response response = client.newCall(new Request.Builder()
.url(String.format("%s/api/v1/trace/%016x", zipkin.httpUrl(), traceId))
.url(format("%s/api/v1/trace/%016x", zipkin.httpUrl(), traceId))
.addHeader("Accept-Encoding", "gzip").build()
).execute();

Expand Down

0 comments on commit 2b17d95

Please sign in to comment.