Skip to content

Commit f38f979

Browse files
Fixed missing logs for untraced first request
fixes #231
1 parent 5be6ebf commit f38f979

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed
77.1 KB
Loading

docs/src/main/asciidoc/intro.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ colorful labels. How does it happen that in Zipkin 10 spans are received?
8484
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*.
8585
Altogether *10* spans.
8686

87+
The dependency graph in Zipkin would look like this:
88+
89+
image::https://raw.githubusercontent.com/spring-cloud/spring-cloud-sleuth/master/docs/src/main/asciidoc/images/dependencies.png[Dependencies]
90+
8791
==== Log correlation
8892

8993
When grepping the logs of those four applications by trace id equal to e.g. `2485ec27856c56f4` one would get the following:

spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceFilter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,12 @@ protected void doFilterInternal(HttpServletRequest request,
136136
addResponseTags(response, exception);
137137
if (spanFromRequest.hasSavedSpan()) {
138138
Span parent = spanFromRequest.getSavedSpan();
139-
if (parent != null && parent.isRemote()) {
139+
if (parent.isRemote()) {
140140
parent.logEvent(Span.SERVER_SEND);
141141
this.spanReporter.report(parent);
142142
}
143+
} else {
144+
spanFromRequest.logEvent(Span.SERVER_SEND);
143145
}
144146
// Double close to clean up the parent (remote span as well)
145147
this.tracer.close(spanFromRequest);
@@ -171,6 +173,7 @@ private Span createSpan(HttpServletRequest request,
171173
else {
172174
spanFromRequest = this.tracer.createSpan(name);
173175
}
176+
spanFromRequest.logEvent(Span.SERVER_RECV);
174177
request.setAttribute(TRACE_REQUEST_ATTR, spanFromRequest);
175178
}
176179
return spanFromRequest;

spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceFilterIntegrationTests.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.web.bind.annotation.RequestMapping;
2626
import org.springframework.web.bind.annotation.RestController;
2727

28-
import static org.assertj.core.api.BDDAssertions.then;
28+
import static org.springframework.cloud.sleuth.assertions.SleuthAssertions.then;
2929
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
3030
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3131

@@ -61,6 +61,7 @@ public void should_create_and_return_trace_in_HTTP_header() throws Exception {
6161
MvcResult mvcResult = whenSentPingWithoutTracingData();
6262

6363
then(tracingHeaderFrom(mvcResult)).isNotNull();
64+
then(TraceFilterIntegrationTests.span).hasLoggedAnEvent(Span.SERVER_RECV).hasLoggedAnEvent(Span.SERVER_SEND);
6465
}
6566

6667
@Test
@@ -71,7 +72,7 @@ public void should_ignore_sampling_the_span_if_uri_matches_management_properties
7172
}
7273

7374
@Test
74-
public void when_correlationId_is_sent_should_not_create_a_new_one_but_return_the_existing_one_instead()
75+
public void when_traceId_is_sent_should_not_create_a_new_one_but_return_the_existing_one_instead()
7576
throws Exception {
7677
Long expectedTraceId = new Random().nextLong();
7778

@@ -81,7 +82,7 @@ public void when_correlationId_is_sent_should_not_create_a_new_one_but_return_th
8182
}
8283

8384
@Test
84-
public void when_correlationId_is_sent_to_async_endpoint_span_is_joined()
85+
public void when_traceId_is_sent_to_async_endpoint_span_is_joined()
8586
throws Exception {
8687
Long expectedTraceId = new Random().nextLong();
8788

@@ -115,16 +116,16 @@ private MvcResult whenSentFutureWithTraceId(Long passedTraceId) throws Exception
115116
return sendPingWithTraceId("/future", Span.TRACE_ID_NAME, passedTraceId);
116117
}
117118

118-
private MvcResult sendPingWithTraceId(String headerName, Long correlationId)
119+
private MvcResult sendPingWithTraceId(String headerName, Long traceId)
119120
throws Exception {
120-
return sendPingWithTraceId("/ping", headerName, correlationId);
121+
return sendPingWithTraceId("/ping", headerName, traceId);
121122
}
122123

123124
private MvcResult sendPingWithTraceId(String path, String headerName,
124-
Long correlationId) throws Exception {
125+
Long traceId) throws Exception {
125126
return this.mockMvc
126127
.perform(MockMvcRequestBuilders.get(path).accept(MediaType.TEXT_PLAIN)
127-
.header(headerName, Span.idToHex(correlationId))
128+
.header(headerName, Span.idToHex(traceId))
128129
.header(Span.SPAN_ID_NAME, Span.idToHex(new Random().nextLong())))
129130
.andReturn();
130131
}

0 commit comments

Comments
 (0)