Added patterns to @GlobalChannelInterceptor (#337)

This commit is contained in:
Marcin Grzejszczak
2016-07-15 15:48:36 +02:00
parent 1ec2bc7833
commit b4bcd531d5
4 changed files with 75 additions and 23 deletions

View File

@@ -54,7 +54,7 @@ import org.springframework.messaging.support.MessageBuilder;
public class TraceSpringIntegrationAutoConfiguration {
@Bean
@GlobalChannelInterceptor
@GlobalChannelInterceptor(patterns = "#{environment.getProperty('spring.sleuth.integration.patterns') ?: '*'}")
public TraceChannelInterceptor traceChannelInterceptor(Tracer tracer,
TraceKeys traceKeys, Random random, SpanExtractor<Message<?>> spanExtractor,
SpanInjector<MessageBuilder<?>> spanInjector) {

View File

@@ -0,0 +1,14 @@
{"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",
"description": "Enable Spring Integration sleuth instrumentation.",
"defaultValue": true
}
]}

View File

@@ -47,6 +47,7 @@ import org.springframework.messaging.MessagingException;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.BDDAssertions.then;
import static org.junit.Assert.assertNotNull;
import static org.springframework.cloud.sleuth.assertions.SleuthAssertions.then;
@@ -55,13 +56,17 @@ import static org.springframework.cloud.sleuth.assertions.SleuthAssertions.then;
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = App.class)
@IntegrationTest
@IntegrationTest("spring.sleuth.integration.patterns=traced*")
@DirtiesContext
public class TraceChannelInterceptorTests implements MessageHandler {
@Autowired
@Qualifier("channel")
private DirectChannel channel;
@Qualifier("tracedChannel")
private DirectChannel tracedChannel;
@Autowired
@Qualifier("ignoredChannel")
private DirectChannel ignoredChannel;
@Autowired
private Tracer tracer;
@@ -87,20 +92,22 @@ public class TraceChannelInterceptorTests implements MessageHandler {
@Before
public void init() {
this.channel.subscribe(this);
this.tracedChannel.subscribe(this);
this.ignoredChannel.subscribe(this);
this.accumulator.getSpans().clear();
}
@After
public void close() {
TestSpanContextHolder.removeCurrentSpan();
this.channel.unsubscribe(this);
this.tracedChannel.unsubscribe(this);
this.ignoredChannel.unsubscribe(this);
this.accumulator.getSpans().clear();
}
@Test
public void nonExportableSpanCreation() {
this.channel.send(MessageBuilder.withPayload("hi")
this.tracedChannel.send(MessageBuilder.withPayload("hi")
.setHeader(Span.SAMPLED_NAME, Span.SPAN_NOT_SAMPLED).build());
assertNotNull("message was null", this.message);
@@ -112,7 +119,7 @@ public class TraceChannelInterceptorTests implements MessageHandler {
@Test
public void parentSpanIncluded() {
this.channel.send(MessageBuilder.withPayload("hi")
this.tracedChannel.send(MessageBuilder.withPayload("hi")
.setHeader(Span.TRACE_ID_NAME, Span.idToHex(10L))
.setHeader(Span.SPAN_ID_NAME, Span.idToHex(20L)).build());
then(this.message).isNotNull();
@@ -129,7 +136,7 @@ public class TraceChannelInterceptorTests implements MessageHandler {
// #332
@Test
public void shouldSendNewAndOldHeadersWhenNewHeadersWerePassed() {
this.channel.send(MessageBuilder.withPayload("hi")
this.tracedChannel.send(MessageBuilder.withPayload("hi")
.setHeader(TraceMessageHeaders.TRACE_ID_NAME, Span.idToHex(10L))
.setHeader(TraceMessageHeaders.SPAN_ID_NAME, Span.idToHex(20L)).build());
then(this.message).isNotNull();
@@ -141,7 +148,8 @@ public class TraceChannelInterceptorTests implements MessageHandler {
}
private String thenNewSpanIdEqualsOldSpanId() {
String newSpanId = this.message.getHeaders().get(TraceMessageHeaders.SPAN_ID_NAME, String.class);
String newSpanId = this.message.getHeaders().get(TraceMessageHeaders.SPAN_ID_NAME,
String.class);
then(newSpanId).isNotNull();
String oldSpanId = this.message.getHeaders().get(Span.SPAN_ID_NAME, String.class);
then(oldSpanId).isEqualTo(newSpanId);
@@ -151,7 +159,7 @@ public class TraceChannelInterceptorTests implements MessageHandler {
// #332
@Test
public void shouldSendNewAndOldHeadersWhenOldHeadersWerePassed() {
this.channel.send(MessageBuilder.withPayload("hi")
this.tracedChannel.send(MessageBuilder.withPayload("hi")
.setHeader(Span.TRACE_ID_NAME, Span.idToHex(10L))
.setHeader(Span.SPAN_ID_NAME, Span.idToHex(20L)).build());
then(this.message).isNotNull();
@@ -163,8 +171,8 @@ public class TraceChannelInterceptorTests implements MessageHandler {
}
private void thenNewTraceIdEqualsOldTraceId() {
long traceId = Span
.hexToId(this.message.getHeaders().get(TraceMessageHeaders.TRACE_ID_NAME, String.class));
long traceId = Span.hexToId(this.message.getHeaders()
.get(TraceMessageHeaders.TRACE_ID_NAME, String.class));
then(traceId).isEqualTo(10L);
long oldTraceId = Span
.hexToId(this.message.getHeaders().get(Span.TRACE_ID_NAME, String.class));
@@ -173,7 +181,7 @@ public class TraceChannelInterceptorTests implements MessageHandler {
@Test
public void spanCreation() {
this.channel.send(MessageBuilder.withPayload("hi").build());
this.tracedChannel.send(MessageBuilder.withPayload("hi").build());
then(this.message).isNotNull();
String spanId = this.message.getHeaders().get(Span.SPAN_ID_NAME, String.class);
@@ -186,22 +194,25 @@ public class TraceChannelInterceptorTests implements MessageHandler {
@Test
public void shouldLogClientReceivedClientSentEventWhenTheMessageIsSentAndReceived() {
this.channel.send(MessageBuilder.withPayload("hi").build());
this.tracedChannel.send(MessageBuilder.withPayload("hi").build());
then(this.span.logs()).extracting("event").contains(Span.CLIENT_SEND, Span.CLIENT_RECV);
then(this.span.logs()).extracting("event").contains(Span.CLIENT_SEND,
Span.CLIENT_RECV);
}
@Test
public void shouldLogServerReceivedServerSentEventWhenTheMessageIsPropagatedToTheNextListener() {
this.channel.send(MessageBuilder.withPayload("hi").setHeader("X-Message-Sent", true).build());
this.tracedChannel.send(MessageBuilder.withPayload("hi")
.setHeader("X-Message-Sent", true).build());
then(this.span.logs()).extracting("event").contains(Span.SERVER_RECV, Span.SERVER_SEND);
then(this.span.logs()).extracting("event").contains(Span.SERVER_RECV,
Span.SERVER_SEND);
}
@Test
public void headerCreation() {
Span span = this.tracer.createSpan("http:testSendMessage", new AlwaysSampler());
this.channel.send(MessageBuilder.withPayload("hi").build());
this.tracedChannel.send(MessageBuilder.withPayload("hi").build());
this.tracer.close(span);
then(this.message).isNotNull();
@@ -237,15 +248,33 @@ public class TraceChannelInterceptorTests implements MessageHandler {
errorHeaders.put("THROW_EXCEPTION", "TRUE");
try {
this.messagingTemplate.send(MessageBuilder.withPayload("hi").copyHeaders(errorHeaders).build());
this.messagingTemplate.send(
MessageBuilder.withPayload("hi").copyHeaders(errorHeaders).build());
SleuthAssertions.fail("Exception should occur");
} catch (RuntimeException e) {}
}
catch (RuntimeException e) {
}
then(this.message).isNotNull();
this.tracer.close(span);
then(TestSpanContextHolder.getCurrentSpan()).isNull();
}
@Test
public void shouldNotTraceIgnoredChannel() {
this.ignoredChannel.send(MessageBuilder.withPayload("hi").build());
then(this.message).isNotNull();
String spanId = this.message.getHeaders().get(Span.SPAN_ID_NAME, String.class);
then(spanId).isNull();
String traceId = this.message.getHeaders().get(Span.TRACE_ID_NAME, String.class);
then(traceId).isNull();
then(accumulator.getSpans()).isEmpty();
then(TestSpanContextHolder.getCurrentSpan()).isNull();
}
@Configuration
@EnableAutoConfiguration
static class App {
@@ -256,13 +285,18 @@ public class TraceChannelInterceptorTests implements MessageHandler {
}
@Bean
public DirectChannel channel() {
public DirectChannel tracedChannel() {
return new DirectChannel();
}
@Bean
public DirectChannel ignoredChannel() {
return new DirectChannel();
}
@Bean
public MessagingTemplate messagingTemplate() {
return new MessagingTemplate(channel());
return new MessagingTemplate(tracedChannel());
}
@Bean