Add basic test for zipkin span listener

This commit is contained in:
Dave Syer
2015-09-21 11:58:53 +01:00
parent c786f81086
commit 3b305e6f8f
4 changed files with 135 additions and 7 deletions

View File

@@ -32,14 +32,16 @@ import com.github.kristofa.brave.zipkin.ZipkinSpanCollector;
@Configuration
@EnableConfigurationProperties
@ConditionalOnClass(ZipkinSpanCollector.class)
@ConditionalOnProperty(value = "spring.sleuth.zipkin.enabled", matchIfMissing = true)
@ConditionalOnProperty(value = "spring.zipkin.enabled", matchIfMissing = true)
public class ZipkinAutoConfiguration {
@Bean
@ConditionalOnMissingBean(SpanCollector.class)
public ZipkinSpanCollector spanCollector() {
return new ZipkinSpanCollector(zipkinProperties().getHost(), zipkinProperties()
.getPort());
ZipkinProperties zipkin = zipkinProperties();
ZipkinSpanCollector collector = new ZipkinSpanCollector(zipkin.getHost(),
zipkin.getPort(), zipkin.getCollector());
return collector;
}
@Bean
@@ -48,7 +50,8 @@ public class ZipkinAutoConfiguration {
}
@Bean
// @ConditionalOnProperty(value = "spring.sleuth.zipkin.braveTracer.enabled", havingValue = "false")
// @ConditionalOnProperty(value = "spring.sleuth.zipkin.braveTracer.enabled",
// havingValue = "false")
public ZipkinSpanListener sleuthTracer(SpanCollector spanCollector) {
return new ZipkinSpanListener(spanCollector);
}

View File

@@ -16,10 +16,12 @@
package org.springframework.cloud.sleuth.zipkin;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import com.github.kristofa.brave.zipkin.ZipkinSpanCollectorParams;
import lombok.Data;
/**
* @author Spencer Gibb
*/
@@ -30,4 +32,5 @@ public class ZipkinProperties {
private int fixedSampleRate = 1;
private String host = "localhost";
private int port = 9410;
private ZipkinSpanCollectorParams collector = new ZipkinSpanCollectorParams();
}

View File

@@ -155,7 +155,7 @@ public class ZipkinSpanListener {
public int getAddress() {
String address;
if (this.serverProperties.getAddress() != null) {
if (this.serverProperties!=null && this.serverProperties.getAddress() != null) {
address = this.serverProperties.getAddress().getHostAddress();
}
else {
@@ -249,6 +249,9 @@ public class ZipkinSpanListener {
private static long hash(String string) {
long h = 1125899906842597L;
if (string==null) {
return h;
}
int len = string.length();
for (int i = 0; i < len; i++) {

View File

@@ -0,0 +1,119 @@
/*
* Copyright 2015 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.zipkin;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.sleuth.MilliSpan;
import org.springframework.cloud.sleuth.Sampler;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Trace;
import org.springframework.cloud.sleuth.TraceScope;
import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration;
import org.springframework.cloud.sleuth.event.ClientReceivedEvent;
import org.springframework.cloud.sleuth.event.ClientSentEvent;
import org.springframework.cloud.sleuth.event.ServerReceivedEvent;
import org.springframework.cloud.sleuth.event.ServerSentEvent;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.cloud.sleuth.zipkin.ZipkinSpanListenerTests.TestConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.github.kristofa.brave.EmptySpanCollector;
import com.github.kristofa.brave.SpanCollector;
/**
* @author Dave Syer
*
*/
@SpringApplicationConfiguration(classes = TestConfiguration.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class ZipkinSpanListenerTests {
@Autowired
private Trace trace;
@Autowired
private ApplicationContext application;
@Autowired
private ZipkinTestConfiguration test;
@PostConstruct
public void init() {
this.test.spans.clear();
}
@Test
public void acquireAndRelease() {
TraceScope context = this.trace.startSpan("foo");
context.close();
assertEquals(1, this.test.spans.size());
}
@Test
public void rpcAnnotations() {
Span parent = MilliSpan.builder().traceId("xxxx").name("parent").remote(true).build();
TraceScope context = this.trace.startSpan("child", parent);
this.application.publishEvent(new ClientSentEvent(this, context.getSpan()));
this.application.publishEvent(new ServerReceivedEvent(this, parent, context.getSpan()));
this.application.publishEvent(new ServerSentEvent(this, parent, context.getSpan()));
this.application.publishEvent(new ClientReceivedEvent(this, context.getSpan()));
context.close();
assertEquals(2, this.test.spans.size());
}
@Configuration
@Import({ ZipkinTestConfiguration.class, ZipkinAutoConfiguration.class, TraceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class })
protected static class TestConfiguration {
}
@Configuration
protected static class ZipkinTestConfiguration {
private List<com.twitter.zipkin.gen.Span> spans = new ArrayList<>();
@Bean
public Sampler<?> defaultSampler() {
return new AlwaysSampler();
}
@Bean
public SpanCollector collector() {
return new EmptySpanCollector() {
@Override
public void collect(com.twitter.zipkin.gen.Span span) {
ZipkinTestConfiguration.this.spans.add(span);
}
};
}
}}