Add test for closing span hierarchy in message handler
This commit is contained in:
@@ -135,7 +135,9 @@ public class DefaultTraceManager implements TraceManager {
|
||||
TraceContextHolder.setCurrentTrace(savedTrace);
|
||||
}
|
||||
else {
|
||||
this.publisher.publishEvent(new SpanReleasedEvent(this, span));
|
||||
if (!span.isRemote()) {
|
||||
this.publisher.publishEvent(new SpanReleasedEvent(this, span));
|
||||
}
|
||||
TraceContextHolder.removeCurrentTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,10 +16,14 @@
|
||||
|
||||
package org.springframework.cloud.sleuth.instrument.integration;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -32,11 +36,13 @@ import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.cloud.sleuth.Trace;
|
||||
import org.springframework.cloud.sleuth.TraceManager;
|
||||
import org.springframework.cloud.sleuth.event.SpanReleasedEvent;
|
||||
import org.springframework.cloud.sleuth.instrument.integration.TraceChannelInterceptorTests.App;
|
||||
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
|
||||
import org.springframework.cloud.sleuth.trace.TraceContextHolder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
@@ -61,6 +67,9 @@ public class TraceChannelInterceptorTests implements MessageHandler {
|
||||
@Autowired
|
||||
private TraceManager traceManager;
|
||||
|
||||
@Autowired
|
||||
private App app;
|
||||
|
||||
private Message<?> message;
|
||||
|
||||
private Span span;
|
||||
@@ -94,6 +103,20 @@ public class TraceChannelInterceptorTests implements MessageHandler {
|
||||
assertFalse(this.span.isExportable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parentSpanIncluded() {
|
||||
this.channel.send(MessageBuilder.withPayload("hi").setHeader(Trace.TRACE_ID_NAME, "parent")
|
||||
.build());
|
||||
assertNotNull("message was null", this.message);
|
||||
|
||||
String spanId = this.message.getHeaders().get(Trace.SPAN_ID_NAME, String.class);
|
||||
assertNotNull("spanId was null", spanId);
|
||||
String traceId = this.message.getHeaders().get(Trace.TRACE_ID_NAME, String.class);
|
||||
assertEquals("parent", traceId);
|
||||
assertNull(TraceContextHolder.getCurrentTrace());
|
||||
assertEquals(1, this.app.events.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void spanCreation() {
|
||||
this.channel.send(MessageBuilder.withPayload("hi").build());
|
||||
@@ -127,15 +150,17 @@ public class TraceChannelInterceptorTests implements MessageHandler {
|
||||
@EnableAutoConfiguration
|
||||
static class App {
|
||||
|
||||
private List<SpanReleasedEvent> events = new ArrayList<>();
|
||||
|
||||
@EventListener
|
||||
public void handle(SpanReleasedEvent event) {
|
||||
this.events.add(event);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DirectChannel channel() {
|
||||
return new DirectChannel();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AlwaysSampler alwaysSampler() {
|
||||
return new AlwaysSampler();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ zipkin:
|
||||
---
|
||||
spring:
|
||||
profiles: test
|
||||
datasource:
|
||||
initialize: false
|
||||
zipkin:
|
||||
store:
|
||||
type: mem # default is inMemory
|
||||
|
||||
Reference in New Issue
Block a user