add /messaging annotations

This commit is contained in:
Spencer Gibb
2015-08-19 12:09:14 -06:00
parent 821e9536b9
commit 0f76427ebf
10 changed files with 63 additions and 27 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.cloud.sleuth;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
/**
@@ -58,6 +60,9 @@ public interface Trace {
String PROCESS_ID_NAME = "X-Process-Id";
String NOT_SAMPLED_NAME = "X-Not-Sampled";
List<String> HEADERS = Arrays.asList(SPAN_ID_NAME, TRACE_ID_NAME,
SPAN_NAME_NAME, PARENT_ID_NAME, PROCESS_ID_NAME, NOT_SAMPLED_NAME);
/**
* Creates a trace scope wrapping a new span.
* <p/>
@@ -99,7 +104,7 @@ public interface Trace {
/**
* Adds a data annotation to the current span if tracing is currently on.
*/
void addKVAnnotation(String key, String value);
void addAnnotation(String key, String value);
<V> Callable<V> wrap(Callable<V> callable);

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.sleuth.instrument.integration;
import static org.springframework.cloud.sleuth.Trace.HEADERS;
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;
@@ -45,7 +46,10 @@ public class SpanMessageHeaders {
}
return message;
}
Map<String, String> headers = new HashMap<String, String>();
addAnnotations(message, span);
Map<String, String> headers = new HashMap<>();
addHeader(headers, TRACE_ID_NAME, span.getTraceId());
addHeader(headers, SPAN_ID_NAME, span.getSpanId());
addHeader(headers, PARENT_ID_NAME, getFirst(span.getParents()));
@@ -54,6 +58,32 @@ public class SpanMessageHeaders {
return MessageBuilder.fromMessage(message).copyHeaders(headers).build();
}
public static void addAnnotations(Message<?> message, Span span) {
for ( Map.Entry<String, Object> entry : message.getHeaders().entrySet()) {
if (!HEADERS.contains(entry.getKey())) { //filter out trace headers
String key = "/messaging/headers/" + entry.getKey().toLowerCase();
String value = null;
if (entry.getValue() != null) {
value = entry.getValue().toString(); //TODO: better way to serialize?
}
span.addAnnotation(key, value);
}
}
Object payload = message.getPayload();
if (payload != null) {
span.addAnnotation("/messaging/payload/type",
payload.getClass().getCanonicalName());
if (payload instanceof String) {
span.addAnnotation("/messaging/payload/size",
String.valueOf(((String)payload).length()));
} else if (payload instanceof byte[]) {
span.addAnnotation("/messaging/payload/size",
String.valueOf(((byte[])payload).length));
}
}
}
private static void addHeader(Map<String, String> headers, String name, String value) {
if (value != null) {
headers.put(name, value);

View File

@@ -150,12 +150,13 @@ public class TraceFilter extends OncePerRequestFilter {
}
}
//TODO: move annotation keys to constants
protected void addRequestAnnotations(HttpServletRequest request) {
String uri = this.urlPathHelper.getPathWithinApplication(request);
this.trace.addKVAnnotation("/http/request/uri", request.getRequestURL()
this.trace.addAnnotation("/http/request/uri", request.getRequestURL()
.toString());
this.trace.addKVAnnotation("/http/request/endpoint", uri);
this.trace.addKVAnnotation("/http/request/method", request.getMethod());
this.trace.addAnnotation("/http/request/endpoint", uri);
this.trace.addAnnotation("/http/request/method", request.getMethod());
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
@@ -164,20 +165,20 @@ public class TraceFilter extends OncePerRequestFilter {
while (values.hasMoreElements()) {
String value = values.nextElement();
String key = "/http/request/headers/" + name.toLowerCase();
this.trace.addKVAnnotation(key, value);
this.trace.addAnnotation(key, value);
}
}
}
private void addResponseAnnotations(HttpServletResponse response) {
this.trace.addKVAnnotation("/http/response/status_code",
this.trace.addAnnotation("/http/response/status_code",
String.valueOf(response.getStatus()));
for (String name : response.getHeaderNames()) {
for (String value : response.getHeaders(name)) {
String key = "/http/response/headers/" + name.toLowerCase();
this.trace.addKVAnnotation(key, value);
this.trace.addAnnotation(key, value);
}
}
}

View File

@@ -117,7 +117,7 @@ public class DefaultTrace implements Trace {
}
@Override
public void addKVAnnotation(String key, String value) {
public void addAnnotation(String key, String value) {
Span s = getCurrentSpan();
if (s != null) {
s.addAnnotation(key, value);

View File

@@ -130,13 +130,13 @@ public class TraceFilterTests {
public void verifyHttpAnnotations() {
verify(this.trace).addKVAnnotation("/http/request/uri", "http://localhost/");
verify(this.trace).addKVAnnotation("/http/request/endpoint", "/");
verify(this.trace).addKVAnnotation("/http/request/method", "GET");
verify(this.trace).addKVAnnotation("/http/request/headers/accept", MediaType.APPLICATION_JSON_VALUE);
verify(this.trace).addKVAnnotation("/http/request/headers/user-agent", "MockMvc");
verify(this.trace).addAnnotation("/http/request/uri", "http://localhost/");
verify(this.trace).addAnnotation("/http/request/endpoint", "/");
verify(this.trace).addAnnotation("/http/request/method", "GET");
verify(this.trace).addAnnotation("/http/request/headers/accept", MediaType.APPLICATION_JSON_VALUE);
verify(this.trace).addAnnotation("/http/request/headers/user-agent", "MockMvc");
verify(this.trace).addKVAnnotation("/http/response/status_code", HttpStatus.OK.toString());
verify(this.trace).addKVAnnotation("/http/response/headers/content-type", MediaType.APPLICATION_JSON_VALUE);
verify(this.trace).addAnnotation("/http/response/status_code", HttpStatus.OK.toString());
verify(this.trace).addAnnotation("/http/response/headers/content-type", MediaType.APPLICATION_JSON_VALUE);
}
}

View File

@@ -40,7 +40,7 @@ public class SampleBackground {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
this.trace.addKVAnnotation("background-sleep-millis", String.valueOf(millis));
this.trace.addAnnotation("background-sleep-millis", String.valueOf(millis));
}
}

View File

@@ -40,7 +40,7 @@ public class SampleBackground {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
this.trace.addKVAnnotation("background-sleep-millis", String.valueOf(millis));
this.trace.addAnnotation("background-sleep-millis", String.valueOf(millis));
}
}

View File

@@ -68,7 +68,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
SampleController.this.trace.addKVAnnotation("callable-sleep-millis", String.valueOf(millis));
SampleController.this.trace.addAnnotation("callable-sleep-millis", String.valueOf(millis));
Span currentSpan = TraceContextHolder.getCurrentSpan();
return "async hi: " + currentSpan;
}
@@ -87,7 +87,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
this.trace.addKVAnnotation("random-sleep-millis", String.valueOf(millis));
this.trace.addAnnotation("random-sleep-millis", String.valueOf(millis));
return "hi2";
}
@@ -100,7 +100,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
int millis = random.nextInt(1000);
log.info("Sleeping for {} millis", millis);
Thread.sleep(millis);
this.trace.addKVAnnotation("random-sleep-millis", String.valueOf(millis));
this.trace.addAnnotation("random-sleep-millis", String.valueOf(millis));
String s = this.restTemplate.getForObject("http://localhost:" + this.port
+ "/call", String.class);
@@ -115,7 +115,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
int millis = random.nextInt(1000);
log.info("Sleeping for {} millis", millis);
Thread.sleep(millis);
this.trace.addKVAnnotation("random-sleep-millis", String.valueOf(millis));
this.trace.addAnnotation("random-sleep-millis", String.valueOf(millis));
String s = this.restTemplate.getForObject("http://localhost:" + this.port
+ "/call", String.class);

View File

@@ -40,7 +40,7 @@ public class SampleBackground {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
this.trace.addKVAnnotation("background-sleep-millis", String.valueOf(millis));
this.trace.addAnnotation("background-sleep-millis", String.valueOf(millis));
}
}

View File

@@ -68,7 +68,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
SampleController.this.trace.addKVAnnotation("callable-sleep-millis", String.valueOf(millis));
SampleController.this.trace.addAnnotation("callable-sleep-millis", String.valueOf(millis));
Span currentSpan = TraceContextHolder.getCurrentSpan();
return "async hi: " + currentSpan;
}
@@ -87,7 +87,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
this.trace.addKVAnnotation("random-sleep-millis", String.valueOf(millis));
this.trace.addAnnotation("random-sleep-millis", String.valueOf(millis));
return "hi2";
}
@@ -100,7 +100,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
int millis = random.nextInt(1000);
log.info("Sleeping for {} millis", millis);
Thread.sleep(millis);
this.trace.addKVAnnotation("random-sleep-millis", String.valueOf(millis));
this.trace.addAnnotation("random-sleep-millis", String.valueOf(millis));
String s = this.restTemplate.getForObject("http://localhost:" + this.port
+ "/call", String.class);
@@ -115,7 +115,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
int millis = random.nextInt(1000);
log.info("Sleeping for {} millis", millis);
Thread.sleep(millis);
this.trace.addKVAnnotation("random-sleep-millis", String.valueOf(millis));
this.trace.addAnnotation("random-sleep-millis", String.valueOf(millis));
String s = this.restTemplate.getForObject("http://localhost:" + this.port
+ "/call", String.class);