Added a fix for 'null' in span names

This commit is contained in:
Marcin Grzejszczak
2016-03-03 22:22:58 +01:00
parent 373f4eb4ef
commit 9957769dd0
4 changed files with 29 additions and 14 deletions

View File

@@ -89,12 +89,16 @@ abstract class AbstractTraceHttpRequestInterceptor
*/
protected void publishStartEvent(HttpRequest request) {
URI uri = request.getURI();
String spanName = uri.getScheme() + ":" + uri.getPath();
String spanName = uriScheme(uri) + ":" + uri.getPath();
Span newSpan = this.tracer.startTrace(spanName);
enrichWithTraceHeaders(request, newSpan);
publish(new ClientSentEvent(this, newSpan));
}
private String uriScheme(URI uri) {
return uri.getScheme() == null ? "http" : uri.getScheme();
}
/**
* Close the current span and emit the ClientReceivedEvent
*/

View File

@@ -125,7 +125,7 @@ public class TraceFeignClientAutoConfiguration {
@Override
public void apply(RequestTemplate template) {
URI uri = URI.create(template.url());
String spanName = uri.getScheme() + ":" + uri.getPath();
String spanName = uriScheme(uri) + ":" + uri.getPath();
Span span = TraceFeignClientAutoConfiguration.this.tracer.startTrace(spanName);
if (span == null) {
setHeader(template, Span.NOT_SAMPLED_NAME, "true");
@@ -147,6 +147,10 @@ public class TraceFeignClientAutoConfiguration {
};
}
private String uriScheme(URI uri) {
return uri.getScheme() == null ? "http" : uri.getScheme();
}
private void publish(ApplicationEvent event) {
if (this.publisher != null) {
this.publisher.publishEvent(event);

View File

@@ -16,10 +16,7 @@
package org.springframework.cloud.sleuth.instrument.web.client;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
@@ -40,8 +37,6 @@ import org.springframework.http.client.ClientHttpResponse;
public class TraceRestTemplateInterceptor extends AbstractTraceHttpRequestInterceptor
implements ClientHttpRequestInterceptor {
private static final Log log = LogFactory.getLog(MethodHandles.lookup().lookupClass());
public TraceRestTemplateInterceptor(Tracer tracer) {
super(tracer);
}
@@ -67,5 +62,4 @@ public class TraceRestTemplateInterceptor extends AbstractTraceHttpRequestInterc
}
}
}

View File

@@ -27,7 +27,6 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.cloud.sleuth.DefaultSpanNamer;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.assertions.SleuthAssertions;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.cloud.sleuth.trace.DefaultTracer;
import org.springframework.cloud.sleuth.trace.TestSpanContextHolder;
@@ -42,8 +41,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.BDDAssertions.then;
import static org.junit.Assert.assertFalse;
import static org.springframework.cloud.sleuth.assertions.SleuthAssertions.then;
/**
* @author Dave Syer
@@ -51,7 +50,9 @@ import static org.junit.Assert.assertFalse;
*/
public class TraceRestTemplateInterceptorTests {
private MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new TestController())
private TestController testController = new TestController();
private MockMvc mockMvc = MockMvcBuilders.standaloneSetup(this.testController)
.build();
private RestTemplate template = new RestTemplate(
@@ -115,18 +116,30 @@ public class TraceRestTemplateInterceptorTests {
this.template.getForEntity("/exception", Map.class).getBody();
Assert.fail("should throw an exception");
} catch (RuntimeException e) {
SleuthAssertions.then(e).hasMessage("500 Internal Server Error");
then(e).hasMessage("500 Internal Server Error");
}
SleuthAssertions.then(this.tracer.getCurrentSpan()).isEqualTo(span);
then(this.tracer.getCurrentSpan()).isEqualTo(span);
this.tracer.close(span);
}
@Test
public void createdSpanNameDoesNotHaveNullInName() {
this.tracer.continueSpan(Span.builder().traceId(1L).spanId(2L).exportable(false).build());
this.template.getForEntity("/", Map.class).getBody();
then(this.testController.span).hasNameEqualTo("http:/");
}
@RestController
public static class TestController {
public class TestController {
Span span;
@RequestMapping("/")
public Map<String, String> home(@RequestHeader HttpHeaders headers) {
this.span = TraceRestTemplateInterceptorTests.this.tracer.getCurrentSpan();
Map<String, String> map = new HashMap<String, String>();
addHeaders(map, headers, Span.SPAN_ID_NAME, Span.TRACE_ID_NAME,
Span.PARENT_ID_NAME, Span.NOT_SAMPLED_NAME);