WIP
This commit is contained in:
@@ -17,7 +17,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
|
||||
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.cloud.sleuth.Tracer;
|
||||
|
||||
@@ -8,7 +8,7 @@ import java.util.Date;
|
||||
import org.junit.After;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
|
||||
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
|
||||
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
||||
import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.sleuth.zipkin;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -24,9 +25,11 @@ import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.cloud.sleuth.Sampler;
|
||||
@@ -64,6 +67,7 @@ import org.springframework.web.client.RestTemplate;
|
||||
public class ZipkinAutoConfiguration {
|
||||
|
||||
@Autowired(required = false) List<SpanAdjuster> spanAdjusters = new ArrayList<>();
|
||||
@Autowired ZipkinUrlExtractor extractor;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@@ -71,10 +75,51 @@ public class ZipkinAutoConfiguration {
|
||||
ZipkinRestTemplateCustomizer zipkinRestTemplateCustomizer) {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
zipkinRestTemplateCustomizer.customize(restTemplate);
|
||||
return new HttpZipkinSpanReporter(restTemplate, zipkin.getBaseUrl(), zipkin.getFlushInterval(),
|
||||
String zipkinUrl = this.extractor.zipkinUrl(zipkin);
|
||||
return new HttpZipkinSpanReporter(restTemplate, zipkinUrl, zipkin.getFlushInterval(),
|
||||
spanMetricReporter);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(DiscoveryClient.class)
|
||||
static class DiscoveryClientZipkinUrlExtractorConfiguration {
|
||||
|
||||
@Autowired(required = false) DiscoveryClient discoveryClient;
|
||||
|
||||
@Bean
|
||||
ZipkinUrlExtractor zipkinUrlExtractor() {
|
||||
final DiscoveryClient discoveryClient = this.discoveryClient;
|
||||
return new ZipkinUrlExtractor() {
|
||||
@Override
|
||||
public String zipkinUrl(ZipkinProperties zipkinProperties) {
|
||||
if (discoveryClient != null) {
|
||||
URI uri = URI.create(zipkinProperties.getBaseUrl());
|
||||
String host = uri.getHost();
|
||||
List<ServiceInstance> instances = discoveryClient.getInstances(host);
|
||||
if (!instances.isEmpty()) {
|
||||
return instances.get(0).getUri().toString();
|
||||
}
|
||||
}
|
||||
return zipkinProperties.getBaseUrl();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingClass("org.springframework.cloud.client.discovery.DiscoveryClient")
|
||||
static class DefaultZipkinUrlExtractorConfiguration {
|
||||
@Bean
|
||||
ZipkinUrlExtractor zipkinUrlExtractor() {
|
||||
return new ZipkinUrlExtractor() {
|
||||
@Override
|
||||
public String zipkinUrl(ZipkinProperties zipkinProperties) {
|
||||
return zipkinProperties.getBaseUrl();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public ZipkinRestTemplateCustomizer zipkinRestTemplateCustomizer(ZipkinProperties zipkinProperties) {
|
||||
@@ -156,3 +201,7 @@ public class ZipkinAutoConfiguration {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface ZipkinUrlExtractor {
|
||||
String zipkinUrl(ZipkinProperties zipkinProperties);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.springframework.cloud.sleuth.zipkin;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
class ZipkinDiscoveryClientTests {
|
||||
}
|
||||
@@ -17,11 +17,16 @@
|
||||
package org.springframework.cloud.sleuth.zipkin;
|
||||
|
||||
import org.assertj.core.api.Condition;
|
||||
import org.junit.Ignore;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import zipkin.Constants;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -56,16 +61,17 @@ import static org.junit.Assert.assertEquals;
|
||||
public class ZipkinSpanListenerTests {
|
||||
|
||||
@Autowired Tracer tracer;
|
||||
@Autowired ApplicationContext application;
|
||||
@Autowired TestConfiguration test;
|
||||
@Autowired ZipkinSpanListener spanListener;
|
||||
@Autowired ZipkinSpanReporter spanReporter;
|
||||
@Autowired MockEnvironment mockEnvironment;
|
||||
@Autowired EndpointLocator endpointLocator;
|
||||
@Autowired ZipkinDiscoveryClient discoveryClient;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
this.test.zipkinSpans.clear();
|
||||
this.discoveryClient.serviceCalled = false;
|
||||
}
|
||||
|
||||
Span parent = Span.builder().traceId(1L).name("http:parent").remote(true).build();
|
||||
@@ -276,6 +282,17 @@ public class ZipkinSpanListenerTests {
|
||||
assertThat(this.test.zipkinSpans).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void shouldUseDiscoveryClientToFindZipkinUrlIfPresent() {
|
||||
Span span = Span.builder().traceIdHigh(1L).traceId(2L).spanId(3L).name("foo").build();
|
||||
|
||||
this.spanListener.report(span);
|
||||
|
||||
assertThat(this.test.zipkinSpans).isNotEmpty();
|
||||
assertThat(this.discoveryClient.serviceCalled).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldAddClientServiceIdTagWhenSpanContainsRpcEvent() {
|
||||
this.parent.logEvent(Span.CLIENT_SEND);
|
||||
@@ -335,6 +352,71 @@ public class ZipkinSpanListenerTests {
|
||||
return new MockEnvironment();
|
||||
}
|
||||
|
||||
@Bean
|
||||
DiscoveryClient client() {
|
||||
return new ZipkinDiscoveryClient();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ZipkinDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
boolean serviceCalled = false;
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServiceInstance getLocalServiceInstance() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ServiceInstance> getInstances(String s) {
|
||||
if ("zipkin".equals(s)) {
|
||||
this.serviceCalled = true;
|
||||
return Collections.singletonList(new ServiceInstance() {
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return "zipkin";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHost() {
|
||||
return "1.2.3.4";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return 1234;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSecure() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getUri() {
|
||||
return URI.create("http://1.2.3.4:1234");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getMetadata() {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getServices() {
|
||||
return Collections.singletonList("zipkin");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user