Fixed missing logs for untraced first request

fixes #231
This commit is contained in:
Marcin Grzejszczak
2016-03-29 12:59:45 +02:00
parent 5be6ebf671
commit f38f979d13
4 changed files with 16 additions and 8 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

View File

@@ -84,6 +84,10 @@ colorful labels. How does it happen that in Zipkin 10 spans are received?
So 1 span from *A*, 2 spans from *B*, 1 span from *C*, 2 spans from *D*, 1 span from *E*, 2 spans from *F* and 1 from *G*.
Altogether *10* spans.
The dependency graph in Zipkin would look like this:
image::https://raw.githubusercontent.com/spring-cloud/spring-cloud-sleuth/master/docs/src/main/asciidoc/images/dependencies.png[Dependencies]
==== Log correlation
When grepping the logs of those four applications by trace id equal to e.g. `2485ec27856c56f4` one would get the following:

View File

@@ -136,10 +136,12 @@ public class TraceFilter extends OncePerRequestFilter {
addResponseTags(response, exception);
if (spanFromRequest.hasSavedSpan()) {
Span parent = spanFromRequest.getSavedSpan();
if (parent != null && parent.isRemote()) {
if (parent.isRemote()) {
parent.logEvent(Span.SERVER_SEND);
this.spanReporter.report(parent);
}
} else {
spanFromRequest.logEvent(Span.SERVER_SEND);
}
// Double close to clean up the parent (remote span as well)
this.tracer.close(spanFromRequest);
@@ -171,6 +173,7 @@ public class TraceFilter extends OncePerRequestFilter {
else {
spanFromRequest = this.tracer.createSpan(name);
}
spanFromRequest.logEvent(Span.SERVER_RECV);
request.setAttribute(TRACE_REQUEST_ATTR, spanFromRequest);
}
return spanFromRequest;

View File

@@ -25,7 +25,7 @@ import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.assertj.core.api.BDDAssertions.then;
import static org.springframework.cloud.sleuth.assertions.SleuthAssertions.then;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -61,6 +61,7 @@ public class TraceFilterIntegrationTests extends AbstractMvcIntegrationTest {
MvcResult mvcResult = whenSentPingWithoutTracingData();
then(tracingHeaderFrom(mvcResult)).isNotNull();
then(TraceFilterIntegrationTests.span).hasLoggedAnEvent(Span.SERVER_RECV).hasLoggedAnEvent(Span.SERVER_SEND);
}
@Test
@@ -71,7 +72,7 @@ public class TraceFilterIntegrationTests extends AbstractMvcIntegrationTest {
}
@Test
public void when_correlationId_is_sent_should_not_create_a_new_one_but_return_the_existing_one_instead()
public void when_traceId_is_sent_should_not_create_a_new_one_but_return_the_existing_one_instead()
throws Exception {
Long expectedTraceId = new Random().nextLong();
@@ -81,7 +82,7 @@ public class TraceFilterIntegrationTests extends AbstractMvcIntegrationTest {
}
@Test
public void when_correlationId_is_sent_to_async_endpoint_span_is_joined()
public void when_traceId_is_sent_to_async_endpoint_span_is_joined()
throws Exception {
Long expectedTraceId = new Random().nextLong();
@@ -115,16 +116,16 @@ public class TraceFilterIntegrationTests extends AbstractMvcIntegrationTest {
return sendPingWithTraceId("/future", Span.TRACE_ID_NAME, passedTraceId);
}
private MvcResult sendPingWithTraceId(String headerName, Long correlationId)
private MvcResult sendPingWithTraceId(String headerName, Long traceId)
throws Exception {
return sendPingWithTraceId("/ping", headerName, correlationId);
return sendPingWithTraceId("/ping", headerName, traceId);
}
private MvcResult sendPingWithTraceId(String path, String headerName,
Long correlationId) throws Exception {
Long traceId) throws Exception {
return this.mockMvc
.perform(MockMvcRequestBuilders.get(path).accept(MediaType.TEXT_PLAIN)
.header(headerName, Span.idToHex(correlationId))
.header(headerName, Span.idToHex(traceId))
.header(Span.SPAN_ID_NAME, Span.idToHex(new Random().nextLong())))
.andReturn();
}