Normalize usage of IdGenerator (only in TraceManager)
Also ensures TraceFilter sends traceId and spanId in response if they are generated in the request. Fixes gh-65
This commit is contained in:
@@ -19,12 +19,9 @@ package org.springframework.cloud.sleuth.instrument.scheduling;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.springframework.cloud.sleuth.MilliSpan;
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.cloud.sleuth.Trace;
|
||||
import org.springframework.cloud.sleuth.TraceManager;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.util.IdGenerator;
|
||||
|
||||
/**
|
||||
* Aspect that creates a new Span for running threads executing methods annotated with
|
||||
@@ -42,20 +39,14 @@ import org.springframework.util.IdGenerator;
|
||||
public class TraceSchedulingAspect {
|
||||
|
||||
private final TraceManager trace;
|
||||
private final IdGenerator idGenerator;
|
||||
|
||||
public TraceSchedulingAspect(TraceManager trace, IdGenerator idGenerator) {
|
||||
public TraceSchedulingAspect(TraceManager trace) {
|
||||
this.trace = trace;
|
||||
this.idGenerator = idGenerator;
|
||||
}
|
||||
|
||||
@Around("execution (@org.springframework.scheduling.annotation.Scheduled * *.*(..))")
|
||||
public Object traceBackgroundThread(final ProceedingJoinPoint pjp) throws Throwable {
|
||||
final Span span = this.trace.isTracing() ? this.trace.getCurrentSpan()
|
||||
: MilliSpan.builder().begin(System.currentTimeMillis())
|
||||
.traceId(createId()).spanId(createId())
|
||||
.build();
|
||||
Trace scope = this.trace.startSpan(pjp.toShortString(), span);
|
||||
Trace scope = this.trace.startSpan(pjp.toShortString());
|
||||
try {
|
||||
return pjp.proceed();
|
||||
}
|
||||
@@ -64,7 +55,4 @@ public class TraceSchedulingAspect {
|
||||
}
|
||||
}
|
||||
|
||||
private String createId() {
|
||||
return this.idGenerator.generateId().toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@ import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
import org.springframework.util.IdGenerator;
|
||||
|
||||
/**
|
||||
* Registers beans related to task scheduling.
|
||||
@@ -49,8 +48,8 @@ public class TraceSchedulingAutoConfiguration {
|
||||
|
||||
@ConditionalOnClass(ProceedingJoinPoint.class)
|
||||
@Bean
|
||||
public TraceSchedulingAspect traceSchedulingAspect(TraceManager trace, IdGenerator idGenerator) {
|
||||
return new TraceSchedulingAspect(trace, idGenerator);
|
||||
public TraceSchedulingAspect traceSchedulingAspect(TraceManager trace) {
|
||||
return new TraceSchedulingAspect(trace);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -55,13 +55,14 @@ import org.springframework.web.util.UrlPathHelper;
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE + 5)
|
||||
public class TraceFilter extends OncePerRequestFilter implements ApplicationEventPublisherAware {
|
||||
public class TraceFilter extends OncePerRequestFilter
|
||||
implements ApplicationEventPublisherAware {
|
||||
|
||||
protected static final String TRACE_REQUEST_ATTR = TraceFilter.class.getName()
|
||||
+ ".TRACE";
|
||||
|
||||
public static final Pattern DEFAULT_SKIP_PATTERN = Pattern
|
||||
.compile("/api-docs.*|/autoconfig|/configprops|/dump|/info|/metrics.*|/mappings|/trace|/swagger.*|.*\\.png|.*\\.css|.*\\.js|.*\\.html|/favicon.ico|/hystrix.stream");
|
||||
public static final Pattern DEFAULT_SKIP_PATTERN = Pattern.compile(
|
||||
"/api-docs.*|/autoconfig|/configprops|/dump|/info|/metrics.*|/mappings|/trace|/swagger.*|.*\\.png|.*\\.css|.*\\.js|.*\\.html|/favicon.ico|/hystrix.stream");
|
||||
|
||||
private final TraceManager traceManager;
|
||||
private final Pattern skipPattern;
|
||||
@@ -128,10 +129,10 @@ public class TraceFilter extends OncePerRequestFilter implements ApplicationEven
|
||||
publish(new ServerReceivedEvent(this, parent, trace.getSpan()));
|
||||
request.setAttribute(TRACE_REQUEST_ATTR, trace);
|
||||
// Send new span id back
|
||||
addToResponseIfNotPresent(response, Trace.TRACE_ID_NAME, trace.getSpan()
|
||||
.getTraceId());
|
||||
addToResponseIfNotPresent(response, Trace.SPAN_ID_NAME, trace.getSpan()
|
||||
.getSpanId());
|
||||
addToResponseIfNotPresent(response, Trace.TRACE_ID_NAME,
|
||||
trace.getSpan().getTraceId());
|
||||
addToResponseIfNotPresent(response, Trace.SPAN_ID_NAME,
|
||||
trace.getSpan().getSpanId());
|
||||
}
|
||||
else {
|
||||
trace = this.traceManager.startSpan(name);
|
||||
@@ -151,9 +152,11 @@ public class TraceFilter extends OncePerRequestFilter implements ApplicationEven
|
||||
return;
|
||||
}
|
||||
if (trace != null) {
|
||||
addResponseHeaders(response, trace.getSpan());
|
||||
addResponseAnnotations(response);
|
||||
if (trace.getSavedTrace()!=null) {
|
||||
publish(new ServerSentEvent(this, trace.getSavedTrace().getSpan(), trace.getSpan()));
|
||||
if (trace.getSavedTrace() != null) {
|
||||
publish(new ServerSentEvent(this, trace.getSavedTrace().getSpan(),
|
||||
trace.getSpan()));
|
||||
}
|
||||
// Double close to clean up the parent (remote span as well)
|
||||
this.traceManager.close(this.traceManager.close(trace));
|
||||
@@ -161,17 +164,24 @@ public class TraceFilter extends OncePerRequestFilter implements ApplicationEven
|
||||
}
|
||||
}
|
||||
|
||||
private void addResponseHeaders(HttpServletResponse response, Span span) {
|
||||
if (span != null) {
|
||||
response.addHeader(Trace.SPAN_ID_NAME, span.getSpanId());
|
||||
response.addHeader(Trace.TRACE_ID_NAME, span.getTraceId());
|
||||
}
|
||||
}
|
||||
|
||||
private void publish(ApplicationEvent event) {
|
||||
if (this.publisher!=null) {
|
||||
if (this.publisher != null) {
|
||||
this.publisher.publishEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: move annotation keys to constants
|
||||
// TODO: move annotation keys to constants
|
||||
protected void addRequestAnnotations(HttpServletRequest request) {
|
||||
String uri = this.urlPathHelper.getPathWithinApplication(request);
|
||||
this.traceManager.addAnnotation("/http/request/uri", request.getRequestURL()
|
||||
.toString());
|
||||
this.traceManager.addAnnotation("/http/request/uri",
|
||||
request.getRequestURL().toString());
|
||||
this.traceManager.addAnnotation("/http/request/endpoint", uri);
|
||||
this.traceManager.addAnnotation("/http/request/method", request.getMethod());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user