From 2190cccb8c80bb6ee7919fa7b8b90d41e30f694e Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 22 May 2018 18:57:36 +0200 Subject: [PATCH] Added hystrix to skip pattern of scheduled and messaging channel name fixes gh-986 --- .../src/main/asciidoc/spring-cloud-sleuth.adoc | 8 +++----- .../messaging/SleuthMessagingProperties.java | 18 ++++++++++++++++++ ...raceSpringIntegrationAutoConfiguration.java | 13 +++++++++++-- .../scheduling/SleuthSchedulingProperties.java | 2 +- ...ditional-spring-configuration-metadata.json | 6 ------ 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/docs/src/main/asciidoc/spring-cloud-sleuth.adoc b/docs/src/main/asciidoc/spring-cloud-sleuth.adoc index 886381494..fdf308789 100644 --- a/docs/src/main/asciidoc/spring-cloud-sleuth.adoc +++ b/docs/src/main/asciidoc/spring-cloud-sleuth.adoc @@ -1161,10 +1161,8 @@ If you annotate your method with `@Scheduled`, we automatically create a new spa * The span is tagged with the method's class name and method name. If you want to skip span creation for some `@Scheduled` annotated classes, you can set the `spring.sleuth.scheduled.skipPattern` with a regular expression that matches the fully qualified name of the `@Scheduled` annotated class. - -TIP: If you use `spring-cloud-sleuth-stream` and `spring-cloud-netflix-hystrix-stream` together, a span is created for each Hystrix metrics and sent to Zipkin. -This behavior may be annoying. -You can prevent it by setting `spring.sleuth.scheduled.skipPattern=org.springframework.cloud.netflix.hystrix.stream.HystrixStreamTask`. +If you use `spring-cloud-sleuth-stream` and `spring-cloud-netflix-hystrix-stream` together, a span is created for each Hystrix metrics and sent to Zipkin. +This behavior may be annoying. That's why, by default, `spring.sleuth.scheduled.skipPattern=org.springframework.cloud.netflix.hystrix.stream.HystrixStreamTask`. ==== Executor, ExecutorService, and ScheduledExecutorService @@ -1202,7 +1200,7 @@ It creates spans for publish and subscribe events. To disable Spring Integration instrumentation, set `spring.sleuth.integration.enabled` to `false`. You can provide the `spring.sleuth.integration.patterns` pattern to explicitly provide the names of channels that you want to include for tracing. -By default, all channels are included. +By default, all channels but `hystrixStreamOutput` channel are included. IMPORTANT: When using the `Executor` to build a Spring Integration `IntegrationFlow`, you must use the untraced version of the `Executor`. Decorating the Spring Integration Executor Channel with `TraceableExecutorService` causes the spans to be improperly closed. diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/SleuthMessagingProperties.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/SleuthMessagingProperties.java index dcf7f870f..8296b7637 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/SleuthMessagingProperties.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/SleuthMessagingProperties.java @@ -46,8 +46,26 @@ public class SleuthMessagingProperties { } public static class Integration { + /** + * An array of patterns against which channel names will be matched. + * @see org.springframework.integration.config.GlobalChannelInterceptor#patterns(). + * Defaults to any channel name not matching the Hystrix Stream channel name. + */ + private String[] patterns = new String[] { "!hystrixStreamOutput*", "*" }; + + /** + * Enable Spring Integration sleuth instrumentation + */ private boolean enabled; + public String[] getPatterns() { + return this.patterns; + } + + public void setPatterns(String[] patterns) { + this.patterns = patterns; + } + public boolean isEnabled() { return this.enabled; } diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/TraceSpringIntegrationAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/TraceSpringIntegrationAutoConfiguration.java index 50d80e031..58ae2eda9 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/TraceSpringIntegrationAutoConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/TraceSpringIntegrationAutoConfiguration.java @@ -25,6 +25,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper; import org.springframework.integration.config.GlobalChannelInterceptor; /** @@ -47,8 +48,16 @@ import org.springframework.integration.config.GlobalChannelInterceptor; public class TraceSpringIntegrationAutoConfiguration { @Bean - @GlobalChannelInterceptor(patterns = "${spring.sleuth.integration.patterns:*}") - public TracingChannelInterceptor traceChannelInterceptor(Tracing tracing) { + public GlobalChannelInterceptorWrapper tracingGlobalChannelInterceptorWrapper( + TracingChannelInterceptor interceptor, + SleuthMessagingProperties properties) { + GlobalChannelInterceptorWrapper wrapper = new GlobalChannelInterceptorWrapper(interceptor); + wrapper.setPatterns(properties.getIntegration().getPatterns()); + return wrapper; + } + + @Bean + TracingChannelInterceptor traceChannelInterceptor(Tracing tracing) { return new TracingChannelInterceptor(tracing); } diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/scheduling/SleuthSchedulingProperties.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/scheduling/SleuthSchedulingProperties.java index 724f76b9c..f707a5e0c 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/scheduling/SleuthSchedulingProperties.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/scheduling/SleuthSchedulingProperties.java @@ -35,7 +35,7 @@ public class SleuthSchedulingProperties { /** * Pattern for the fully qualified name of a class that should be skipped. */ - private String skipPattern = ""; + private String skipPattern = "org.springframework.cloud.netflix.hystrix.stream.HystrixStreamTask"; public boolean isEnabled() { return this.enabled; diff --git a/spring-cloud-sleuth-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-sleuth-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json index f09f6cda2..100fdd224 100644 --- a/spring-cloud-sleuth-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-sleuth-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -1,10 +1,4 @@ {"properties": [ - { - "name": "spring.sleuth.integration.patterns", - "type": "java.lang.String[]", - "description": "An array of simple patterns against which channel names will be matched. Default is * (all channels). See org.springframework.util.PatternMatchUtils.simpleMatch(String, String).", - "defaultValue": "*" - }, { "name": "spring.sleuth.integration.enabled", "type": "java.lang.Boolean",