From c72bb22cf938615c6100934357ac52218a12f9ab Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Fri, 10 Nov 2017 15:21:21 +0800 Subject: [PATCH 1/3] Updates to zipkin version fixing encoding bug --- spring-cloud-sleuth-dependencies/pom.xml | 11 +++++------ spring-cloud-sleuth-samples/pom.xml | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/spring-cloud-sleuth-dependencies/pom.xml b/spring-cloud-sleuth-dependencies/pom.xml index 2dedde5de..a93efad67 100644 --- a/spring-cloud-sleuth-dependencies/pom.xml +++ b/spring-cloud-sleuth-dependencies/pom.xml @@ -14,11 +14,9 @@ spring-cloud-sleuth-dependencies Spring Cloud Sleuth Dependencies - - 2.2.0 - 2.2.1 + 2.2.2 1.1.2 - 2.1.3 + 2.1.4 @@ -70,12 +68,13 @@ io.zipkin.java zipkin - ${zipkin.version} + + 2.2.1 io.zipkin.zipkin2 zipkin - ${zipkin2.version} + ${zipkin.version} io.zipkin.java diff --git a/spring-cloud-sleuth-samples/pom.xml b/spring-cloud-sleuth-samples/pom.xml index c041b9129..8088a868d 100644 --- a/spring-cloud-sleuth-samples/pom.xml +++ b/spring-cloud-sleuth-samples/pom.xml @@ -57,7 +57,7 @@ io.zipkin.zipkin2 zipkin - 2.2.1 + 2.2.2 From 360f7ed18776a5f3d3e7a521ff8ecb7333005dd9 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 14 Nov 2017 11:32:16 +0100 Subject: [PATCH 2/3] Removed type target from New/ContinueSpan; fixes #772 --- .../springframework/cloud/sleuth/annotation/ContinueSpan.java | 2 +- .../org/springframework/cloud/sleuth/annotation/NewSpan.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/annotation/ContinueSpan.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/annotation/ContinueSpan.java index b013ffa65..48b3e69d9 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/annotation/ContinueSpan.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/annotation/ContinueSpan.java @@ -31,7 +31,7 @@ import java.lang.annotation.Target; */ @Retention(RetentionPolicy.RUNTIME) @Inherited -@Target(value = { ElementType.METHOD, ElementType.TYPE }) +@Target(value = { ElementType.METHOD }) public @interface ContinueSpan { /** diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/annotation/NewSpan.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/annotation/NewSpan.java index fa97deaeb..c01cc67ad 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/annotation/NewSpan.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/annotation/NewSpan.java @@ -24,7 +24,7 @@ import java.lang.annotation.Target; import org.springframework.core.annotation.AliasFor; /** - * Allows to create a new span around a public method or a class. The new span + * Allows to create a new span around a public method. The new span * will be either a child of an existing span if a trace is already in progress * or a new span will be created if there was no previous trace. *

@@ -38,7 +38,7 @@ import org.springframework.core.annotation.AliasFor; */ @Retention(RetentionPolicy.RUNTIME) @Inherited -@Target(value = { ElementType.METHOD, ElementType.TYPE }) +@Target(value = { ElementType.METHOD }) public @interface NewSpan { /** From e2c11b592c923553c6003a04ce1251627f8e43ff Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 14 Nov 2017 14:57:27 +0100 Subject: [PATCH 3/3] Added RestTemplateBuilder support fixes #777 --- .../TraceWebClientAutoConfiguration.java | 28 +++++++++++++++++ .../client/integration/WebClientTests.java | 30 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/TraceWebClientAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/TraceWebClientAutoConfiguration.java index 2e599ce86..167e8ab2a 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/TraceWebClientAutoConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/TraceWebClientAutoConfiguration.java @@ -30,6 +30,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateCustomizer; +import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.cloud.sleuth.ErrorParser; import org.springframework.cloud.sleuth.Tracer; import org.springframework.cloud.sleuth.instrument.web.HttpSpanInjector; @@ -65,6 +66,33 @@ public class TraceWebClientAutoConfiguration { httpTraceKeysInjector, errorParser); } + @Bean + public BeanPostProcessor traceRestTemplateBuilderBPP(BeanFactory beanFactory) { + return new TraceRestTemplateBuilderBPP(beanFactory); + } + + private static class TraceRestTemplateBuilderBPP implements BeanPostProcessor { + private final BeanFactory beanFactory; + + private TraceRestTemplateBuilderBPP(BeanFactory beanFactory) { + this.beanFactory = beanFactory; + } + + @Override public Object postProcessBeforeInitialization(Object o, String s) + throws BeansException { + return o; + } + + @Override public Object postProcessAfterInitialization(Object o, String s) + throws BeansException { + if (o instanceof RestTemplateBuilder) { + RestTemplateBuilder builder = (RestTemplateBuilder) o; + return builder.additionalInterceptors(this.beanFactory.getBean(TraceRestTemplateInterceptor.class)); + } + return o; + } + } + @Configuration protected static class TraceInterceptorConfiguration { diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/client/integration/WebClientTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/client/integration/WebClientTests.java index 4f283fc69..d757f5554 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/client/integration/WebClientTests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/client/integration/WebClientTests.java @@ -29,6 +29,7 @@ import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.LogFactory; +import org.assertj.core.api.BDDAssertions; import org.junit.After; import org.junit.ClassRule; import org.junit.Rule; @@ -40,7 +41,9 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.web.BasicErrorController; import org.springframework.boot.autoconfigure.web.ErrorAttributes; import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.cloud.netflix.feign.FeignClient; @@ -97,6 +100,9 @@ public class WebClientTests { @Autowired ArrayListSpanAccumulator listener; @Autowired Tracer tracer; @Autowired TestErrorController testErrorController; + @Autowired RestTemplateBuilder restTemplateBuilder; + @LocalServerPort int port; + @Autowired FooController fooController; @After public void close() { @@ -269,6 +275,23 @@ public class WebClientTests { then(this.testErrorController.getSpan()).isNull(); } + @Test + public void should_wrap_rest_template_builders() { + Span span = this.tracer.createSpan("foo"); + try { + RestTemplate template = this.restTemplateBuilder.build(); + + template.getForObject("http://localhost:" + this.port + "/traceid", String.class); + + Span spanInController = this.fooController.getSpan(); + BDDAssertions.then(spanInController).isNotNull(); + then(spanInController.getTraceId()).isEqualTo(span.getTraceId()); + } finally { + this.tracer.close(span); + } + then(this.tracer.getCurrentSpan()).isNull(); + } + private void thenRegisteredClientSentAndReceivedEvents(Span span) { then(span).hasLoggedAnEvent(Span.CLIENT_RECV); then(span).hasLoggedAnEvent(Span.CLIENT_SEND); @@ -363,6 +386,8 @@ public class WebClientTests { @Autowired Tracer tracer; + Span span; + @RequestMapping(value = "/notrace", method = RequestMethod.GET) public String notrace( @RequestHeader(name = Span.TRACE_ID_NAME, required = false) String traceId) { @@ -377,6 +402,7 @@ public class WebClientTests { then(traceId).isNotEmpty(); then(parentId).isNotEmpty(); then(spanId).isNotEmpty(); + this.span = this.tracer.getCurrentSpan(); return traceId; } @@ -401,6 +427,10 @@ public class WebClientTests { then(parentId).isNotEmpty(); then(spanId).isNotEmpty(); } + + public Span getSpan() { + return this.span; + } } @Configuration