From 6c2c87ab3c4679be437b998bb5d1844b8a62f244 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 1 Mar 2016 13:45:58 +0100 Subject: [PATCH] [#191] Added local component if a span doesn't have any Zipkin constants fixes #191 --- .../stream/SamplingZipkinSpanIterator.java | 38 +++++++++++++--- .../sleuth/zipkin/ZipkinSpanListener.java | 45 ++++++++++++++----- 2 files changed, 67 insertions(+), 16 deletions(-) diff --git a/spring-cloud-sleuth-zipkin-stream/src/main/java/org/springframework/cloud/sleuth/zipkin/stream/SamplingZipkinSpanIterator.java b/spring-cloud-sleuth-zipkin-stream/src/main/java/org/springframework/cloud/sleuth/zipkin/stream/SamplingZipkinSpanIterator.java index 7e6ff577a..c128aec54 100644 --- a/spring-cloud-sleuth-zipkin-stream/src/main/java/org/springframework/cloud/sleuth/zipkin/stream/SamplingZipkinSpanIterator.java +++ b/spring-cloud-sleuth-zipkin-stream/src/main/java/org/springframework/cloud/sleuth/zipkin/stream/SamplingZipkinSpanIterator.java @@ -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 { + private static final List 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 { // 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 { } 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; + } } \ No newline at end of file diff --git a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinSpanListener.java b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinSpanListener.java index 9b7aeef00..52c6740fa 100644 --- a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinSpanListener.java +++ b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinSpanListener.java @@ -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 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. */