Make span name and process id properly optional

This commit is contained in:
Dave Syer
2015-08-12 16:22:03 +01:00
parent 47dee39bb3
commit b4d71727bf
9 changed files with 70 additions and 74 deletions

View File

@@ -53,9 +53,8 @@ public interface Trace {
String SPAN_ID_NAME = "X-Span-Id";
String TRACE_ID_NAME = "X-Trace-Id";
String SPAN_NAME_NAME = "X-Span-Name";
String PARENT_ID_NAME = "X-Parent-Id";
String PROCESS_ID_NAME = "X-Process-Id";
String NOT_SAMPLED_NAME = "X-Not-Sampled";
/**
* Creates a trace scope wrapping a new span.

View File

@@ -16,10 +16,9 @@
package org.springframework.cloud.sleuth.instrument.integration;
import static org.springframework.cloud.sleuth.Trace.NOT_SAMPLED_NAME;
import static org.springframework.cloud.sleuth.Trace.PARENT_ID_NAME;
import static org.springframework.cloud.sleuth.Trace.PROCESS_ID_NAME;
import static org.springframework.cloud.sleuth.Trace.SPAN_ID_NAME;
import static org.springframework.cloud.sleuth.Trace.SPAN_NAME_NAME;
import static org.springframework.cloud.sleuth.Trace.TRACE_ID_NAME;
import java.util.HashMap;
@@ -37,27 +36,28 @@ import org.springframework.messaging.Message;
public class SpanMessageHeaders {
public static Message<?> addSpanHeaders(Message<?> message, Span span) {
if (span==null) {
if (span == null) {
if (!message.getHeaders().containsKey(NOT_SAMPLED_NAME)) {
return MessageBuilder.fromMessage(message).setHeader(NOT_SAMPLED_NAME, "")
.build();
}
return message;
}
Map<String, String> headers = new HashMap<String, String>();
addHeader(headers, TRACE_ID_NAME, span.getTraceId());
addHeader(headers, SPAN_ID_NAME, span.getSpanId());
addHeader(headers, PARENT_ID_NAME, getFirst(span.getParents()));
addHeader(headers, SPAN_NAME_NAME, span.getName());
addHeader(headers, PROCESS_ID_NAME, span.getProcessId());
return MessageBuilder.fromMessage(message).copyHeaders(headers).build();
}
private static void addHeader(Map<String, String> headers, String name, String value) {
if (value!=null) {
if (value != null) {
headers.put(name, value);
}
}
private static String getFirst(List<String> parents) {
return parents==null || parents.isEmpty() ? null : parents.get(0);
return parents == null || parents.isEmpty() ? null : parents.get(0);
}
}

View File

@@ -16,10 +16,9 @@
package org.springframework.cloud.sleuth.instrument.integration;
import static org.springframework.cloud.sleuth.Trace.NOT_SAMPLED_NAME;
import static org.springframework.cloud.sleuth.Trace.PARENT_ID_NAME;
import static org.springframework.cloud.sleuth.Trace.PROCESS_ID_NAME;
import static org.springframework.cloud.sleuth.Trace.SPAN_ID_NAME;
import static org.springframework.cloud.sleuth.Trace.SPAN_NAME_NAME;
import static org.springframework.cloud.sleuth.Trace.TRACE_ID_NAME;
import static org.springframework.util.StringUtils.hasText;
@@ -47,7 +46,7 @@ public class TraceChannelInterceptor extends ChannelInterceptorAdapter {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
if (TraceContextHolder.isTracing()) {
if (TraceContextHolder.isTracing() || message.getHeaders().containsKey(NOT_SAMPLED_NAME)) {
return SpanMessageHeaders.addSpanHeaders(message, TraceContextHolder.getCurrentSpan());
}
String spanId = getHeader(message, SPAN_ID_NAME);
@@ -60,14 +59,6 @@ public class TraceChannelInterceptor extends ChannelInterceptorAdapter {
MilliSpanBuilder span = MilliSpan.builder().traceId(traceId).spanId(spanId);
String parentId = getHeader(message, PARENT_ID_NAME);
String processId = getHeader(message, PROCESS_ID_NAME);
String spanName = getHeader(message, SPAN_NAME_NAME);
if (spanName != null) {
span.name(spanName);
}
if (processId != null) {
span.processId(processId);
}
if (parentId != null) {
span.parent(parentId);
}

View File

@@ -17,9 +17,7 @@
package org.springframework.cloud.sleuth.instrument.integration;
import static org.springframework.cloud.sleuth.Trace.PARENT_ID_NAME;
import static org.springframework.cloud.sleuth.Trace.PROCESS_ID_NAME;
import static org.springframework.cloud.sleuth.Trace.SPAN_ID_NAME;
import static org.springframework.cloud.sleuth.Trace.SPAN_NAME_NAME;
import static org.springframework.cloud.sleuth.Trace.TRACE_ID_NAME;
import static org.springframework.cloud.sleuth.TraceContextHolder.getCurrentSpan;
import static org.springframework.cloud.sleuth.TraceContextHolder.setCurrentSpan;
@@ -144,15 +142,10 @@ implements ExecutorChannelInterceptor {
setHeader(headers, SPAN_ID_NAME, this.span.getSpanId());
setHeader(headers, TRACE_ID_NAME, this.span.getTraceId());
setHeader(headers, SPAN_NAME_NAME, this.span.getName());
String parentId = getParentId(getCurrentSpan());
if (parentId != null) {
setHeader(headers, PARENT_ID_NAME, parentId);
}
String processId = this.span.getProcessId();
if (processId != null) {
setHeader(headers, PROCESS_ID_NAME, processId);
}
this.messageHeaders = new MessageHeaders(headers);
}

View File

@@ -15,10 +15,9 @@
*/
package org.springframework.cloud.sleuth.instrument.web;
import static org.springframework.cloud.sleuth.Trace.NOT_SAMPLED_NAME;
import static org.springframework.cloud.sleuth.Trace.PARENT_ID_NAME;
import static org.springframework.cloud.sleuth.Trace.PROCESS_ID_NAME;
import static org.springframework.cloud.sleuth.Trace.SPAN_ID_NAME;
import static org.springframework.cloud.sleuth.Trace.SPAN_NAME_NAME;
import static org.springframework.cloud.sleuth.Trace.TRACE_ID_NAME;
import static org.springframework.util.StringUtils.hasText;
@@ -82,13 +81,15 @@ public class TraceFilter extends OncePerRequestFilter {
throws ServletException, IOException {
String uri = this.urlPathHelper.getPathWithinApplication(request);
boolean skip = this.skipPattern.matcher(uri).matches();
boolean skip = this.skipPattern.matcher(uri).matches() || getHeader(request, response, NOT_SAMPLED_NAME)!=null;
TraceScope traceScope = (TraceScope) request.getAttribute(TRACE_REQUEST_ATTR);
if (traceScope != null) {
this.trace.continueSpan(traceScope.getSpan());
}
else if (!skip) {
else if (skip) {
addToResponseIfNotPresent(response, NOT_SAMPLED_NAME, "");
} else {
String spanId = getHeader(request, response, SPAN_ID_NAME);
String traceId = getHeader(request, response, TRACE_ID_NAME);
String name = "http" + uri;
@@ -97,14 +98,6 @@ public class TraceFilter extends OncePerRequestFilter {
MilliSpanBuilder span = MilliSpan.builder().traceId(traceId)
.spanId(spanId);
String parentId = getHeader(request, response, PARENT_ID_NAME);
String processId = getHeader(request, response, PROCESS_ID_NAME);
String parentName = getHeader(request, response, SPAN_NAME_NAME);
if (parentName != null) {
span.name(parentName);
}
if (processId != null) {
span.processId(processId);
}
if (parentId != null) {
span.parent(parentId);
}

View File

@@ -16,9 +16,7 @@
package org.springframework.cloud.sleuth.instrument.web.client;
import static org.springframework.cloud.sleuth.Trace.PARENT_ID_NAME;
import static org.springframework.cloud.sleuth.Trace.PROCESS_ID_NAME;
import static org.springframework.cloud.sleuth.Trace.SPAN_ID_NAME;
import static org.springframework.cloud.sleuth.Trace.SPAN_NAME_NAME;
import static org.springframework.cloud.sleuth.Trace.TRACE_ID_NAME;
import static org.springframework.cloud.sleuth.TraceContextHolder.getCurrentSpan;
import static org.springframework.cloud.sleuth.TraceContextHolder.isTracing;
@@ -65,9 +63,7 @@ ApplicationEventPublisherAware {
}
setHeader(request, SPAN_ID_NAME, getCurrentSpan().getSpanId());
setHeader(request, TRACE_ID_NAME, getCurrentSpan().getTraceId());
setHeader(request, SPAN_NAME_NAME, getCurrentSpan().getName());
setHeader(request, PARENT_ID_NAME, getParentId(getCurrentSpan()));
setHeader(request, PROCESS_ID_NAME, getCurrentSpan().getProcessId());
publish(new ClientSentEvent(this, getCurrentSpan()));
return new TraceHttpResponse(this, execution.execute(request, body));
}

View File

@@ -17,6 +17,8 @@
package org.springframework.cloud.sleuth.instrument.integration;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.springframework.cloud.sleuth.Trace.NOT_SAMPLED_NAME;
import static org.springframework.cloud.sleuth.Trace.SPAN_ID_NAME;
import static org.springframework.cloud.sleuth.Trace.TRACE_ID_NAME;
@@ -48,7 +50,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes=App.class)
@SpringApplicationConfiguration(classes = App.class)
@IntegrationTest
@DirtiesContext
public class TraceChannelInterceptorTests implements MessageHandler {
@@ -78,6 +80,16 @@ public class TraceChannelInterceptorTests implements MessageHandler {
this.channel.unsubscribe(this);
}
@Test
public void testNoSpanCreation() {
this.channel.send(MessageBuilder.withPayload("hi").setHeader(NOT_SAMPLED_NAME, "")
.build());
assertNotNull("message was null", this.message);
String spanId = this.message.getHeaders().get(SPAN_ID_NAME, String.class);
assertNull("spanId was not null", spanId);
}
@Test
public void testSpanCreation() {
this.channel.send(MessageBuilder.withPayload("hi").build());
@@ -92,7 +104,8 @@ public class TraceChannelInterceptorTests implements MessageHandler {
@Test
public void testHeaderCreation() {
TraceScope traceScope = this.trace.startSpan("testSendMessage", new AlwaysSampler(), null);
TraceScope traceScope = this.trace.startSpan("testSendMessage",
new AlwaysSampler(), null);
this.channel.send(MessageBuilder.withPayload("hi").build());
traceScope.close();
assertNotNull("message was null", this.message);

View File

@@ -88,7 +88,7 @@ public class TraceRestTemplateInterceptorTests {
public Map<String, String> home(@RequestHeader HttpHeaders headers) {
Map<String, String> map = new HashMap<String, String>();
addHeaders(map, headers, Trace.SPAN_ID_NAME, Trace.TRACE_ID_NAME,
Trace.PARENT_ID_NAME, Trace.SPAN_NAME_NAME, Trace.PROCESS_ID_NAME);
Trace.PARENT_ID_NAME);
return map;
}