Added tests

This commit is contained in:
Marcin Grzejszczak
2016-03-10 13:56:28 +01:00
parent 045bfc5317
commit e4bcd13b8c
7 changed files with 325 additions and 54 deletions

View File

@@ -27,7 +27,6 @@ import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.sleuth.Sampler;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.metric.SpanReporterService;
import org.springframework.cloud.sleuth.sampler.PercentageBasedSampler;
import org.springframework.cloud.sleuth.sampler.SamplerProperties;
@@ -37,11 +36,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.integration.config.GlobalChannelInterceptor;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.ChannelInterceptorAdapter;
/**
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration}
@@ -69,30 +64,8 @@ public class SleuthStreamAutoConfiguration {
@Bean
@GlobalChannelInterceptor(patterns = SleuthSource.OUTPUT, order = Ordered.HIGHEST_PRECEDENCE)
public ChannelInterceptor zipkinChannelInterceptor(final SpanReporterService spanReporterService) {
// don't trace the tracer (suppress spans originating from our own source)
return new ChannelInterceptorAdapter() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
return MessageBuilder.fromMessage(message)
.setHeader(Span.NOT_SAMPLED_NAME, "true").build();
}
@Override
public void afterSendCompletion(Message<?> message, MessageChannel channel,
boolean sent, Exception ex) {
if (!(message.getPayload() instanceof Spans)) {
return;
}
Spans spans = (Spans) message.getPayload();
int spanNumber = spans.getSpans().size();
if (sent) {
spanReporterService.incrementAcceptedSpans(spanNumber);
} else {
spanReporterService.incrementDroppedSpans(spanNumber);
}
}
};
public ChannelInterceptor zipkinChannelInterceptor(SpanReporterService spanReporterService) {
return new TracerIgnoringChannelInterceptor(spanReporterService);
}
@Bean

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth.stream;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.metric.SpanReporterService;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.ChannelInterceptorAdapter;
/**
* {@link org.springframework.messaging.support.ChannelInterceptor} that doesn't
* trace the tracer.
*
* @author Marcin Grzejszczak
*/
class TracerIgnoringChannelInterceptor extends ChannelInterceptorAdapter {
private final SpanReporterService spanReporterService;
public TracerIgnoringChannelInterceptor(SpanReporterService spanReporterService) {
this.spanReporterService = spanReporterService;
}
/**
* Don't trace the tracer (suppress spans originating from our own source)
**/
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
return MessageBuilder.fromMessage(message)
.setHeader(Span.NOT_SAMPLED_NAME, "true").build();
}
@Override
public void afterSendCompletion(Message<?> message, MessageChannel channel,
boolean sent, Exception ex) {
if (!(message.getPayload() instanceof Spans)) {
return;
}
Spans spans = (Spans) message.getPayload();
int spanNumber = spans.getSpans().size();
if (sent) {
this.spanReporterService.incrementAcceptedSpans(spanNumber);
} else {
this.spanReporterService.incrementDroppedSpans(spanNumber);
}
}
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth.stream;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.BDDMockito;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.metric.SpanReporterService;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import static org.assertj.core.api.BDDAssertions.then;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* @author Marcin Grzejszczak
*/
@RunWith(MockitoJUnitRunner.class)
public class TracerIgnoringChannelInterceptorTest {
@Mock MessageChannel messageChannel;
@Mock SpanReporterService spanReporterService;
@InjectMocks TracerIgnoringChannelInterceptor tracerIgnoringChannelInterceptor;
@Test
public void should_attach_not_sampled_header_to_the_message() throws Exception {
Message<String> message = MessageBuilder.withPayload("hello").build();
Message interceptedMessage = this.tracerIgnoringChannelInterceptor.preSend(message, this.messageChannel);
then(interceptedMessage.getHeaders().containsKey(
Span.NOT_SAMPLED_NAME)).isTrue();
}
@Test
public void should_ignore_metrics_when_message_payload_does_not_contain_spans() throws Exception {
Message<String> message = MessageBuilder.withPayload("hello").build();
this.tracerIgnoringChannelInterceptor.afterSendCompletion(message, this.messageChannel, true, null);
verifyZeroInteractions(this.spanReporterService);
}
@Test
public void should_increment_accepted_spans_when_message_sending_was_successful() throws Exception {
Span span1 = Span.builder().build();
Span span2 = Span.builder().build();
Message<Spans> message = MessageBuilder.withPayload(new Spans(null,
Arrays.asList(span1, span2))).build();
this.tracerIgnoringChannelInterceptor.afterSendCompletion(message, this.messageChannel, true, null);
BDDMockito.then(this.spanReporterService).should().incrementAcceptedSpans(2);
}
@Test
public void should_increment_dropped_spans_when_message_sending_was_successful() throws Exception {
Span span1 = Span.builder().build();
Span span2 = Span.builder().build();
Message<Spans> message = MessageBuilder.withPayload(new Spans(null,
Arrays.asList(span1, span2))).build();
this.tracerIgnoringChannelInterceptor.afterSendCompletion(message, this.messageChannel, false, null);
BDDMockito.then(this.spanReporterService).should().incrementDroppedSpans(2);
}
}