[#106] Converted UUID to Long

- Changed Random instantiation to a shared Random
- Changed the name of the converter
- Changed generator into random
- Span id is now non-nullable.
    - it gets generated in the http filter if it's not there
    - it's generated in the spring-integration channels if it wasn't set
This commit is contained in:
Marcin Grzejszczak
2016-01-18 14:38:09 +01:00
parent 483737fdc1
commit 82ed9f78a1
68 changed files with 685 additions and 888 deletions

View File

@@ -1,13 +1,9 @@
package org.springframework.cloud.sleuth.zipkin.stream;
import io.zipkin.Sampler;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import javax.sql.DataSource;
import io.zipkin.*;
import io.zipkin.BinaryAnnotation.Type;
import io.zipkin.Span.Builder;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
@@ -16,17 +12,13 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.Cloud;
import org.springframework.cloud.CloudFactory;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Log;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.stream.Host;
import org.springframework.cloud.sleuth.stream.SleuthSink;
import org.springframework.cloud.sleuth.stream.Spans;
import org.springframework.cloud.sleuth.zipkin.stream.ZipkinMessageListener.NotSleuthStreamClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.*;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
@@ -35,20 +27,19 @@ import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.util.StringUtils;
import io.zipkin.Annotation;
import io.zipkin.BinaryAnnotation;
import io.zipkin.BinaryAnnotation.Type;
import io.zipkin.Constants;
import io.zipkin.Endpoint;
import io.zipkin.Span.Builder;
import io.zipkin.SpanStore;
import lombok.extern.apachecommons.CommonsLog;
import javax.sql.DataSource;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
@MessageEndpoint
@CommonsLog
@Conditional(NotSleuthStreamClient.class)
public class ZipkinMessageListener {
private static final String UNKNOWN_PROCESS_ID = "unknown";
@Autowired
SpanStore spanStore;
@@ -80,10 +71,9 @@ public class ZipkinMessageListener {
// A zipkin span without any annotations cannot be queried, add special "lc" to avoid that.
if (span.logs().isEmpty() && span.tags().isEmpty()) {
// TODO: javadocs say this isn't nullable!
String processId = span.getProcessId() != null
? span.getProcessId().toLowerCase()
: "unknown";
: UNKNOWN_PROCESS_ID;
zipkinSpan.addBinaryAnnotation(
BinaryAnnotation.create(Constants.LOCAL_COMPONENT, processId, ep)
);
@@ -94,15 +84,15 @@ public class ZipkinMessageListener {
zipkinSpan.timestamp(span.getBegin() * 1000);
zipkinSpan.duration((span.getEnd() - span.getBegin()) * 1000);
zipkinSpan.traceId(hash(span.getTraceId()));
zipkinSpan.traceId(span.getTraceId());
if (span.getParents().size() > 0) {
if (span.getParents().size() > 1) {
log.error("zipkin doesn't support spans with multiple parents. Omitting "
+ "other parents for " + span);
}
zipkinSpan.parentId(hash(span.getParents().get(0)));
zipkinSpan.parentId(span.getParents().get(0));
}
zipkinSpan.id(hash(span.getSpanId()));
zipkinSpan.id(span.getSpanId());
if (StringUtils.hasText(span.getName())) {
zipkinSpan.name(span.getName());
}
@@ -145,19 +135,6 @@ public class ZipkinMessageListener {
}
}
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++) {
h = 31 * h + string.charAt(i);
}
return h;
}
protected static class NotSleuthStreamClient extends SpringBootCondition {
@Override

View File

@@ -16,17 +16,18 @@
package org.springframework.cloud.sleuth.zipkin.stream;
import io.zipkin.Sampler;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.springframework.cloud.sleuth.MilliSpan;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.stream.Host;
import org.springframework.cloud.sleuth.stream.Spans;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import static org.assertj.core.api.Assertions.assertThat;
public class SamplingZipkinSpanIteratorTests {
@@ -69,7 +70,7 @@ public class SamplingZipkinSpanIteratorTests {
}
Span span(String name) {
String id = UUID.randomUUID().toString();
return new MilliSpan(1, 3, name, id, Collections.<String>emptyList(), id, true, true, "proc");
Long id = new Random().nextLong();
return new MilliSpan(1, 3, name, id, Collections.<Long>emptyList(), id, true, true, "process");
}
}

View File

@@ -19,8 +19,6 @@ package org.springframework.cloud.sleuth.zipkin.stream;
import io.zipkin.BinaryAnnotation;
import io.zipkin.Endpoint;
import java.util.Collections;
import java.util.Comparator;
import java.util.Objects;
import org.junit.Test;
import org.springframework.cloud.sleuth.MilliSpan;
@@ -29,7 +27,7 @@ import org.springframework.cloud.sleuth.stream.Host;
import static org.assertj.core.api.Assertions.assertThat;
public class ZipkinMessageListenerTests {
MilliSpan span = new MilliSpan(1, 3, "name", "traceId", Collections.<String>emptyList(), "spanId", true, true, "processId");
MilliSpan span = new MilliSpan(1, 3, "name", 1L, Collections.<Long>emptyList(), 2L, true, true, "process");
Host host = new Host("myservice", "1.2.3.4", 8080);
Endpoint endpoint = Endpoint.create("myservice", 1 << 24 | 2 << 16 | 3 << 8 | 4, 8080);
@@ -75,13 +73,13 @@ public class ZipkinMessageListenerTests {
assertThat(result.binaryAnnotations).hasSize(1);
assertThat(result.binaryAnnotations.get(0)).isEqualToComparingFieldByField(
BinaryAnnotation.create("lc", span.getProcessId().toLowerCase(), endpoint));
BinaryAnnotation.create("lc", span.getProcessId(), endpoint));
}
// TODO: "unknown" bc process id, documented as not nullable, is null in some tests.
@Test
public void nullProcessIdCoercesToUnknownServiceName() {
MilliSpan noProcessId = MilliSpan.builder().traceId("xxxx").name("parent").remote(true).build();
MilliSpan noProcessId = MilliSpan.builder().traceId(1L).name("parent").remote(true).build();
io.zipkin.Span result = ZipkinMessageListener.convert(noProcessId, host);