Merge pull request #208 from spring-cloud/issues_#205_deferring_locating_endpoint

[#205] Deferring endpoint localization
This commit is contained in:
Marcin Grzejszczak
2016-03-09 15:34:00 +01:00
5 changed files with 21 additions and 12 deletions

View File

@@ -75,7 +75,7 @@ public class TraceFilter extends OncePerRequestFilter
+ ".TRACE";
public static final Pattern DEFAULT_SKIP_PATTERN = Pattern.compile(
"/api-docs.*|/autoconfig|/configprops|/dump|/info|/metrics.*|/mappings|/trace|/swagger.*|.*\\.png|.*\\.css|.*\\.js|.*\\.html|/favicon.ico|/hystrix.stream");
"/api-docs.*|/autoconfig|/configprops|/dump|/health|/info|/metrics.*|/mappings|/trace|/swagger.*|.*\\.png|.*\\.css|.*\\.js|.*\\.html|/favicon.ico|/hystrix.stream");
private final Tracer tracer;
private final TraceKeys traceKeys;

View File

@@ -1,5 +1,7 @@
package org.springframework.cloud.sleuth.zipkin;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import zipkin.Endpoint;
@@ -12,6 +14,8 @@ import zipkin.Endpoint;
*/
public class FallbackHavingEndpointLocator implements EndpointLocator {
private final AtomicReference<Endpoint> cachedEndpoint = new AtomicReference<>();
private static final Log log = LogFactory.getLog(FallbackHavingEndpointLocator.class);
private final DiscoveryClientEndpointLocator discoveryClientEndpointLocator;
@@ -25,6 +29,11 @@ public class FallbackHavingEndpointLocator implements EndpointLocator {
@Override
public Endpoint local() {
this.cachedEndpoint.compareAndSet(null, endpoint());
return this.cachedEndpoint.get();
}
private Endpoint endpoint() {
if (this.discoveryClientEndpointLocator == null) {
return this.serverPropertiesEndpointLocator.local();
}

View File

@@ -65,7 +65,7 @@ public class ZipkinAutoConfiguration {
@Bean
public ZipkinSpanListener sleuthTracer(ZipkinSpanReporter reporter, EndpointLocator endpointLocator) {
return new ZipkinSpanListener(reporter, endpointLocator.local());
return new ZipkinSpanListener(reporter, endpointLocator);
}
@Configuration

View File

@@ -61,11 +61,11 @@ public class ZipkinSpanListener {
* the service name from discovery.
*/
// Visible for testing
Endpoint localEndpoint;
EndpointLocator endpointLocator;
public ZipkinSpanListener(ZipkinSpanReporter reporter, Endpoint localEndpoint) {
public ZipkinSpanListener(ZipkinSpanReporter reporter, EndpointLocator endpointLocator) {
this.reporter = reporter;
this.localEndpoint = localEndpoint;
this.endpointLocator = endpointLocator;
}
@EventListener
@@ -141,8 +141,8 @@ public class ZipkinSpanListener {
if (notClientOrServer(span)) {
ensureLocalComponent(span, zipkinSpan);
}
addZipkinAnnotations(zipkinSpan, span, this.localEndpoint);
addZipkinBinaryAnnotations(zipkinSpan, span, this.localEndpoint);
addZipkinAnnotations(zipkinSpan, span, this.endpointLocator.local());
addZipkinBinaryAnnotations(zipkinSpan, span, this.endpointLocator.local());
if (hasClientSend(span)) {
ensureServerAddr(span, zipkinSpan);
}
@@ -174,15 +174,15 @@ public class ZipkinSpanListener {
.type(BinaryAnnotation.Type.STRING)
.key("lc") // LOCAL_COMPONENT
.value(processId)
.endpoint(this.localEndpoint).build();
.endpoint(this.endpointLocator.local()).build();
zipkinSpan.addBinaryAnnotation(component);
}
private void ensureServerAddr(Span span, zipkin.Span.Builder zipkinSpan) {
String serviceName = span.tags().containsKey(Span.SPAN_PEER_SERVICE_TAG_NAME) ?
span.tags().get(Span.SPAN_PEER_SERVICE_TAG_NAME) : this.localEndpoint.serviceName;
span.tags().get(Span.SPAN_PEER_SERVICE_TAG_NAME) : this.endpointLocator.local().serviceName;
zipkinSpan.addBinaryAnnotation(BinaryAnnotation.address(Constants.SERVER_ADDR,
Endpoint.create(serviceName, this.localEndpoint.ipv4, this.localEndpoint.port)));
Endpoint.create(serviceName, this.endpointLocator.local().ipv4, this.endpointLocator.local().port)));
}
private boolean notClientOrServer(Span span) {

View File

@@ -101,7 +101,7 @@ public class ZipkinSpanListenerTests {
zipkin.Span result = this.listener.convert(this.parent);
assertThat(result.annotations.get(0).endpoint)
.isEqualTo(this.listener.localEndpoint);
.isEqualTo(this.listener.endpointLocator.local());
assertThat(result.binaryAnnotations.get(0).endpoint)
.isEqualTo(result.annotations.get(0).endpoint);
}
@@ -109,7 +109,7 @@ public class ZipkinSpanListenerTests {
/** zipkin's Endpoint.serviceName should never be null. */
@Test
public void localEndpointIncludesServiceName() {
assertThat(this.listener.localEndpoint.serviceName)
assertThat(this.listener.endpointLocator.local().serviceName)
.isNotEmpty();
}