[#128] Added spans for messaging

fixes #128
This commit is contained in:
Marcin Grzejszczak
2016-01-22 23:03:44 +01:00
committed by Marcin Grzejszczak
parent 4dca83f248
commit 0ff1cf92bd
5 changed files with 73 additions and 32 deletions

View File

@@ -15,6 +15,12 @@
*/
package integration;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;
import integration.MessagingApplicationTests.IntegrationSpanCollectorConfig;
import org.junit.After;
import org.junit.Test;
@@ -31,10 +37,6 @@ import sample.SampleMessagingApplication;
import tools.AbstractIntegrationTest;
import zipkin.Span;
import java.util.Collection;
import java.util.Optional;
import java.util.Random;
import static org.assertj.core.api.BDDAssertions.then;
@RunWith(SpringJUnit4ClassRunner.class)
@@ -43,6 +45,8 @@ import static org.assertj.core.api.BDDAssertions.then;
@TestPropertySource(properties="sample.zipkin.enabled=true")
public class MessagingApplicationTests extends AbstractIntegrationTest {
private static final String SERVER_SENT = "ss";
private static final String CLIENT_RECEIVED = "cr";
private static int port = 3381;
private static String sampleAppUrl = "http://localhost:" + port;
@Autowired IntegrationTestZipkinSpanReporter integrationTestSpanCollector;
@@ -72,7 +76,7 @@ public class MessagingApplicationTests extends AbstractIntegrationTest {
await().until(() -> {
thenAllSpansHaveTraceIdEqualTo(traceId);
thenTheLastSpansParentHasIdEqualToFirstSpansId();
thenTheSpansHaveProperParentStructure();
});
}
@@ -99,14 +103,56 @@ public class MessagingApplicationTests extends AbstractIntegrationTest {
then(this.integrationTestSpanCollector.hashedSpans.stream().allMatch(span -> span.traceId == traceId)).isTrue();
}
private void thenTheLastSpansParentHasIdEqualToFirstSpansId() {
Optional<Span> firstSpan = this.integrationTestSpanCollector.hashedSpans.stream()
.filter(span -> "http/".equals(span.name) && span.parentId != null).findFirst();
Optional<Span> lastSpan = this.integrationTestSpanCollector.hashedSpans.stream()
private void thenTheSpansHaveProperParentStructure() {
Optional<Span> firstHttpSpan = findFirstHttpRequestSpan();
List<Span> eventSpans = findAllEventRelatedSpans();
Optional<Span> eventSentSpan = findSpanWithServerSentAnnotation(eventSpans);
Optional<Span> eventReceivedSpan = findSpanWithClientReceivedAnnotation(eventSpans);
Optional<Span> lastHttpSpan = findLastHttpSpan();
thenAllSpansArePresent(firstHttpSpan, eventSpans, lastHttpSpan, eventSentSpan, eventReceivedSpan);
then(lastHttpSpan.get().parentId).isEqualTo(eventSentSpan.get().id);
then(eventSentSpan.get().parentId).isEqualTo(firstHttpSpan.get().id);
then(eventSentSpan.get()).isNotEqualTo(eventReceivedSpan.get());
}
private Optional<Span> findLastHttpSpan() {
return this.integrationTestSpanCollector.hashedSpans.stream()
.filter(span -> "http/foo".equals(span.name)).findFirst();
then(firstSpan.isPresent()).isTrue();
then(lastSpan.isPresent()).isTrue();
then(lastSpan.get().parentId).isEqualTo(firstSpan.get().id);
}
private Optional<Span> findSpanWithClientReceivedAnnotation(List<Span> eventSpans) {
return eventSpans.stream()
.filter(span -> span.annotations.stream().filter(annotation -> CLIENT_RECEIVED
.equals(annotation.value)).findFirst().isPresent())
.findFirst();
}
private Optional<Span> findSpanWithServerSentAnnotation(List<Span> eventSpans) {
return eventSpans.stream()
.filter(span -> span.annotations.stream().filter(annotation -> SERVER_SENT
.equals(annotation.value)).findFirst().isPresent())
.findFirst();
}
private List<Span> findAllEventRelatedSpans() {
return this.integrationTestSpanCollector.hashedSpans.stream()
.filter(span -> "message/messages".equals(span.name) && span.parentId != null).collect(
Collectors.toList());
}
private Optional<Span> findFirstHttpRequestSpan() {
return this.integrationTestSpanCollector.hashedSpans.stream()
.filter(span -> "http/".equals(span.name) && span.parentId != null).findFirst();
}
private void thenAllSpansArePresent(Optional<Span> firstHttpSpan,
List<Span> eventSpans, Optional<Span> lastHttpSpan,
Optional<Span> eventSentSpan, Optional<Span> eventReceivedSpan) {
then(firstHttpSpan.isPresent()).isTrue();
then(eventSpans).isNotEmpty();
then(eventSentSpan.isPresent()).isTrue();
then(eventReceivedSpan.isPresent()).isTrue();
then(lastHttpSpan.isPresent()).isTrue();
}
@Configuration