Switch to Spring Core IdGenerator

This commit is contained in:
Dave Syer
2015-12-01 16:44:48 +00:00
parent c0e5c865dd
commit fcb12c8f5e
13 changed files with 70 additions and 99 deletions

View File

@@ -1,24 +0,0 @@
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth;
/**
* @author Spencer Gibb
*/
public interface IdGenerator {
String create();
}

View File

@@ -1,32 +0,0 @@
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth.autoconfig;
import java.util.UUID;
import org.springframework.cloud.sleuth.IdGenerator;
/**
* @author Spencer Gibb
*/
public class RandomUuidGenerator implements IdGenerator {
@Override
public String create() {
return UUID.randomUUID().toString();
}
}

View File

@@ -18,13 +18,14 @@ package org.springframework.cloud.sleuth.autoconfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.sleuth.IdGenerator;
import org.springframework.cloud.sleuth.Sampler;
import org.springframework.cloud.sleuth.sampler.IsTracingSampler;
import org.springframework.cloud.sleuth.trace.DefaultTraceManager;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.IdGenerator;
import org.springframework.util.JdkIdGenerator;
/**
* @author Spencer Gibb
@@ -36,7 +37,7 @@ public class TraceAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public IdGenerator traceIdGenerator() {
return new RandomUuidGenerator();
return new JdkIdGenerator();
}
@Bean

View File

@@ -19,12 +19,12 @@ 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.IdGenerator;
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
@@ -53,7 +53,7 @@ public class TraceSchedulingAspect {
public Object traceBackgroundThread(final ProceedingJoinPoint pjp) throws Throwable {
final Span span = this.trace.isTracing() ? this.trace.getCurrentSpan()
: MilliSpan.builder().begin(System.currentTimeMillis())
.traceId(this.idGenerator.create()).spanId(this.idGenerator.create())
.traceId(createId()).spanId(createId())
.build();
Trace scope = this.trace.startSpan(pjp.toShortString(), span);
try {
@@ -63,4 +63,8 @@ public class TraceSchedulingAspect {
this.trace.close(scope);
}
}
private String createId() {
return this.idGenerator.generateId().toString();
}
}

View File

@@ -25,12 +25,12 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.sleuth.IdGenerator;
import org.springframework.cloud.sleuth.TraceManager;
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.

View File

@@ -20,7 +20,6 @@ import static org.springframework.cloud.sleuth.util.ExceptionUtils.error;
import java.util.concurrent.Callable;
import org.springframework.cloud.sleuth.IdGenerator;
import org.springframework.cloud.sleuth.MilliSpan;
import org.springframework.cloud.sleuth.Sampler;
import org.springframework.cloud.sleuth.Span;
@@ -33,6 +32,7 @@ import org.springframework.cloud.sleuth.instrument.TraceCallable;
import org.springframework.cloud.sleuth.instrument.TraceRunnable;
import org.springframework.cloud.sleuth.util.ExceptionUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.util.IdGenerator;
/**
* @author Spencer Gibb
@@ -137,8 +137,8 @@ public class DefaultTraceManager implements TraceManager {
protected Span createChild(Span parent, String name) {
if (parent == null) {
MilliSpan span = MilliSpan.builder().begin(System.currentTimeMillis())
.name(name).traceId(this.idGenerator.create())
.spanId(this.idGenerator.create()).build();
.name(name).traceId(createId())
.spanId(createId()).build();
this.publisher.publishEvent(new SpanAcquiredEvent(this, span));
return span;
}
@@ -149,13 +149,17 @@ public class DefaultTraceManager implements TraceManager {
}
MilliSpan span = MilliSpan.builder().begin(System.currentTimeMillis())
.name(name).traceId(parent.getTraceId()).parent(parent.getSpanId())
.spanId(this.idGenerator.create()).processId(parent.getProcessId())
.spanId(createId()).processId(parent.getProcessId())
.build();
this.publisher.publishEvent(new SpanAcquiredEvent(this, parent, span));
return span;
}
}
private String createId() {
return this.idGenerator.generateId().toString();
}
@Override
public Trace continueSpan(Span span) {
// Return an empty TraceScope that does nothing on close

View File

@@ -29,7 +29,6 @@ import java.util.List;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.cloud.sleuth.autoconfig.RandomUuidGenerator;
import org.springframework.cloud.sleuth.event.SpanAcquiredEvent;
import org.springframework.cloud.sleuth.event.SpanReleasedEvent;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
@@ -37,6 +36,7 @@ import org.springframework.cloud.sleuth.sampler.IsTracingSampler;
import org.springframework.cloud.sleuth.trace.DefaultTraceManager;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.util.JdkIdGenerator;
/**
* @author Spencer Gibb
@@ -53,7 +53,7 @@ public class DefaultTraceManagerTests {
ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
DefaultTraceManager traceManager = new DefaultTraceManager(new IsTracingSampler(),
new RandomUuidGenerator(), publisher);
new JdkIdGenerator(), publisher);
Trace scope = traceManager.startSpan(CREATE_SIMPLE_TRACE, new AlwaysSampler(), null);
try {

View File

@@ -6,36 +6,39 @@ import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.cloud.sleuth.Trace;
import org.springframework.cloud.sleuth.TraceManager;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.cloud.sleuth.trace.DefaultTraceManager;
import org.springframework.cloud.sleuth.trace.TraceContextHolder;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.util.JdkIdGenerator;
@RunWith(MockitoJUnitRunner.class)
public class TraceCallableTest {
ExecutorService executor = Executors.newSingleThreadExecutor();
TraceManager traceManager = Mockito.mock(TraceManager.class);
TraceManager traceManager = new DefaultTraceManager(new AlwaysSampler(),
new JdkIdGenerator(), Mockito.mock(ApplicationEventPublisher.class));
@Test
@Ignore("Will fail because trace is not removed after callable gets executed")
public void should_remove_span_from_thread_local_after_finishing_work() throws Exception {
givenCallableGetsSubmitted(thatSetsTraceInCurrentThreadLocalWithInitialTrace());
public void should_remove_span_from_thread_local_after_finishing_work()
throws Exception {
Trace firstTrace = givenCallableGetsSubmitted(thatSetsTraceInCurrentThreadLocalWithInitialTrace());
Trace secondTrace = whenCallableGetsSubmitted(thatRetrievesTraceFromThreadLocal());
then(secondTrace).isNull();
then(secondTrace.getSpan().getTraceId()).isNotEqualTo(firstTrace.getSpan().getTraceId());
}
private Callable<Trace> thatSetsTraceInCurrentThreadLocalWithInitialTrace() {
return new Callable<Trace>() {
@Override
public Trace call() throws Exception {
TraceContextHolder.setCurrentTrace(Mockito.mock(Trace.class));
return TraceContextHolder.getCurrentTrace();
}
};
@@ -50,13 +53,15 @@ public class TraceCallableTest {
};
}
private Trace givenCallableGetsSubmitted(Callable<Trace> callable) throws InterruptedException, java.util.concurrent.ExecutionException {
private Trace givenCallableGetsSubmitted(Callable<Trace> callable)
throws InterruptedException, java.util.concurrent.ExecutionException {
return whenCallableGetsSubmitted(callable);
}
private Trace whenCallableGetsSubmitted(Callable<Trace> callable) throws InterruptedException, java.util.concurrent.ExecutionException {
return executor.submit(new TraceCallable<>(traceManager, callable)).get();
private Trace whenCallableGetsSubmitted(Callable<Trace> callable)
throws InterruptedException, java.util.concurrent.ExecutionException {
return this.executor.submit(new TraceCallable<>(this.traceManager, callable))
.get();
}
}

View File

@@ -23,7 +23,6 @@ import org.mockito.Mockito;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Trace;
import org.springframework.cloud.sleuth.TraceManager;
import org.springframework.cloud.sleuth.autoconfig.RandomUuidGenerator;
import org.springframework.cloud.sleuth.event.SpanAcquiredEvent;
import org.springframework.cloud.sleuth.event.SpanReleasedEvent;
import org.springframework.cloud.sleuth.instrument.TraceRunnable;
@@ -31,6 +30,7 @@ import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.cloud.sleuth.trace.DefaultTraceManager;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.util.JdkIdGenerator;
public class TraceableExecutorServiceTests {
private ApplicationEventPublisher publisher;
@@ -44,7 +44,7 @@ public class TraceableExecutorServiceTests {
@Before
public void setUp() throws Exception {
this.publisher = Mockito.mock(ApplicationEventPublisher.class);
this.traceManager = new DefaultTraceManager(new AlwaysSampler(), new RandomUuidGenerator(), this.publisher);
this.traceManager = new DefaultTraceManager(new AlwaysSampler(), new JdkIdGenerator(), this.publisher);
ExecutorService es = Executors.newFixedThreadPool(3);
this.traceManagerableExecutorService = new TraceableExecutorService(es, this.traceManager);
this.executorService = Executors.newFixedThreadPool(3);

View File

@@ -10,7 +10,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.sleuth.IdGenerator;
import org.springframework.cloud.sleuth.MilliSpan;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.TraceManager;
@@ -22,17 +21,23 @@ import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.IdGenerator;
import com.jayway.awaitility.Awaitility;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {TraceAsyncITest.CorrelationIdAsyncSpecConfiguration.class})
@SpringApplicationConfiguration(classes = {
TraceAsyncITest.CorrelationIdAsyncSpecConfiguration.class })
public class TraceAsyncITest {
@Autowired AsyncClass asyncClass;
@Autowired AsyncDelegation asyncDelegation;
@Autowired IdGenerator idGenerator;
@Autowired TraceManager traceManager;
@Autowired
AsyncClass asyncClass;
@Autowired
AsyncDelegation asyncDelegation;
@Autowired
IdGenerator idGenerator;
@Autowired
TraceManager traceManager;
@Test
public void should_set_span_on_an_async_annotated_method() {
@@ -44,7 +49,8 @@ public class TraceAsyncITest {
}
private Span givenASpanInCurrentThread() {
Span span = MilliSpan.builder().traceId(this.idGenerator.create()).spanId(this.idGenerator.create()).build();
Span span = MilliSpan.builder().traceId(this.idGenerator.generateId().toString())
.spanId(this.idGenerator.generateId().toString()).build();
this.traceManager.continueSpan(span);
return span;
}
@@ -57,8 +63,11 @@ public class TraceAsyncITest {
Awaitility.await().until(new Runnable() {
@Override
public void run() {
then(span.getTraceId()).isNotNull().isEqualTo(asyncClass.getTraceId());
then(span.getName()).isNotEqualTo(asyncClass.getSpanName()); }
then(span.getTraceId()).isNotNull()
.isEqualTo(TraceAsyncITest.this.asyncClass.getTraceId());
then(span.getName())
.isNotEqualTo(TraceAsyncITest.this.asyncClass.getSpanName());
}
});
}
@@ -73,11 +82,13 @@ public class TraceAsyncITest {
@Configuration
public static class CorrelationIdAsyncSpecConfiguration {
@Bean AsyncClass asyncClass() {
@Bean
AsyncClass asyncClass() {
return new AsyncClass();
}
@Bean AsyncDelegation asyncDelegation() {
@Bean
AsyncDelegation asyncDelegation() {
return new AsyncDelegation(asyncClass());
}
}
@@ -105,14 +116,16 @@ public class TraceAsyncITest {
}
public String getTraceId() {
if (this.span == null || (this.span.get() != null && this.span.get().getTraceId() == null)) {
if (this.span == null || (this.span.get() != null
&& this.span.get().getTraceId() == null)) {
return null;
}
return this.span.get().getTraceId();
}
public String getSpanName() {
if (this.span == null || (this.span.get() != null && this.span.get().getName() == null)) {
if (this.span == null
|| (this.span.get() != null && this.span.get().getName() == null)) {
return null;
}
return this.span.get().getName();

View File

@@ -23,7 +23,6 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.cloud.sleuth.Trace;
import org.springframework.cloud.sleuth.TraceManager;
import org.springframework.cloud.sleuth.autoconfig.RandomUuidGenerator;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.cloud.sleuth.trace.DefaultTraceManager;
import org.springframework.cloud.sleuth.trace.TraceContextHolder;
@@ -34,6 +33,7 @@ import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.util.JdkIdGenerator;
import lombok.SneakyThrows;
@@ -46,7 +46,7 @@ public class TraceFilterIntegrationTests {
private StaticApplicationContext context = new StaticApplicationContext();
private TraceManager traceManager = new DefaultTraceManager(new AlwaysSampler(),
new RandomUuidGenerator(), this.context);
new JdkIdGenerator(), this.context);
private MockHttpServletRequest request;
private MockHttpServletResponse response;

View File

@@ -32,7 +32,6 @@ import org.mockito.Mockito;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Trace;
import org.springframework.cloud.sleuth.TraceManager;
import org.springframework.cloud.sleuth.autoconfig.RandomUuidGenerator;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.cloud.sleuth.trace.DefaultTraceManager;
import org.springframework.cloud.sleuth.trace.TraceContextHolder;
@@ -44,6 +43,7 @@ import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.util.JdkIdGenerator;
import lombok.SneakyThrows;
@@ -68,7 +68,7 @@ public class TraceFilterTests {
public void init() {
initMocks(this);
this.trace = new DefaultTraceManager(new AlwaysSampler(),
new RandomUuidGenerator(), this.publisher) {
new JdkIdGenerator(), this.publisher) {
@Override
protected Trace createTrace(Trace trace, Span span) {
TraceFilterTests.this.span= span;

View File

@@ -28,7 +28,6 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.cloud.sleuth.MilliSpan;
import org.springframework.cloud.sleuth.Trace;
import org.springframework.cloud.sleuth.autoconfig.RandomUuidGenerator;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.cloud.sleuth.trace.DefaultTraceManager;
import org.springframework.cloud.sleuth.trace.TraceContextHolder;
@@ -38,6 +37,7 @@ import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.test.web.client.MockMvcClientHttpRequestFactory;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.JdkIdGenerator;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -63,7 +63,7 @@ public class TraceRestTemplateInterceptorTests {
public void setup() {
this.publisher.refresh();
this.traces = new DefaultTraceManager(new AlwaysSampler(),
new RandomUuidGenerator(), this.publisher);
new JdkIdGenerator(), this.publisher);
this.template.setInterceptors(Arrays.<ClientHttpRequestInterceptor>asList(
new TraceRestTemplateInterceptor(this.traces)));
}