Reuses messaging properties for remote service name for brokers; fixes gh-1691

This commit is contained in:
Marcin Grzejszczak
2020-07-29 15:53:55 +02:00
parent 97574373a9
commit 5fa8409114
4 changed files with 26 additions and 14 deletions

View File

@@ -64,10 +64,11 @@ public class TraceSpringIntegrationAutoConfiguration {
@Bean
TracingChannelInterceptor traceChannelInterceptor(Tracing tracing,
SleuthMessagingProperties properties,
Propagation.Setter<MessageHeaderAccessor, String> traceMessagePropagationSetter,
Propagation.Getter<MessageHeaderAccessor, String> traceMessagePropagationGetter) {
return new TracingChannelInterceptor(tracing, traceMessagePropagationSetter,
traceMessagePropagationGetter);
return new TracingChannelInterceptor(tracing, properties,
traceMessagePropagationSetter, traceMessagePropagationGetter);
}
}

View File

@@ -103,6 +103,8 @@ public final class TracingChannelInterceptor extends ChannelInterceptorAdapter
final TraceContext.Extractor<MessageHeaderAccessor> 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<MessageHeaderAccessor, String> setter,
Propagation.Getter<MessageHeaderAccessor, String> 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;

View File

@@ -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));
}
}

View File

@@ -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();