Updates to get tracing working on sample app.
This commit is contained in:
@@ -19,4 +19,8 @@ public class SpanHolder {
|
||||
}
|
||||
currentSpan.set(span);
|
||||
}
|
||||
|
||||
public static boolean isTracing() {
|
||||
return currentSpan.get() != null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ public class TraceAutoConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public Sampler defaultSampler() {
|
||||
return new IsTracingSampler();
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ package org.springframework.cloud.sleuth.scheduling;
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.cloud.sleuth.Trace;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -21,6 +23,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
@EnableAspectJAutoProxy
|
||||
@ConditionalOnClass(ProceedingJoinPoint.class)
|
||||
public class TraceSchedulingAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -16,7 +16,8 @@ public class Slf4jSpanStartListener implements SpanStartListener {
|
||||
@Override
|
||||
public void startSpan(Span span) {
|
||||
//TODO: what log level?
|
||||
log.info("Starting span with id: [{}]", span.getSpanId());
|
||||
log.info("Starting span: {}", span);
|
||||
MDC.put(Trace.SPAN_ID_NAME, span.getSpanId());
|
||||
MDC.put(Trace.TRACE_ID_NAME, span.getTraceId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,10 @@ package org.springframework.cloud.sleuth.web;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
@@ -26,6 +28,7 @@ import org.springframework.boot.context.embedded.FilterRegistrationBean;
|
||||
import org.springframework.cloud.sleuth.Trace;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -37,8 +40,10 @@ import org.springframework.util.StringUtils;
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Configuration
|
||||
@EnableAspectJAutoProxy
|
||||
@ConditionalOnProperty(value = "spring.cloud.sleuth.trace.web.enabled", matchIfMissing = true)
|
||||
@ConditionalOnWebApplication
|
||||
@ConditionalOnClass(ProceedingJoinPoint.class)
|
||||
public class TraceWebAutoConfiguration {
|
||||
|
||||
/**
|
||||
|
||||
@@ -42,15 +42,15 @@ public class TraceRestTemplateInterceptor implements ClientHttpRequestIntercepto
|
||||
@Override
|
||||
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
|
||||
ClientHttpRequestExecution execution) throws IOException {
|
||||
if (!request.getHeaders().containsKey(SPAN_ID_NAME)) {
|
||||
request.getHeaders().add(SPAN_ID_NAME,
|
||||
SpanHolder.getCurrentSpan().getSpanId());
|
||||
}
|
||||
if (!request.getHeaders().containsKey(TRACE_ID_NAME)) {
|
||||
request.getHeaders().add(TRACE_ID_NAME,
|
||||
SpanHolder.getCurrentSpan().getSpanId());
|
||||
}
|
||||
setHeader(request, SPAN_ID_NAME, SpanHolder.getCurrentSpan().getSpanId());
|
||||
setHeader(request, TRACE_ID_NAME, SpanHolder.getCurrentSpan().getTraceId());
|
||||
return execution.execute(request, body);
|
||||
}
|
||||
|
||||
public void setHeader(HttpRequest request, String spanIdName, String spanId) {
|
||||
if (!request.getHeaders().containsKey(spanIdName) && SpanHolder.isTracing()) {
|
||||
request.getHeaders().add(spanIdName, spanId);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user