From 5fa8409114e048b37d5fe39e2ecceb3fae08cbcb Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Wed, 29 Jul 2020 15:53:55 +0200 Subject: [PATCH] Reuses messaging properties for remote service name for brokers; fixes gh-1691 --- ...raceSpringIntegrationAutoConfiguration.java | 5 +++-- .../messaging/TracingChannelInterceptor.java | 18 +++++++++++------- .../TraceWebSocketAutoConfiguration.java | 14 ++++++++++---- .../TracingChannelInterceptorTest.java | 3 ++- 4 files changed, 26 insertions(+), 14 deletions(-) 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 f2a8ea58f..85e6b836e 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 @@ -64,10 +64,11 @@ public class TraceSpringIntegrationAutoConfiguration { @Bean TracingChannelInterceptor traceChannelInterceptor(Tracing tracing, + SleuthMessagingProperties properties, Propagation.Setter traceMessagePropagationSetter, Propagation.Getter traceMessagePropagationGetter) { - return new TracingChannelInterceptor(tracing, traceMessagePropagationSetter, - traceMessagePropagationGetter); + return new TracingChannelInterceptor(tracing, properties, + traceMessagePropagationSetter, traceMessagePropagationGetter); } } diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/TracingChannelInterceptor.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/TracingChannelInterceptor.java index e87048f63..4e8bb6d13 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/TracingChannelInterceptor.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/TracingChannelInterceptor.java @@ -103,6 +103,8 @@ public final class TracingChannelInterceptor extends ChannelInterceptorAdapter final TraceContext.Extractor extractor; + final SleuthMessagingProperties properties; + final boolean integrationObjectSupportPresent; private final boolean hasDirectChannelClass; @@ -111,15 +113,16 @@ public final class TracingChannelInterceptor extends ChannelInterceptorAdapter private final Class directWithAttributesChannelClass; @Autowired - TracingChannelInterceptor(Tracing tracing) { - this(tracing, MessageHeaderPropagation.INSTANCE, + TracingChannelInterceptor(Tracing tracing, SleuthMessagingProperties properties) { + this(tracing, properties, MessageHeaderPropagation.INSTANCE, MessageHeaderPropagation.INSTANCE); } - TracingChannelInterceptor(Tracing tracing, + TracingChannelInterceptor(Tracing tracing, SleuthMessagingProperties properties, Propagation.Setter setter, Propagation.Getter getter) { this.tracing = tracing; + this.properties = properties; this.tracer = tracing.tracer(); this.threadLocalSpan = ThreadLocalSpan.create(this.tracer); this.injector = tracing.propagation().injector(setter); @@ -133,8 +136,9 @@ public final class TracingChannelInterceptor extends ChannelInterceptorAdapter ? ClassUtils.resolveClassName(STREAM_DIRECT_CHANNEL, null) : null; } - public static TracingChannelInterceptor create(Tracing tracing) { - return new TracingChannelInterceptor(tracing); + public static TracingChannelInterceptor create(Tracing tracing, + SleuthMessagingProperties properties) { + return new TracingChannelInterceptor(tracing, properties); } /** @@ -194,10 +198,10 @@ public final class TracingChannelInterceptor extends ChannelInterceptorAdapter private String toRemoteServiceName(MessageHeaderAccessor headers) { for (String key : headers.getMessageHeaders().keySet()) { if (key.startsWith("kafka_")) { - return "kafka"; + return this.properties.getMessaging().getKafka().getRemoteServiceName(); } else if (key.startsWith("amqp_")) { - return "rabbitmq"; + return this.properties.getMessaging().getRabbit().getRemoteServiceName(); } } return REMOTE_SERVICE_NAME; diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/websocket/TraceWebSocketAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/websocket/TraceWebSocketAutoConfiguration.java index 600c044b9..45d8672cb 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/websocket/TraceWebSocketAutoConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/websocket/TraceWebSocketAutoConfiguration.java @@ -22,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired; 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.instrument.messaging.SleuthMessagingProperties; import org.springframework.cloud.sleuth.instrument.messaging.TracingChannelInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.ChannelRegistration; @@ -52,6 +53,9 @@ public class TraceWebSocketAutoConfiguration @Autowired Tracing tracing; + @Autowired + SleuthMessagingProperties properties; + @Override public void registerStompEndpoints(StompEndpointRegistry registry) { // The user must register their own endpoints @@ -59,18 +63,20 @@ public class TraceWebSocketAutoConfiguration @Override public void configureMessageBroker(MessageBrokerRegistry registry) { - registry.configureBrokerChannel() - .setInterceptors(TracingChannelInterceptor.create(this.tracing)); + registry.configureBrokerChannel().setInterceptors( + TracingChannelInterceptor.create(this.tracing, this.properties)); } @Override public void configureClientOutboundChannel(ChannelRegistration registration) { - registration.setInterceptors(TracingChannelInterceptor.create(this.tracing)); + registration.setInterceptors( + TracingChannelInterceptor.create(this.tracing, this.properties)); } @Override public void configureClientInboundChannel(ChannelRegistration registration) { - registration.setInterceptors(TracingChannelInterceptor.create(this.tracing)); + registration.setInterceptors( + TracingChannelInterceptor.create(this.tracing, this.properties)); } } diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/messaging/TracingChannelInterceptorTest.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/messaging/TracingChannelInterceptorTest.java index 380f77d96..6337faa26 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/messaging/TracingChannelInterceptorTest.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/messaging/TracingChannelInterceptorTest.java @@ -59,7 +59,8 @@ public class TracingChannelInterceptorTest { Tracing tracing = Tracing.newBuilder().currentTraceContext(this.currentTraceContext) .addSpanHandler(this.spans).build(); - ChannelInterceptor interceptor = TracingChannelInterceptor.create(tracing); + ChannelInterceptor interceptor = TracingChannelInterceptor.create(tracing, + new SleuthMessagingProperties()); QueueChannel channel = new QueueChannel();