From 53feadd8420a566c7268810fdb2b27c0f44f3b4a Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Tue, 23 Jun 2015 21:28:23 -0600 Subject: [PATCH] remove s-c-s-correlation, functionality moved to s-c-s-core --- spring-cloud-sleuth-correlation/pom.xml | 132 --------------- .../sleuth/logging/CorrelationIdAspect.java | 132 --------------- .../CorrelationIdAutoConfiguration.java | 74 --------- .../sleuth/logging/CorrelationIdFilter.java | 150 ------------------ .../sleuth/logging/CorrelationIdHolder.java | 39 ----- ...ationIdSettingRestTemplateInterceptor.java | 56 ------- .../sleuth/logging/CorrelationIdUpdater.java | 118 -------------- .../cloud/sleuth/logging/UuidGenerator.java | 29 ---- .../logging/hystrix/CorrelatedCommand.java | 57 ------- .../ScheduledTaskWithCorrelationIdAspect.java | 67 -------- .../TaskSchedulingConfiguration.java | 40 ----- .../main/resources/META-INF/spring.factories | 3 - .../CorrelationIdAspectISpec.groovy | 118 -------------- .../CorrelationIdFilterISpec.groovy | 59 ------- .../CorrelationIdFilterSkipPatternSpec.groovy | 68 -------- .../CorrelationIdUpdaterSpec.groovy | 68 -------- .../correlation/base/BaseConfiguration.groovy | 17 -- .../correlation/base/HttpMockServer.groovy | 31 ---- .../base/MockServerConfiguration.groovy | 25 --- ...CorrelationIdSettingIntegrationSpec.groovy | 13 -- .../base/MvcIntegrationSpec.groovy | 45 ------ .../base/MvcWiremockIntegrationSpec.groovy | 37 ----- .../hystrix/CorrelatedCommandSpec.groovy | 41 ----- ...CorrelationIdOnScheduledMethodISpec.groovy | 25 --- .../ScheduledBeanConfiguration.groovy | 14 -- .../TestBeanWithScheduledMethod.groovy | 15 -- 26 files changed, 1473 deletions(-) delete mode 100644 spring-cloud-sleuth-correlation/pom.xml delete mode 100644 spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdAspect.java delete mode 100644 spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdAutoConfiguration.java delete mode 100644 spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdFilter.java delete mode 100644 spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdHolder.java delete mode 100644 spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdSettingRestTemplateInterceptor.java delete mode 100644 spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdUpdater.java delete mode 100644 spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/UuidGenerator.java delete mode 100644 spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/hystrix/CorrelatedCommand.java delete mode 100644 spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/scheduling/ScheduledTaskWithCorrelationIdAspect.java delete mode 100644 spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/scheduling/TaskSchedulingConfiguration.java delete mode 100644 spring-cloud-sleuth-correlation/src/main/resources/META-INF/spring.factories delete mode 100644 spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/CorrelationIdAspectISpec.groovy delete mode 100644 spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/CorrelationIdFilterISpec.groovy delete mode 100644 spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/CorrelationIdFilterSkipPatternSpec.groovy delete mode 100644 spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/CorrelationIdUpdaterSpec.groovy delete mode 100644 spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/BaseConfiguration.groovy delete mode 100755 spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/HttpMockServer.groovy delete mode 100755 spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/MockServerConfiguration.groovy delete mode 100644 spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/MvcCorrelationIdSettingIntegrationSpec.groovy delete mode 100755 spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/MvcIntegrationSpec.groovy delete mode 100755 spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/MvcWiremockIntegrationSpec.groovy delete mode 100644 spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/hystrix/CorrelatedCommandSpec.groovy delete mode 100644 spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/scheduling/CorrelationIdOnScheduledMethodISpec.groovy delete mode 100644 spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/scheduling/ScheduledBeanConfiguration.groovy delete mode 100644 spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/scheduling/TestBeanWithScheduledMethod.groovy diff --git a/spring-cloud-sleuth-correlation/pom.xml b/spring-cloud-sleuth-correlation/pom.xml deleted file mode 100644 index 697ebf14a..000000000 --- a/spring-cloud-sleuth-correlation/pom.xml +++ /dev/null @@ -1,132 +0,0 @@ - - - 4.0.0 - - spring-cloud-sleuth-correlation - jar - Spring Cloud Sleuth Correlation - Spring Cloud Sleuth Correlation - - - org.springframework.cloud - spring-cloud-sleuth - 1.0.0.BUILD-SNAPSHOT - .. - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - - org.codehaus.gmavenplus - gmavenplus-plugin - - - maven-surefire-plugin - - - - - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.cloud - spring-cloud-sleuth-core - - - org.springframework.boot - spring-boot-starter-actuator - true - - - commons-lang - commons-lang - - - org.projectlombok - lombok - - provided - - - org.springframework.boot - spring-boot-starter-test - test - - - com.netflix.hystrix - hystrix-core - - - org.aspectj - aspectjrt - - - org.aspectj - aspectjweaver - runtime - - - io.reactivex - rxjava - - - - org.spockframework - spock-core - test - - - org.spockframework - spock-spring - test - - - cglib - cglib-nodep - test - - - org.objenesis - objenesis - test - - - org.hamcrest - hamcrest-core - test - - - org.codehaus.groovy - groovy-all - test - - - org.codehaus.gpars - gpars - 1.2.1 - test - - - com.github.tomakehurst - wiremock - 1.53 - test - - - com.github.stefanbirkner - system-rules - 1.9.0 - test - - - - diff --git a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdAspect.java b/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdAspect.java deleted file mode 100644 index 23b995ded..000000000 --- a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdAspect.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright 2012-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.logging; - -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Pointcut; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.client.RestOperations; - -import java.lang.invoke.MethodHandles; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.Callable; - -/** - * Aspect that adds correlation id to - *

- *

- *

- * For controllers an around aspect is created that wraps the {@link Callable#call()} method execution - * in {@link CorrelationIdUpdater#wrapCallableWithId(Callable)} - *

- * For {@link RestOperations} we are wrapping all executions of the - * exchange methods and we are extracting {@link HttpHeaders} from the passed {@link HttpEntity}. - * Next we are adding correlation id header {@link CorrelationIdHolder#CORRELATION_ID_HEADER} with - * the value taken from {@link CorrelationIdHolder}. Finally the method execution proceeds. - * - * @see RestController - * @see Controller - * @see RestOperations - * @see CorrelationIdHolder - * @see CorrelationIdFilter - * - * @author Tomasz Nurkewicz, 4financeIT - * @author Marcin Grzejszczak, 4financeIT - * @author Michal Chmielarz, 4financeIT - */ -@Aspect -public class CorrelationIdAspect { - private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - - private static final int HTTP_ENTITY_PARAM_INDEX = 2; - - @Pointcut("@target(org.springframework.web.bind.annotation.RestController)") - private void anyRestControllerAnnotated() { - } - - @Pointcut("@target(org.springframework.stereotype.Controller)") - private void anyControllerAnnotated() { - } - - @Pointcut("execution(public java.util.concurrent.Callable *(..))") - private void anyPublicMethodReturningCallable() { - } - - @Pointcut("(anyRestControllerAnnotated() || anyControllerAnnotated()) && anyPublicMethodReturningCallable()") - private void anyControllerOrRestControllerWithPublicAsyncMethod() { - } - - @Around("anyControllerOrRestControllerWithPublicAsyncMethod()") - public Object wrapWithCorrelationId(ProceedingJoinPoint pjp) throws Throwable { - final Callable callable = (Callable) pjp.proceed(); - log.debug("Wrapping callable with correlation id [" + CorrelationIdHolder.get() + "]"); - return CorrelationIdUpdater.wrapCallableWithId(new Callable() { - @Override - public Object call() throws Exception { - return callable.call(); - } - }); - } - - @Pointcut("execution(public * org.springframework.web.client.RestOperations.exchange(..))") - private void anyExchangeRestOperationsMethod() { - } - - @Around("anyExchangeRestOperationsMethod()") - public Object wrapWithCorrelationIdForRestOperations(ProceedingJoinPoint pjp) throws Throwable { - String correlationId = CorrelationIdHolder.get(); - log.debug("Wrapping RestTemplate call with correlation id [" + correlationId + "]"); - HttpEntity httpEntity = (HttpEntity) pjp.getArgs()[HTTP_ENTITY_PARAM_INDEX]; - HttpEntity newHttpEntity = createNewHttpEntity(httpEntity, correlationId); - List newArgs = modifyHttpEntityInMethodArguments(pjp, newHttpEntity); - return pjp.proceed(newArgs.toArray()); - } - - @SuppressWarnings("unchecked") - private HttpEntity createNewHttpEntity(HttpEntity httpEntity, String correlationId) { - HttpHeaders newHttpHeaders = new HttpHeaders(); - newHttpHeaders.putAll(httpEntity.getHeaders()); - newHttpHeaders.add(CorrelationIdHolder.CORRELATION_ID_HEADER, correlationId); - return new HttpEntity(httpEntity.getBody(), newHttpHeaders); - } - - private List modifyHttpEntityInMethodArguments(ProceedingJoinPoint pjp, HttpEntity newHttpEntity) { - List newArgs = new ArrayList<>(); - for (int i = 0; i < pjp.getArgs().length; i++) { - Object arg = pjp.getArgs()[i]; - if (i != HTTP_ENTITY_PARAM_INDEX) { - newArgs.add(i, arg); - } else { - newArgs.add(i, newHttpEntity); - } - } - return newArgs; - } -} diff --git a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdAutoConfiguration.java b/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdAutoConfiguration.java deleted file mode 100644 index 4d5f4aac2..000000000 --- a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdAutoConfiguration.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2012-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.logging; - -import org.apache.commons.lang.StringUtils; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.context.embedded.FilterRegistrationBean; -import org.springframework.cloud.sleuth.resttemplate.SleuthRestTemplateAutoConfiguration; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import java.util.regex.Pattern; - -/** - * Registers beans that add correlation id to requests - * - * @see CorrelationIdAspect - * @see CorrelationIdFilter - * - * @author Tomasz Nurkewicz, 4financeIT - * @author Marcin Grzejszczak, 4financeIT - * @author Michal Chmielarz, 4financeIT - */ -@Configuration -@ConditionalOnProperty(value = "spring.cloud.sleuth.correlation.enabled", matchIfMissing = true) -@AutoConfigureAfter(SleuthRestTemplateAutoConfiguration.class) -public class CorrelationIdAutoConfiguration { - - /** - * Pattern for URLs that should be skipped in correlationID setting - */ - @Value("${spring.cloud.sleuth.correlation.skipPattern:}") - private String skipPattern; - - @Bean - @ConditionalOnMissingBean - public CorrelationIdAspect correlationIdAspect() { - return new CorrelationIdAspect(); - } - - @Bean - @ConditionalOnMissingBean - public FilterRegistrationBean correlationHeaderFilter(UuidGenerator uuidGenerator) { - Pattern pattern = StringUtils.isBlank(skipPattern) ? Pattern.compile(skipPattern) : CorrelationIdFilter.DEFAULT_SKIP_PATTERN; - return new FilterRegistrationBean(new CorrelationIdFilter(uuidGenerator, pattern)); - } - - @Bean - @ConditionalOnMissingBean - public UuidGenerator uuidGenerator() { - return new UuidGenerator(); - } - - @Bean - public CorrelationIdSettingRestTemplateInterceptor correlationIdSettingRestTemplateInterceptor() { - return new CorrelationIdSettingRestTemplateInterceptor(); - } -} diff --git a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdFilter.java b/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdFilter.java deleted file mode 100644 index 78aabfd99..000000000 --- a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdFilter.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright 2012-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.logging; - -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; -import org.springframework.web.filter.OncePerRequestFilter; - -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.lang.invoke.MethodHandles; -import java.util.concurrent.Callable; -import java.util.regex.Pattern; - -import static org.springframework.util.StringUtils.hasText; - -/** - * Filter that takes the value of the {@link CorrelationIdHolder#CORRELATION_ID_HEADER} header - * from either request or response and sets it in the {@link CorrelationIdHolder}. It also provides - * that value in {@link MDC} logging related class so that logger prints the value of - * correlation id at each log. - * - * @see CorrelationIdHolder - * @see MDC - * - * @author Jakub Nabrdalik, 4financeIT - * @author Tomasz Nurkiewicz, 4financeIT - * @author Marcin Grzejszczak, 4financeIT - */ -public class CorrelationIdFilter extends OncePerRequestFilter { - private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - public static final Pattern DEFAULT_SKIP_PATTERN = Pattern.compile("/api-docs.*|/autoconfig|/configprops|/dump|/info|/metrics.*|/mappings|/trace|/swagger.*|.*\\.png|.*\\.css|.*\\.js|.*\\.html"); - - private final Pattern skipCorrId; - private final UuidGenerator uuidGenerator; - - public CorrelationIdFilter() { - this.uuidGenerator = new UuidGenerator(); - this.skipCorrId = null; - } - - public CorrelationIdFilter(UuidGenerator uuidGenerator, Pattern skipCorrId) { - this.uuidGenerator = uuidGenerator; - this.skipCorrId = skipCorrId; - } - - @Override - protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { - setupCorrelationId(request, response); - try { - filterChain.doFilter(request, response); - } finally { - cleanupCorrelationId(); - } - } - - private void setupCorrelationId(HttpServletRequest request, HttpServletResponse response) { - String correlationIdFromRequest = getCorrelationIdFrom(request); - String correlationId = (hasText(correlationIdFromRequest)) ? correlationIdFromRequest : getCorrelationIdFrom(response); - if (!hasText(correlationId) && shouldGenerateCorrId(request)) { - correlationId = createNewCorrIdIfEmpty(); - } - CorrelationIdHolder.set(correlationId); - addCorrelationIdToResponseIfNotPresent(response, correlationId); - } - - private String getCorrelationIdFrom(final HttpServletResponse response) { - return withLoggingAs("response", new Callable() { - @Override - public String call() throws Exception { - return response.getHeader(CorrelationIdHolder.CORRELATION_ID_HEADER); - } - }); - } - - private String getCorrelationIdFrom(final HttpServletRequest request) { - return withLoggingAs("request", new Callable() { - @Override - public String call() throws Exception { - return request.getHeader(CorrelationIdHolder.CORRELATION_ID_HEADER); - } - }); - } - - private String withLoggingAs(String whereWasFound, Callable correlationIdGetter) { - String correlationId = tryToGetCorrelationId(correlationIdGetter); - if (hasText(correlationId)) { - MDC.put(CorrelationIdHolder.CORRELATION_ID_HEADER, correlationId); - log.debug("Found correlationId in " + whereWasFound + ": " + correlationId); - } - return correlationId; - } - - private String tryToGetCorrelationId(Callable correlationIdGetter) { - try { - return correlationIdGetter.call(); - } catch (Exception e) { - log.error("Exception occurred while trying to retrieve request header", e); - return StringUtils.EMPTY; - } - } - - private String createNewCorrIdIfEmpty() { - String currentCorrId = uuidGenerator.create(); - MDC.put(CorrelationIdHolder.CORRELATION_ID_HEADER, currentCorrId); - log.debug("Generating new correlationId: " + currentCorrId); - return currentCorrId; - } - - protected boolean shouldGenerateCorrId(HttpServletRequest request) { - final String i = request.getRequestURI(); - final String uri = StringUtils.defaultIfEmpty(i, StringUtils.EMPTY); - boolean skip = skipCorrId != null && skipCorrId.matcher(uri).matches(); - return !skip; - } - - private void addCorrelationIdToResponseIfNotPresent(HttpServletResponse response, String correlationId) { - if (!hasText(response.getHeader(CorrelationIdHolder.CORRELATION_ID_HEADER))) { - response.addHeader(CorrelationIdHolder.CORRELATION_ID_HEADER, correlationId); - } - } - - private void cleanupCorrelationId() { - MDC.remove(CorrelationIdHolder.CORRELATION_ID_HEADER); - CorrelationIdHolder.remove(); - } - - @Override - protected boolean shouldNotFilterAsyncDispatch() { - return false; - } -} diff --git a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdHolder.java b/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdHolder.java deleted file mode 100644 index 291bc747a..000000000 --- a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdHolder.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2012-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.logging; - -/** - * Component that stores correlation id using {@link ThreadLocal} - * - * @author Jakub Nabrdalik, 4financeIT - * @author Marcin Zajaczkowski, 4financeIT - */ -public class CorrelationIdHolder { - public static final String CORRELATION_ID_HEADER = "Correlation-Id"; - private static final ThreadLocal id = new ThreadLocal(); - - public static void set(String correlationId) { - id.set(correlationId); - } - - public static String get() { - return id.get(); - } - - public static void remove() { - id.remove(); - } -} diff --git a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdSettingRestTemplateInterceptor.java b/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdSettingRestTemplateInterceptor.java deleted file mode 100644 index c17a6fc57..000000000 --- a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdSettingRestTemplateInterceptor.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2012-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.logging; - -import lombok.SneakyThrows; -import org.springframework.http.HttpRequest; -import org.springframework.http.client.ClientHttpRequestExecution; -import org.springframework.http.client.ClientHttpRequestInterceptor; -import org.springframework.http.client.ClientHttpResponse; - -import java.io.IOException; - -/** - * Interceptor that verifies whether the correlation id has been - * set on the request and sets it if it's missing. - * - * @see org.springframework.web.client.RestTemplate - * @see CorrelationIdHolder - * - * @author Marcin Grzejszczak, 4financeIT - */ -public class CorrelationIdSettingRestTemplateInterceptor implements ClientHttpRequestInterceptor { - - @SneakyThrows - @Override - public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { - appendCorrelationIdToRequestIfMissing(request); - ClientHttpResponse response = null; - try { - response = execution.execute(request, body); - } catch (final Exception e) { - throw new RuntimeException(e); - } - return response; - } - - private void appendCorrelationIdToRequestIfMissing(HttpRequest request) { - if (!request.getHeaders().containsKey(CorrelationIdHolder.CORRELATION_ID_HEADER)) { - request.getHeaders().add(CorrelationIdHolder.CORRELATION_ID_HEADER, CorrelationIdHolder.get()); - } - } - -} diff --git a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdUpdater.java b/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdUpdater.java deleted file mode 100644 index 29f2afbcc..000000000 --- a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/CorrelationIdUpdater.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright 2012-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.logging; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; -import org.springframework.util.StringUtils; - -import java.lang.invoke.MethodHandles; -import java.util.concurrent.Callable; - -/** - * Class that takes care of updating all necessary components with new value - * of correlation id. - * It sets correlationId on {@link ThreadLocal} in {@link CorrelationIdHolder} - * and in {@link MDC}. - * - * @see CorrelationIdHolder - * @see MDC - * - * @author Jakub Nabrdalik, 4financeIT - * @author Michal Chmielarz, 4financeIT - */ -public class CorrelationIdUpdater { - - private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - - public static void updateCorrelationId(String correlationId) { - if (StringUtils.hasText(correlationId)) { - log.debug("Updating correlationId with value: [" + correlationId + "]"); - CorrelationIdHolder.set(correlationId); - MDC.put(CorrelationIdHolder.CORRELATION_ID_HEADER, correlationId); - } - } - - /** - * Temporarily updates correlation ID inside block of code. - * Makes sure previous ID is restored after block's execution - * - * @param temporaryCorrelationId - correlationID to passed to the executed block of code - * @param block Callable to be executed with new ID - * @return the result of Callable block execution - */ - public static T withId(String temporaryCorrelationId, Callable block) { - final String oldCorrelationId = CorrelationIdHolder.get(); - try { - updateCorrelationId(temporaryCorrelationId); - return block.call(); - } catch (RuntimeException e) { - logException(e); - throw e; - } catch (Exception e) { - logException(e); - throw new RuntimeException(e); - } finally { - updateCorrelationId(oldCorrelationId); - } - - } - - private static void logException(Throwable e) { - log.error("Exception occurred while trying to execute the function", e); - } - - /** - * Wraps given {@link Callable} with another {@link Callable Callable} propagating correlation ID inside nested - * Callable/Closure. - *

- *

- * Useful in a situation when a Callable should be executed in a separate thread, for example in aspects. - *

- *


-	 * @Around('...')
-	 * Object wrapWithCorrelationId(ProceedingJoinPoint pjp) throws Throwable {
-	 *     Callable callable = pjp.proceed() as Callable
-	 *     return CorrelationIdUpdater.wrapCallableWithId {
-	 *         callable.call()
-	 *     }
-	 * }
-	 * 
- *

- * Note: Passing only one input parameter currently is supported. - * - * @param block code block to execute in a thread with a correlation ID taken from the original thread - * @return wrapping block as Callable - */ - @SuppressWarnings("unchecked") - public static Callable wrapCallableWithId(final Callable block) { - final String temporaryCorrelationId = CorrelationIdHolder.get(); - // unchecked assignment due to groovyc issues with - return new Callable() { - @Override - public Object call() throws Exception { - final String oldCorrelationId = CorrelationIdHolder.get(); - try { - updateCorrelationId(temporaryCorrelationId); - return block.call(); - } finally { - updateCorrelationId(oldCorrelationId); - } - } - }; - } -} diff --git a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/UuidGenerator.java b/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/UuidGenerator.java deleted file mode 100644 index a289a0a0a..000000000 --- a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/UuidGenerator.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2012-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.logging; - -import java.util.UUID; - -/** - * Default Uuid generator - * - * @author extends HystrixCommand { - private final String clientCorrelationId = CorrelationIdHolder.get(); - - protected CorrelatedCommand(HystrixCommandGroupKey group) { - super(group); - } - - protected CorrelatedCommand(Setter setter) { - super(setter); - } - - @Override - protected final R run() throws Exception { - return CorrelationIdUpdater.withId(clientCorrelationId, new Callable() { - @Override - public R call() throws Exception { - return doRun(); - } - - }); - } - - public abstract R doRun() throws Exception; -} diff --git a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/scheduling/ScheduledTaskWithCorrelationIdAspect.java b/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/scheduling/ScheduledTaskWithCorrelationIdAspect.java deleted file mode 100644 index 63fed0ecc..000000000 --- a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/scheduling/ScheduledTaskWithCorrelationIdAspect.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2012-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.logging.scheduling; - -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.cloud.sleuth.logging.CorrelationIdUpdater; -import org.springframework.cloud.sleuth.logging.UuidGenerator; -import org.springframework.scheduling.annotation.Scheduled; - -import java.lang.invoke.MethodHandles; -import java.util.concurrent.Callable; - -/** - * Aspect that sets correlationId for running threads executing methods annotated with {@link Scheduled} annotation. - * For every execution of scheduled method a new, i.e. unique one, value of correlationId will be set. - * - * @author Tomasz Nurkewicz, 4financeIT - * @author Michal Chmielarz, 4financeIT - * @author Marcin Grzejszczak, 4financeIT - * - * @see UuidGenerator - * @see CorrelationIdUpdater - */ -@Aspect -public class ScheduledTaskWithCorrelationIdAspect { - - private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - - private final UuidGenerator uuidGenerator; - - public ScheduledTaskWithCorrelationIdAspect(UuidGenerator uuidGenerator) { - this.uuidGenerator = uuidGenerator; - } - - @Around("execution (@org.springframework.scheduling.annotation.Scheduled * *.*(..))") - public Object setNewCorrelationIdOnThread(final ProceedingJoinPoint pjp) throws Throwable { - String correlationId = uuidGenerator.create(); - return CorrelationIdUpdater.withId(correlationId, new Callable() { - @Override - public Object call() throws Exception { - try { - return pjp.proceed(); - } catch (Throwable throwable) { - log.error("Didn't manage to proceed with the pointcut", throwable); - throw new RuntimeException(throwable); - } - } - }); - } -} diff --git a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/scheduling/TaskSchedulingConfiguration.java b/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/scheduling/TaskSchedulingConfiguration.java deleted file mode 100644 index 3e825f7ef..000000000 --- a/spring-cloud-sleuth-correlation/src/main/java/org/springframework/cloud/sleuth/logging/scheduling/TaskSchedulingConfiguration.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2012-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.logging.scheduling; - -import org.springframework.cloud.sleuth.logging.UuidGenerator; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.EnableAspectJAutoProxy; -import org.springframework.scheduling.annotation.EnableScheduling; - -/** - * Registers beans related to task scheduling. - * - * @see ScheduledTaskWithCorrelationIdAspect - * - * @author Michal Chmielarz, 4financeIT - */ -@Configuration -@EnableScheduling -@EnableAspectJAutoProxy -public class TaskSchedulingConfiguration { - @Bean - public ScheduledTaskWithCorrelationIdAspect scheduledTaskPointcut(UuidGenerator uuidGenerator) { - return new ScheduledTaskWithCorrelationIdAspect(uuidGenerator); - } - -} diff --git a/spring-cloud-sleuth-correlation/src/main/resources/META-INF/spring.factories b/spring-cloud-sleuth-correlation/src/main/resources/META-INF/spring.factories deleted file mode 100644 index ec398fdbf..000000000 --- a/spring-cloud-sleuth-correlation/src/main/resources/META-INF/spring.factories +++ /dev/null @@ -1,3 +0,0 @@ -# Auto Configuration -org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -org.springframework.cloud.sleuth.correlation.logging.CorrelationIdAutoConfiguration diff --git a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/CorrelationIdAspectISpec.groovy b/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/CorrelationIdAspectISpec.groovy deleted file mode 100644 index e47e47720..000000000 --- a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/CorrelationIdAspectISpec.groovy +++ /dev/null @@ -1,118 +0,0 @@ -package org.springframework.cloud.sleuth.correlation -import groovy.transform.CompileStatic -import groovy.transform.PackageScope -import groovy.transform.TypeChecked -import org.hamcrest.Description -import org.hamcrest.TypeSafeMatcher -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.autoconfigure.EnableAutoConfiguration -import org.springframework.boot.test.SpringApplicationContextLoader -import org.springframework.cloud.sleuth.correlation.base.HttpMockServer -import org.springframework.cloud.sleuth.correlation.base.MvcCorrelationIdSettingIntegrationSpec -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration -import org.springframework.context.annotation.EnableAspectJAutoProxy -import org.springframework.http.MediaType -import org.springframework.scheduling.annotation.EnableAsync -import org.springframework.test.context.ContextConfiguration -import org.springframework.test.web.servlet.MvcResult -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders -import org.springframework.test.web.servlet.result.MockMvcResultHandlers -import org.springframework.test.web.servlet.result.MockMvcResultMatchers -import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.RequestMethod -import org.springframework.web.bind.annotation.RestController -import org.springframework.web.client.RestTemplate - -import java.util.concurrent.Callable -import java.util.concurrent.TimeUnit - -import static com.github.tomakehurst.wiremock.client.WireMock.* -import static org.springframework.cloud.sleuth.logging.CorrelationIdHolder.CORRELATION_ID_HEADER - -@ContextConfiguration(classes = [CorrelationIdAspectSpecConfiguration], loader = SpringApplicationContextLoader) -class CorrelationIdAspectISpec extends MvcCorrelationIdSettingIntegrationSpec { - - public static final String CORRELATION_ID_PATTERN = /^(?!\s*$).+/ - public static final TypeSafeMatcher hasCorrelationIdSet = new TypeSafeMatcher() { - @Override - protected boolean matchesSafely(String item) { - return item.matches(CORRELATION_ID_PATTERN) - } - - @Override - void describeTo(Description description) { - - } - } - - def "should set correlationId on header via aspect in synchronous call"() { - given: - stubInteraction(get(urlMatching('.*')), aResponse().withStatus(200)) - when: - mockMvc.perform(MockMvcRequestBuilders.get('/syncPing').accept(MediaType.TEXT_PLAIN)) - .andExpect(MockMvcResultMatchers.header().string(CORRELATION_ID_HEADER, hasCorrelationIdSet)) - then: - wireMock.verifyThat(getRequestedFor(urlMatching('.*')).withHeader(CORRELATION_ID_HEADER, matching(CORRELATION_ID_PATTERN))) - - } - - def "should set correlationId on header via aspect in asynchronous call"() { - given: - stubInteraction(get(urlMatching('.*')), aResponse().withStatus(200)) - when: - MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get('/asyncPing').accept(MediaType.TEXT_PLAIN)) - .andExpect(MockMvcResultMatchers.request().asyncStarted()) - .andReturn() - and: - mvcResult.getAsyncResult(TimeUnit.SECONDS.toMillis(2)) - then: - mockMvc.perform(MockMvcRequestBuilders.asyncDispatch(mvcResult)). - andDo(MockMvcResultHandlers.print()). - andExpect(MockMvcResultMatchers.status().isOk()). - andExpect(MockMvcResultMatchers.header().string(CORRELATION_ID_HEADER, hasCorrelationIdSet)) - and: - wireMock.verifyThat(getRequestedFor(urlMatching('.*')).withHeader(CORRELATION_ID_HEADER, matching(CORRELATION_ID_PATTERN))) - } - - @CompileStatic - @Configuration - @EnableAsync - @EnableAutoConfiguration - @EnableAspectJAutoProxy(proxyTargetClass = true) - static class CorrelationIdAspectSpecConfiguration { - @Bean - AspectTestingController aspectTestingController() { - return new AspectTestingController() - } - } - - @RestController - @TypeChecked - @PackageScope - static class AspectTestingController { - - @Autowired - private HttpMockServer httpMockServer - @Autowired - private RestTemplate restTemplate - - @RequestMapping(value = "/syncPing", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE) - String syncPing() { - callWiremockAndReturnOk() - } - - @RequestMapping(value = "/asyncPing", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE) - Callable asyncPing() { - return { - callWiremockAndReturnOk() - } - } - - private String callWiremockAndReturnOk() { - restTemplate.getForObject("http://localhost:${httpMockServer.port()}", String) - return "OK" - } - } - -} diff --git a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/CorrelationIdFilterISpec.groovy b/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/CorrelationIdFilterISpec.groovy deleted file mode 100644 index 282191849..000000000 --- a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/CorrelationIdFilterISpec.groovy +++ /dev/null @@ -1,59 +0,0 @@ -package org.springframework.cloud.sleuth.correlation - -import org.slf4j.MDC -import org.springframework.boot.test.SpringApplicationContextLoader -import org.springframework.cloud.sleuth.correlation.base.BaseConfiguration -import org.springframework.cloud.sleuth.correlation.base.MvcCorrelationIdSettingIntegrationSpec -import org.springframework.cloud.sleuth.logging.CorrelationIdAutoConfiguration -import org.springframework.cloud.sleuth.logging.CorrelationIdHolder -import org.springframework.http.MediaType -import org.springframework.test.context.ContextConfiguration -import org.springframework.test.web.servlet.MvcResult -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders - -@ContextConfiguration(classes = [BaseConfiguration, CorrelationIdAutoConfiguration], loader = SpringApplicationContextLoader) -class CorrelationIdFilterISpec extends MvcCorrelationIdSettingIntegrationSpec { - - def "should create and return correlationId in HTTP header"() { - when: - MvcResult mvcResult = sendPingWithoutCorrelationId() - - then: - getCorrelationIdFromResponseHeader(mvcResult) != null - } - - def "when correlationId is sent, should not create a new one, but return the existing one instead"() { - given: - String passedCorrelationId = "passedCorId" - - when: - MvcResult mvcResult = sendPingWithCorrelationId(passedCorrelationId) - - then: - getCorrelationIdFromResponseHeader(mvcResult) == passedCorrelationId - } - - def "should clean up MDC after the call"() { - given: - String passedCorrelationId = "passedCorId" - - when: - sendPingWithCorrelationId(passedCorrelationId) - - then: - MDC.get(CorrelationIdHolder.CORRELATION_ID_HEADER) == null - } - - private MvcResult sendPingWithCorrelationId(String passedCorrelationId) { - mockMvc.perform(MockMvcRequestBuilders.get('/ping').accept(MediaType.TEXT_PLAIN) - .header(CorrelationIdHolder.CORRELATION_ID_HEADER, passedCorrelationId)).andReturn() - } - - private MvcResult sendPingWithoutCorrelationId() { - mockMvc.perform(MockMvcRequestBuilders.get('/ping').accept(MediaType.TEXT_PLAIN)).andReturn() - } - - private String getCorrelationIdFromResponseHeader(MvcResult mvcResult) { - mvcResult.response.getHeader(CorrelationIdHolder.CORRELATION_ID_HEADER) - } -} diff --git a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/CorrelationIdFilterSkipPatternSpec.groovy b/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/CorrelationIdFilterSkipPatternSpec.groovy deleted file mode 100644 index 55dc7f241..000000000 --- a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/CorrelationIdFilterSkipPatternSpec.groovy +++ /dev/null @@ -1,68 +0,0 @@ -package org.springframework.cloud.sleuth.correlation - -import org.springframework.cloud.sleuth.logging.CorrelationIdFilter -import org.springframework.cloud.sleuth.logging.CorrelationIdHolder -import org.springframework.cloud.sleuth.logging.UuidGenerator -import spock.lang.Specification -import spock.lang.Unroll - -import javax.servlet.FilterChain -import javax.servlet.http.HttpServletRequest -import javax.servlet.http.HttpServletResponse - -@Unroll -class CorrelationIdFilterSkipPatternSpec extends Specification { - - CorrelationIdFilter filter = new CorrelationIdFilter(Stub(UuidGenerator), CorrelationIdFilter.DEFAULT_SKIP_PATTERN) - - def 'should skip meaningless URIs like #uri'() { - given: - HttpServletResponse responseMock = Mock(HttpServletResponse) - HttpServletRequest requestMock = Mock(HttpServletRequest) - and: - requestMock.getRequestURI() >> uri - when: - filter.doFilter(requestMock, responseMock, Stub(FilterChain)) - then: - 0 * responseMock.addHeader(CorrelationIdHolder.CORRELATION_ID_HEADER, _ as String) - where: - uri | _ - '/api-docs' | _ - '/api-docs/default' | _ - '/swagger' | _ - '/trace' | _ - '/metrics' | _ - '/metrics/foo' | _ - '/mappings' | _ - '/autoconfig' | _ - '/configprops' | _ - '/info' | _ - '/dump' | _ - '/swagger/foo' | _ - '/foo.js' | _ - '/foo/bar.png' | _ - '/foo/bar.html' | _ - '/foo/bar.css' | _ - '/foo/bar.js' | _ - '/foo/bar.js' | _ - } - - def 'should not skip #uri'() { - given: - HttpServletResponse responseMock = Mock(HttpServletResponse) - HttpServletRequest requestMock = Mock(HttpServletRequest) - and: - requestMock.getRequestURI() >> uri - when: - filter.doFilter(requestMock, responseMock, Stub(FilterChain)) - then: - 1 * responseMock.addHeader(CorrelationIdHolder.CORRELATION_ID_HEADER, _ as String) - where: - uri | _ - '/business/api-docs' | _ - '/business/swagger/foo' | _ - '/foo.js/service' | _ - } - - -} diff --git a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/CorrelationIdUpdaterSpec.groovy b/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/CorrelationIdUpdaterSpec.groovy deleted file mode 100644 index 1d68af8c1..000000000 --- a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/CorrelationIdUpdaterSpec.groovy +++ /dev/null @@ -1,68 +0,0 @@ -package org.springframework.cloud.sleuth.correlation - -import groovyx.gpars.GParsPool -import org.springframework.cloud.sleuth.logging.CorrelationIdHolder -import org.springframework.cloud.sleuth.logging.CorrelationIdUpdater -import spock.lang.Specification - -import java.util.concurrent.Callable -import java.util.concurrent.ExecutorService -import java.util.concurrent.Executors -import java.util.concurrent.TimeUnit - -class CorrelationIdUpdaterSpec extends Specification { - - def cleanup() { - CorrelationIdHolder.remove() - } - - def "correlation ID should not be propagated to other thread by default"() { - given: - CorrelationIdUpdater.updateCorrelationId('A') - expect: - GParsPool.withPool(1) { - ["1"].eachParallel { - assert CorrelationIdHolder.get() == null - } - } - } - - def "should propagate correlation ID into nested Callable"() { - given: - ExecutorService threadPool = Executors.newFixedThreadPool(1) - CorrelationIdUpdater.updateCorrelationId('A') - Callable callable = new CorrelationIdTestCallable() - when: - Callable wrappedCallable = CorrelationIdUpdater.wrapCallableWithId(callable) - String nestedCorrelationId = threadPool.submit(wrappedCallable).get(1, TimeUnit.SECONDS) - then: - nestedCorrelationId == 'A' - cleanup: - threadPool.shutdown() - } - - def "should restore previous correlation ID after Callable execution in other thread"() { - given: - ExecutorService threadPool = Executors.newFixedThreadPool(1) - CorrelationIdUpdater.updateCorrelationId('A') - Callable callable = new CorrelationIdTestCallable() - and: - threadPool.submit({ CorrelationIdHolder.set('B') }).get(1, TimeUnit.SECONDS) - when: - threadPool.submit(CorrelationIdUpdater.wrapCallableWithId(callable)).get(1, TimeUnit.SECONDS) - then: - def restoredCorrelationId = threadPool.submit({ - CorrelationIdHolder.get() - } as Callable).get(1, TimeUnit.SECONDS) - restoredCorrelationId == 'B' - cleanup: - threadPool.shutdown() - } - - private static class CorrelationIdTestCallable implements Callable { - @Override - String call() throws Exception { - CorrelationIdHolder.get() - } - } -} diff --git a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/BaseConfiguration.groovy b/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/BaseConfiguration.groovy deleted file mode 100644 index d3fc8744f..000000000 --- a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/BaseConfiguration.groovy +++ /dev/null @@ -1,17 +0,0 @@ -package org.springframework.cloud.sleuth.correlation.base - -import groovy.transform.CompileStatic -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration -import org.springframework.context.support.PropertySourcesPlaceholderConfigurer - -@CompileStatic -@Configuration -class BaseConfiguration { - - @Bean - static PropertySourcesPlaceholderConfigurer placeholderConfigurer() { - return new PropertySourcesPlaceholderConfigurer() - } - -} diff --git a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/HttpMockServer.groovy b/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/HttpMockServer.groovy deleted file mode 100755 index 629190b34..000000000 --- a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/HttpMockServer.groovy +++ /dev/null @@ -1,31 +0,0 @@ -package org.springframework.cloud.sleuth.correlation.base - -import com.github.tomakehurst.wiremock.WireMockServer -import groovy.transform.CompileStatic - -/** - * Custom implementation of {@link WireMockServer} that by default registers itself at port - * {@link HttpMockServer#DEFAULT_PORT}. - * - * @see WireMockServer - */ -@CompileStatic -class HttpMockServer extends WireMockServer { - - public static final int DEFAULT_PORT = 8030 - - HttpMockServer(int port) { - super(port) - } - - HttpMockServer() { - super(DEFAULT_PORT) - } - - void shutdownServer() { - if (isRunning()) { - stop() - } - shutdown() - } -} diff --git a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/MockServerConfiguration.groovy b/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/MockServerConfiguration.groovy deleted file mode 100755 index 7e9a97c61..000000000 --- a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/MockServerConfiguration.groovy +++ /dev/null @@ -1,25 +0,0 @@ -package org.springframework.cloud.sleuth.correlation.base - -import groovy.transform.CompileStatic -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration -import org.springframework.util.SocketUtils - -/** - * Configuration that registers {@link HttpMockServer} as a Spring bean. Takes care - * of graceful shutdown process. - * - * @see HttpMockServer - */ -@CompileStatic -@Configuration -class MockServerConfiguration { - - @Bean(destroyMethod = 'shutdownServer') - HttpMockServer httpMockServer() { - HttpMockServer httpMockServer = new HttpMockServer(SocketUtils.findAvailableTcpPort()) - httpMockServer.start() - return httpMockServer - } - -} diff --git a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/MvcCorrelationIdSettingIntegrationSpec.groovy b/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/MvcCorrelationIdSettingIntegrationSpec.groovy deleted file mode 100644 index 2361197b9..000000000 --- a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/MvcCorrelationIdSettingIntegrationSpec.groovy +++ /dev/null @@ -1,13 +0,0 @@ -package org.springframework.cloud.sleuth.correlation.base - -import org.springframework.cloud.sleuth.logging.CorrelationIdFilter -import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder - -class MvcCorrelationIdSettingIntegrationSpec extends org.springframework.cloud.sleuth.correlation.base.MvcWiremockIntegrationSpec { - - @Override - protected void configureMockMvcBuilder(ConfigurableMockMvcBuilder mockMvcBuilder) { - super.configureMockMvcBuilder(mockMvcBuilder) - mockMvcBuilder.addFilter(new CorrelationIdFilter()) - } -} diff --git a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/MvcIntegrationSpec.groovy b/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/MvcIntegrationSpec.groovy deleted file mode 100755 index 4cd63fd9e..000000000 --- a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/MvcIntegrationSpec.groovy +++ /dev/null @@ -1,45 +0,0 @@ -package org.springframework.cloud.sleuth.correlation.base - -import groovy.transform.CompileStatic -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.context.ApplicationContext -import org.springframework.test.context.web.WebAppConfiguration -import org.springframework.test.web.servlet.MockMvc -import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder -import org.springframework.test.web.servlet.setup.MockMvcBuilders -import org.springframework.web.context.WebApplicationContext -import spock.lang.Specification - -/** - * Base for specifications that use Spring's {@link MockMvc}. Provides also {@link WebApplicationContext}, - * {@link ApplicationContext}. The latter you can use to specify what - * kind of address should be returned for a given dependency name. - * - * @see WebApplicationContext - * @see ApplicationContext - */ -@CompileStatic -@WebAppConfiguration -abstract class MvcIntegrationSpec extends Specification { - - @Autowired - protected WebApplicationContext webApplicationContext - @Autowired - protected ApplicationContext applicationContext - - protected MockMvc mockMvc - - void setup() { - ConfigurableMockMvcBuilder mockMvcBuilder = MockMvcBuilders.webAppContextSetup(webApplicationContext) - configureMockMvcBuilder(mockMvcBuilder) - mockMvc = mockMvcBuilder.build() - } - - /** - * Override in a subclass to modify mockMvcBuilder configuration (e.g. add filter). - * - * The method from super class should be called. - */ - protected void configureMockMvcBuilder(ConfigurableMockMvcBuilder mockMvcBuilder) { - } -} diff --git a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/MvcWiremockIntegrationSpec.groovy b/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/MvcWiremockIntegrationSpec.groovy deleted file mode 100755 index 5e84ff830..000000000 --- a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/base/MvcWiremockIntegrationSpec.groovy +++ /dev/null @@ -1,37 +0,0 @@ -package org.springframework.cloud.sleuth.correlation.base - -import com.github.tomakehurst.wiremock.client.MappingBuilder -import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder -import com.github.tomakehurst.wiremock.client.WireMock -import groovy.transform.CompileStatic -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.test.context.ContextConfiguration - -/** - * Base specification for tests that use Wiremock as HTTP server stub. - * By extending this specification you gain a bean with {@link HttpMockServer} and a {@link WireMock} - * instance that you can stub by using {@link MvcWiremockIntegrationSpec#stubInteraction(com.github.tomakehurst.wiremock.client.MappingBuilder, com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder)} - * - * @see MockServerConfiguration - * @see WireMock - * @see HttpMockServer - * @see MvcIntegrationSpec - */ -@CompileStatic -@ContextConfiguration(classes = [MockServerConfiguration]) -abstract class MvcWiremockIntegrationSpec extends MvcIntegrationSpec { - - @Autowired - protected HttpMockServer httpMockServer - protected WireMock wireMock - - void setup() { - wireMock = new WireMock('localhost', httpMockServer.port()) - wireMock.resetToDefaultMappings() - } - - protected void stubInteraction(MappingBuilder mapping, ResponseDefinitionBuilder response) { - wireMock.register(mapping.willReturn(response)) - } - -} diff --git a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/hystrix/CorrelatedCommandSpec.groovy b/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/hystrix/CorrelatedCommandSpec.groovy deleted file mode 100644 index e572217a0..000000000 --- a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/hystrix/CorrelatedCommandSpec.groovy +++ /dev/null @@ -1,41 +0,0 @@ -package org.springframework.cloud.sleuth.correlation.hystrix - -import com.netflix.hystrix.HystrixCommand -import com.netflix.hystrix.HystrixCommandGroupKey -import org.springframework.cloud.sleuth.logging.CorrelationIdHolder -import org.springframework.cloud.sleuth.logging.CorrelationIdUpdater -import org.springframework.cloud.sleuth.logging.hystrix.CorrelatedCommand -import spock.lang.Specification - -class CorrelatedCommandSpec extends Specification { - - public static final String CORRELATION_ID = 'A' - - def 'should run Hystrix command with client correlation ID'() { - given: - CorrelationIdUpdater.updateCorrelationId(CORRELATION_ID) - def command = new CorrelatedCommand(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(""))) { - String doRun() { - return CorrelationIdHolder.get() - } - } - when: - def result = command.execute() - then: - result == CORRELATION_ID - } - - def 'should run Hystrix command in different thread'() { - given: - def command = new CorrelatedCommand(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(""))) { - String doRun() { - return Thread.currentThread().name - } - } - when: - def threadName = command.execute() - then: - Thread.currentThread().name != threadName - } - -} diff --git a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/scheduling/CorrelationIdOnScheduledMethodISpec.groovy b/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/scheduling/CorrelationIdOnScheduledMethodISpec.groovy deleted file mode 100644 index aef08e234..000000000 --- a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/scheduling/CorrelationIdOnScheduledMethodISpec.groovy +++ /dev/null @@ -1,25 +0,0 @@ -package org.springframework.cloud.sleuth.correlation.scheduling - -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.cloud.sleuth.correlation.base.BaseConfiguration -import org.springframework.cloud.sleuth.logging.CorrelationIdAutoConfiguration -import org.springframework.cloud.sleuth.logging.scheduling.TaskSchedulingConfiguration -import org.springframework.test.context.ContextConfiguration -import spock.lang.Specification -import spock.util.concurrent.PollingConditions - -@ContextConfiguration(classes = [TaskSchedulingConfiguration, ScheduledBeanConfiguration, CorrelationIdAutoConfiguration, BaseConfiguration]) -class CorrelationIdOnScheduledMethodISpec extends Specification { - - @Autowired - TestBeanWithScheduledMethod beanWithScheduledMethod - - def "should have correlationId set after scheduled method has been called"() { - PollingConditions conditions = new PollingConditions(timeout: 1.5, initialDelay: 0.1, factor: 1.05) - expect: - conditions.eventually { - beanWithScheduledMethod.correlationId != null - } - } - -} diff --git a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/scheduling/ScheduledBeanConfiguration.groovy b/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/scheduling/ScheduledBeanConfiguration.groovy deleted file mode 100644 index 7bddf1cdb..000000000 --- a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/scheduling/ScheduledBeanConfiguration.groovy +++ /dev/null @@ -1,14 +0,0 @@ -package org.springframework.cloud.sleuth.correlation.scheduling - -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration - -@Configuration -class ScheduledBeanConfiguration { - - @Bean - TestBeanWithScheduledMethod testBeanWithScheduledMethod() { - return new TestBeanWithScheduledMethod() - } - -} diff --git a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/scheduling/TestBeanWithScheduledMethod.groovy b/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/scheduling/TestBeanWithScheduledMethod.groovy deleted file mode 100644 index 212b159c3..000000000 --- a/spring-cloud-sleuth-correlation/src/test/groovy/org/springframework/cloud/sleuth/correlation/scheduling/TestBeanWithScheduledMethod.groovy +++ /dev/null @@ -1,15 +0,0 @@ -package org.springframework.cloud.sleuth.correlation.scheduling - -import org.springframework.cloud.sleuth.logging.CorrelationIdHolder -import org.springframework.scheduling.annotation.Scheduled - -class TestBeanWithScheduledMethod { - - String correlationId - - @Scheduled(fixedDelay = 50L) - void scheduledMethod() { - correlationId = CorrelationIdHolder.get() - } - -}