[#191] Added local component if a span doesn't have any Zipkin constants
fixes #191
This commit is contained in:
@@ -15,7 +15,9 @@
|
||||
*/
|
||||
package org.springframework.cloud.sleuth.zipkin.stream;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -38,6 +40,14 @@ import zipkin.Span.Builder;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
final class SamplingZipkinSpanIterator implements Iterator<zipkin.Span> {
|
||||
private static final List<String> ZIPKIN_ANNOTATIONS = Arrays.asList(
|
||||
Constants.CLIENT_ADDR, Constants.CLIENT_RECV, Constants.CLIENT_SEND,
|
||||
Constants.CLIENT_RECV_FRAGMENT, Constants.CLIENT_SEND_FRAGMENT,
|
||||
Constants.SERVER_ADDR, Constants.SERVER_RECV, Constants.SERVER_SEND,
|
||||
Constants.SERVER_RECV_FRAGMENT, Constants.SERVER_SEND_FRAGMENT,
|
||||
Constants.LOCAL_COMPONENT,
|
||||
Constants.WIRE_RECV, Constants.WIRE_SEND
|
||||
);
|
||||
|
||||
private static final Log log = org.apache.commons.logging.LogFactory
|
||||
.getLog(SamplingZipkinSpanIterator.class);
|
||||
@@ -110,17 +120,15 @@ final class SamplingZipkinSpanIterator implements Iterator<zipkin.Span> {
|
||||
// A zipkin span without any annotations cannot be queried, add special "lc" to
|
||||
// avoid that.
|
||||
if (span.logs().isEmpty() && span.tags().isEmpty()) {
|
||||
String processId = span.getProcessId() != null
|
||||
? span.getProcessId().toLowerCase()
|
||||
: ZipkinMessageListener.UNKNOWN_PROCESS_ID;
|
||||
zipkinSpan.addBinaryAnnotation(
|
||||
BinaryAnnotation.create(Constants.LOCAL_COMPONENT, processId, ep));
|
||||
addLocalComponentAnnotation(span, zipkinSpan, ep);
|
||||
}
|
||||
else {
|
||||
ZipkinMessageListener.addZipkinAnnotations(zipkinSpan, span, ep);
|
||||
ZipkinMessageListener.addZipkinBinaryAnnotations(zipkinSpan, span, ep);
|
||||
}
|
||||
|
||||
if (!spanContainsAnyZipkinConstant(span)) {
|
||||
addLocalComponentAnnotation(span, zipkinSpan, ep);
|
||||
}
|
||||
zipkinSpan.timestamp(span.getBegin() * 1000);
|
||||
zipkinSpan.duration(span.getAccumulatedMillis() * 1000);
|
||||
zipkinSpan.traceId(span.getTraceId());
|
||||
@@ -138,4 +146,22 @@ final class SamplingZipkinSpanIterator implements Iterator<zipkin.Span> {
|
||||
}
|
||||
return zipkinSpan.build();
|
||||
}
|
||||
|
||||
private static void addLocalComponentAnnotation(Span span, Builder zipkinSpan,
|
||||
Endpoint ep) {
|
||||
String processId = span.getProcessId() != null
|
||||
? span.getProcessId().toLowerCase()
|
||||
: ZipkinMessageListener.UNKNOWN_PROCESS_ID;
|
||||
zipkinSpan.addBinaryAnnotation(
|
||||
BinaryAnnotation.create(Constants.LOCAL_COMPONENT, processId, ep));
|
||||
}
|
||||
|
||||
private static boolean spanContainsAnyZipkinConstant(Span span) {
|
||||
for (org.springframework.cloud.sleuth.Log log : span.logs()) {
|
||||
if (ZIPKIN_ANNOTATIONS.contains(log.getEvent())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,8 @@
|
||||
package org.springframework.cloud.sleuth.zipkin;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.cloud.sleuth.Log;
|
||||
@@ -44,6 +46,14 @@ import zipkin.Endpoint;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ZipkinSpanListener {
|
||||
private static final List<String> ZIPKIN_ANNOTATIONS = Arrays.asList(
|
||||
Constants.CLIENT_ADDR, Constants.CLIENT_RECV, Constants.CLIENT_SEND,
|
||||
Constants.CLIENT_RECV_FRAGMENT, Constants.CLIENT_SEND_FRAGMENT,
|
||||
Constants.SERVER_ADDR, Constants.SERVER_RECV, Constants.SERVER_SEND,
|
||||
Constants.SERVER_RECV_FRAGMENT, Constants.SERVER_SEND_FRAGMENT,
|
||||
Constants.LOCAL_COMPONENT,
|
||||
Constants.WIRE_RECV, Constants.WIRE_SEND
|
||||
);
|
||||
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
|
||||
.getLog(ZipkinSpanListener.class);
|
||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
@@ -129,20 +139,14 @@ public class ZipkinSpanListener {
|
||||
|
||||
// A zipkin span without any annotations cannot be queried, add special "lc" to avoid that.
|
||||
if (span.logs().isEmpty() && span.tags().isEmpty()) {
|
||||
byte[] processId = span.getProcessId() != null
|
||||
? span.getProcessId().toLowerCase().getBytes(UTF_8)
|
||||
: UNKNOWN_BYTES;
|
||||
BinaryAnnotation component = new BinaryAnnotation.Builder()
|
||||
.type(BinaryAnnotation.Type.STRING)
|
||||
.key("lc") // LOCAL_COMPONENT
|
||||
.value(processId)
|
||||
.endpoint(this.localEndpoint).build();
|
||||
zipkinSpan.addBinaryAnnotation(component);
|
||||
addLocalComponentAnnotation(span, zipkinSpan);
|
||||
} else {
|
||||
addZipkinAnnotations(zipkinSpan, span, this.localEndpoint);
|
||||
addZipkinBinaryAnnotations(zipkinSpan, span, this.localEndpoint);
|
||||
}
|
||||
|
||||
if (!spanContainsAnyZipkinConstant(span)) {
|
||||
addLocalComponentAnnotation(span, zipkinSpan);
|
||||
}
|
||||
zipkinSpan.timestamp(span.getBegin() * 1000L);
|
||||
zipkinSpan.duration(span.getAccumulatedMillis() * 1000L);
|
||||
zipkinSpan.traceId(span.getTraceId());
|
||||
@@ -160,6 +164,27 @@ public class ZipkinSpanListener {
|
||||
return zipkinSpan.build();
|
||||
}
|
||||
|
||||
private void addLocalComponentAnnotation(Span span, zipkin.Span.Builder zipkinSpan) {
|
||||
byte[] processId = span.getProcessId() != null
|
||||
? span.getProcessId().toLowerCase().getBytes(UTF_8)
|
||||
: UNKNOWN_BYTES;
|
||||
BinaryAnnotation component = new BinaryAnnotation.Builder()
|
||||
.type(BinaryAnnotation.Type.STRING)
|
||||
.key("lc") // LOCAL_COMPONENT
|
||||
.value(processId)
|
||||
.endpoint(this.localEndpoint).build();
|
||||
zipkinSpan.addBinaryAnnotation(component);
|
||||
}
|
||||
|
||||
private boolean spanContainsAnyZipkinConstant(Span span) {
|
||||
for (Log log : span.logs()) {
|
||||
if (ZIPKIN_ANNOTATIONS.contains(log.getEvent())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add annotations from the sleuth Span.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user