This commit is contained in:
Spencer Gibb
2015-12-10 10:33:21 -07:00
parent 2f35fc7966
commit 2d0ff8f436
18 changed files with 59 additions and 59 deletions

View File

@@ -45,7 +45,7 @@ public class Trace {
SPAN_NAME_NAME, PARENT_ID_NAME, PROCESS_ID_NAME, NOT_SAMPLED_NAME);
/**
* the span for this scope
* the span for this trace
*/
private final Span span;

View File

@@ -33,8 +33,8 @@ public abstract class TraceDelegate<T> {
private final String name;
private final Span parent;
public TraceDelegate(TraceManager trace, T delegate) {
this(trace, delegate, null);
public TraceDelegate(TraceManager traceManager, T delegate) {
this(traceManager, delegate, null);
}
public TraceDelegate(TraceManager traceManager, T delegate, String name) {

View File

@@ -34,7 +34,7 @@ import org.springframework.messaging.support.ChannelInterceptorAdapter;
*/
public class TraceChannelInterceptor extends ChannelInterceptorAdapter {
private ThreadLocal<Trace> traceManagerScopeHolder = new ThreadLocal<Trace>();
private ThreadLocal<Trace> traceHolder = new ThreadLocal<>();
private final TraceManager traceManager;
@@ -44,9 +44,9 @@ public class TraceChannelInterceptor extends ChannelInterceptorAdapter {
@Override
public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
Trace traceManagerScope = this.traceManagerScopeHolder.get();
this.traceManager.close(traceManagerScope);
this.traceManagerScopeHolder.remove();
Trace trace = this.traceHolder.get();
this.traceManager.close(trace);
this.traceHolder.remove();
}
@Override
@@ -83,7 +83,7 @@ public class TraceChannelInterceptor extends ChannelInterceptorAdapter {
else {
trace = this.traceManager.startSpan(name);
}
this.traceManagerScopeHolder.set(trace);
this.traceHolder.set(trace);
return SpanMessageHeaders.addSpanHeaders(message, trace.getSpan());
}

View File

@@ -38,20 +38,20 @@ import org.springframework.scheduling.annotation.Scheduled;
@Aspect
public class TraceSchedulingAspect {
private final TraceManager trace;
private final TraceManager traceManager;
public TraceSchedulingAspect(TraceManager trace) {
this.trace = trace;
public TraceSchedulingAspect(TraceManager traceManager) {
this.traceManager = traceManager;
}
@Around("execution (@org.springframework.scheduling.annotation.Scheduled * *.*(..))")
public Object traceBackgroundThread(final ProceedingJoinPoint pjp) throws Throwable {
Trace scope = this.trace.startSpan(pjp.toShortString());
Trace trace = this.traceManager.startSpan(pjp.toShortString());
try {
return pjp.proceed();
}
finally {
this.trace.close(scope);
this.traceManager.close(trace);
}
}

View File

@@ -48,8 +48,8 @@ public class TraceSchedulingAutoConfiguration {
@ConditionalOnClass(ProceedingJoinPoint.class)
@Bean
public TraceSchedulingAspect traceSchedulingAspect(TraceManager trace) {
return new TraceSchedulingAspect(trace);
public TraceSchedulingAspect traceSchedulingAspect(TraceManager traceManager) {
return new TraceSchedulingAspect(traceManager);
}
}

View File

@@ -29,7 +29,7 @@ import org.springframework.web.servlet.ModelAndView;
*/
public class TraceHandlerInterceptor implements HandlerInterceptor {
private static final String ATTR_NAME = "__CURRENT_TRACE_HANDLER_TRACE_SCOPE_ATTR___";
private static final String ATTR_NAME = "__CURRENT_TRACE_HANDLER_TRACE_ATTR___";
private final TraceManager traceManager;

View File

@@ -19,5 +19,5 @@ package org.springframework.cloud.sleuth.template;
import org.springframework.cloud.sleuth.Trace;
public interface TraceCallback<T> {
T doInTrace(Trace traceScope);
T doInTrace(Trace trace);
}

View File

@@ -36,11 +36,11 @@ public class TraceTemplate implements TraceOperations {
public <T> T trace(final TraceCallback<T> callback) {
if (TraceContextHolder.isTracing()) {
DelegateCallback<T> delegate = new DelegateCallback<>(this.traceManager);
Trace traceScope = delegate.startSpan();
Trace trace = delegate.startSpan();
try {
return callback.doInTrace(traceScope);
return callback.doInTrace(trace);
} finally {
this.traceManager.close(traceScope);
this.traceManager.close(trace);
}
} else {
return callback.doInTrace(null);
@@ -49,8 +49,8 @@ public class TraceTemplate implements TraceOperations {
class DelegateCallback<T> extends TraceDelegate<TraceCallback<T>> {
public DelegateCallback(TraceManager trace) {
super(trace, null);
public DelegateCallback(TraceManager traceManager) {
super(traceManager, null);
}
@Override

View File

@@ -165,7 +165,7 @@ public class DefaultTraceManager implements TraceManager {
@Override
public Trace continueSpan(Span span) {
// Return an empty TraceScope that does nothing on close
// Return an empty Trace that does nothing on close
if (span == null) {
return NullTrace.INSTANCE;
}

View File

@@ -55,12 +55,12 @@ public class DefaultTraceManagerTests {
DefaultTraceManager traceManager = new DefaultTraceManager(new IsTracingSampler(),
new JdkIdGenerator(), publisher);
Trace scope = traceManager.startSpan(CREATE_SIMPLE_TRACE, new AlwaysSampler(), null);
Trace trace = traceManager.startSpan(CREATE_SIMPLE_TRACE, new AlwaysSampler(), null);
try {
importantWork1(traceManager);
}
finally {
traceManager.close(scope);
traceManager.close(trace);
}
verify(publisher, times(NUM_SPANS)).publishEvent(isA(SpanAcquiredEvent.class));

View File

@@ -63,7 +63,7 @@ public class TraceableExecutorServiceTests {
// https://github.com/spring-cloud/spring-cloud-sleuth/issues/60 comment two
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch latch = new CountDownLatch(this.TOTAL_THREADS);
Trace scope = this.traceManager.startSpan("PARENT");
Trace trace = this.traceManager.startSpan("PARENT");
for (int i = 0; i < this.TOTAL_THREADS; i++) {
this.traceManagerableExecutorService.execute(new MyRunnable(counter, latch));
}
@@ -74,7 +74,7 @@ public class TraceableExecutorServiceTests {
e.printStackTrace();
}
this.traceManager.close(scope);
this.traceManager.close(trace);
verify(this.publisher, times(this.NUM_SPANS)).publishEvent(isA(SpanAcquiredEvent.class));
verify(this.publisher, times(this.NUM_SPANS)).publishEvent(isA(SpanReleasedEvent.class));
@@ -97,7 +97,7 @@ public class TraceableExecutorServiceTests {
public void test_whenTraceContextOfWorkerThreadIsClosed_thenNoException() {
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch latch = new CountDownLatch(this.TOTAL_THREADS);
Trace scope = this.traceManager.startSpan("PARENT");
Trace trace = this.traceManager.startSpan("PARENT");
for (int i = 0; i < this.TOTAL_THREADS; i++) {
final Runnable command = new TraceRunnable(this.traceManager, new MyRunnable(counter, latch));
this.executorService.execute(command);
@@ -109,7 +109,7 @@ public class TraceableExecutorServiceTests {
e.printStackTrace();
}
this.traceManager.close(scope);
this.traceManager.close(trace);
verify(this.publisher, times(this.NUM_SPANS)).publishEvent(isA(SpanAcquiredEvent.class));
verify(this.publisher, times(this.NUM_SPANS)).publishEvent(isA(SpanReleasedEvent.class));

View File

@@ -57,7 +57,7 @@ public class TraceChannelInterceptorTests implements MessageHandler {
private DirectChannel channel;
@Autowired
private TraceManager trace;
private TraceManager traceManager;
private Message<?> message;
@@ -102,10 +102,10 @@ public class TraceChannelInterceptorTests implements MessageHandler {
@Test
public void testHeaderCreation() {
Trace traceScope = this.trace.startSpan("testSendMessage",
Trace trace = this.traceManager.startSpan("testSendMessage",
new AlwaysSampler(), null);
this.channel.send(MessageBuilder.withPayload("hi").build());
this.trace.close(traceScope);
this.traceManager.close(trace);
assertNotNull("message was null", this.message);
String spanId = this.message.getHeaders().get(Trace.SPAN_ID_NAME, String.class);

View File

@@ -55,7 +55,7 @@ public class TraceContextPropagationChannelInterceptorTests {
private PollableChannel channel;
@Autowired
private TraceManager trace;
private TraceManager traceManager;
@After
public void close() {
@@ -65,10 +65,10 @@ public class TraceContextPropagationChannelInterceptorTests {
@Test
public void testSpanPropagation() {
Trace traceScope = this.trace.startSpan("testSendMessage", new AlwaysSampler(), null);
Trace trace = this.traceManager.startSpan("testSendMessage", new AlwaysSampler(), null);
this.channel.send(MessageBuilder.withPayload("hi").build());
String expectedSpanId = traceScope.getSpan().getSpanId();
this.trace.close(traceScope);
String expectedSpanId = trace.getSpan().getSpanId();
this.traceManager.close(trace);
Message<?> message = this.channel.receive(0);

View File

@@ -55,7 +55,7 @@ public class TraceFilterTests {
@Mock
private ApplicationEventPublisher publisher;
private TraceManager trace;
private TraceManager traceManager;
private Span span;
@@ -67,7 +67,7 @@ public class TraceFilterTests {
@SneakyThrows
public void init() {
initMocks(this);
this.trace = new DefaultTraceManager(new AlwaysSampler(),
this.traceManager = new DefaultTraceManager(new AlwaysSampler(),
new JdkIdGenerator(), this.publisher) {
@Override
protected Trace createTrace(Trace trace, Span span) {
@@ -88,21 +88,21 @@ public class TraceFilterTests {
@Test
public void notTraced() throws Exception {
TraceManager trace = Mockito.mock(TraceManager.class);
TraceFilter filter = new TraceFilter(trace);
TraceManager mockTraceManager = Mockito.mock(TraceManager.class);
TraceFilter filter = new TraceFilter(mockTraceManager);
this.request = get("/favicon.ico").accept(MediaType.ALL)
.buildRequest(new MockServletContext());
filter.doFilter(this.request, this.response, this.filterChain);
verify(trace, never()).startSpan(anyString());
verify(trace, never()).close(any(Trace.class));
verify(mockTraceManager, never()).startSpan(anyString());
verify(mockTraceManager, never()).close(any(Trace.class));
}
@Test
public void startsNewTrace() throws Exception {
TraceFilter filter = new TraceFilter(this.trace);
TraceFilter filter = new TraceFilter(this.traceManager);
filter.doFilter(this.request, this.response, this.filterChain);
verifyHttpAnnotations();
assertNull(TraceContextHolder.getCurrentTrace());
@@ -111,10 +111,10 @@ public class TraceFilterTests {
@Test
public void continuesSpanInRequestAttr() throws Exception {
Trace traceScope = this.trace.startSpan("foo");
this.request.setAttribute(TraceFilter.TRACE_REQUEST_ATTR, traceScope);
Trace trace = this.traceManager.startSpan("foo");
this.request.setAttribute(TraceFilter.TRACE_REQUEST_ATTR, trace);
TraceFilter filter = new TraceFilter(this.trace);
TraceFilter filter = new TraceFilter(this.traceManager);
filter.doFilter(this.request, this.response, this.filterChain);
verifyHttpAnnotations();
@@ -128,7 +128,7 @@ public class TraceFilterTests {
.header(Trace.TRACE_ID_NAME, "mytrace")
.buildRequest(new MockServletContext());
TraceFilter filter = new TraceFilter(this.trace);
TraceFilter filter = new TraceFilter(this.traceManager);
filter.doFilter(this.request, this.response, this.filterChain);
verifyHttpAnnotations();

View File

@@ -27,7 +27,7 @@ public abstract class MvcWiremockITest extends MvcITest {
protected WireMock wireMock;
@Autowired protected HttpMockServer httpMockServer;
@Autowired protected TraceManager trace;
@Autowired protected TraceManager traceManager;
@Override
@Before
@@ -51,6 +51,6 @@ public abstract class MvcWiremockITest extends MvcITest {
@Override
protected void configureMockMvcBuilder(DefaultMockMvcBuilder mockMvcBuilder) {
mockMvcBuilder.addFilters(new TraceFilter(this.trace));
mockMvcBuilder.addFilters(new TraceFilter(this.traceManager));
}
}

View File

@@ -30,7 +30,7 @@ public class TraceTemplateTest {
private Trace whenTraceCallbackReturningCurrentTraceIsExecuted(TraceTemplate traceTemplate) {
return traceTemplate.trace(new TraceCallback<Trace>() {
@Override
public Trace doInTrace(Trace traceScope) {
public Trace doInTrace(Trace trace) {
return TraceContextHolder.getCurrentTrace();
}
});

View File

@@ -96,7 +96,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
@SneakyThrows
@RequestMapping("/traced")
public String traced() {
Trace scope = this.traceManager.startSpan("customTraceEndpoint",
Trace trace = this.traceManager.startSpan("customTraceEndpoint",
new AlwaysSampler(), null);
final Random random = new Random();
int millis = random.nextInt(1000);
@@ -106,7 +106,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
String s = this.restTemplate.getForObject("http://localhost:" + this.port
+ "/call", String.class);
this.traceManager.close(scope);
this.traceManager.close(trace);
return "traced/" + s;
}

View File

@@ -44,7 +44,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
@Autowired
private RestTemplate restTemplate;
@Autowired
private TraceManager trace;
private TraceManager traceManager;
@Autowired
private TraceAccessor accessor;
@Autowired
@@ -70,7 +70,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
SampleController.this.trace.addAnnotation("callable-sleep-millis", String.valueOf(millis));
SampleController.this.traceManager.addAnnotation("callable-sleep-millis", String.valueOf(millis));
Span currentSpan = SampleController.this.accessor.getCurrentSpan();
return "async hi: " + currentSpan;
}
@@ -89,24 +89,24 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
this.trace.addAnnotation("random-sleep-millis", String.valueOf(millis));
this.traceManager.addAnnotation("random-sleep-millis", String.valueOf(millis));
return "hi2";
}
@SneakyThrows
@RequestMapping("/traced")
public String traced() {
Trace scope = this.trace.startSpan("customTraceEndpoint",
Trace trace = this.traceManager.startSpan("customTraceEndpoint",
new AlwaysSampler(), null);
final Random random = new Random();
int millis = random.nextInt(1000);
log.info("Sleeping for {} millis", millis);
Thread.sleep(millis);
this.trace.addAnnotation("random-sleep-millis", String.valueOf(millis));
this.traceManager.addAnnotation("random-sleep-millis", String.valueOf(millis));
String s = this.restTemplate.getForObject("http://localhost:" + this.port
+ "/call", String.class);
this.trace.close(scope);
this.traceManager.close(trace);
return "traced/" + s;
}
@@ -117,7 +117,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
int millis = random.nextInt(1000);
log.info("Sleeping for {} millis", millis);
Thread.sleep(millis);
this.trace.addAnnotation("random-sleep-millis", String.valueOf(millis));
this.traceManager.addAnnotation("random-sleep-millis", String.valueOf(millis));
String s = this.restTemplate.getForObject("http://localhost:" + this.port
+ "/call", String.class);