[#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

@@ -32,11 +32,12 @@ public class SampleBackground {
@Autowired
private TraceManager traceManager;
@Autowired
private Random random;
@SneakyThrows
@Async
public void background() {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
this.traceManager.addAnnotation("background-sleep-millis", String.valueOf(millis));

View File

@@ -27,11 +27,11 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.JdkIdGenerator;
import sample.SampleMessagingApplication;
import tools.AbstractIntegrationTest;
import java.util.Collection;
import java.util.Random;
import static org.assertj.core.api.BDDAssertions.then;
@@ -52,7 +52,7 @@ public class MessagingApplicationTests extends AbstractIntegrationTest {
@Test
public void should_propagate_spans_for_messaging() {
String traceId = new JdkIdGenerator().generateId().toString();
long traceId = new Random().nextLong();
await().until(httpMessageWithTraceIdInHeadersIsSuccessfullySent(sampleAppUrl + "/", traceId));
@@ -63,7 +63,7 @@ public class MessagingApplicationTests extends AbstractIntegrationTest {
@Test
public void should_propagate_spans_for_messaging_with_async() {
String traceId = new JdkIdGenerator().generateId().toString();
long traceId = new Random().nextLong();
await().until(httpMessageWithTraceIdInHeadersIsSuccessfullySent(sampleAppUrl + "/xform", traceId));
@@ -80,8 +80,8 @@ public class MessagingApplicationTests extends AbstractIntegrationTest {
.anyMatch(b -> b.key.equals(binaryAnnotationKey))).isTrue();
}
private void thenAllSpansHaveTraceIdEqualTo(String traceId) {
then(integrationTestSpanCollector.hashedSpans.stream().allMatch(span -> span.traceId == zipkinHashedTraceId(traceId))).isTrue();
private void thenAllSpansHaveTraceIdEqualTo(long traceId) {
then(this.integrationTestSpanCollector.hashedSpans.stream().allMatch(span -> span.traceId == traceId)).isTrue();
}
@Configuration

View File

@@ -16,15 +16,14 @@
package sample;
import java.util.Random;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Random;
/**
* @author Spencer Gibb
* @author Dave Syer
@@ -34,11 +33,12 @@ public class SampleController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private Random random;
@SneakyThrows
@RequestMapping("/")
public String hi() {
final Random random = new Random();
Thread.sleep(random.nextInt(1000));
String s = this.restTemplate.getForObject("http://zipkin/hi2", String.class);
return "hi/" + s;

View File

@@ -16,9 +16,8 @@
package sample;
import java.util.Random;
import java.util.concurrent.Callable;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.cloud.sleuth.Span;
@@ -31,8 +30,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.util.Random;
import java.util.concurrent.Callable;
/**
* @author Spencer Gibb
@@ -49,12 +48,13 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
private TraceAccessor accessor;
@Autowired
private SampleBackground controller;
@Autowired
private Random random;
private int port;
@SneakyThrows
@RequestMapping("/")
public String hi() {
final Random random = new Random();
Thread.sleep(random.nextInt(1000));
String s = this.restTemplate.getForObject("http://localhost:" + this.port
@@ -67,7 +67,6 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
return new Callable<String>() {
@Override
public String call() throws Exception {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
SampleController.this.traceManager.addAnnotation("callable-sleep-millis", String.valueOf(millis));
@@ -86,7 +85,6 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
@SneakyThrows
@RequestMapping("/hi2")
public String hi2() {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
this.traceManager.addAnnotation("random-sleep-millis", String.valueOf(millis));
@@ -98,7 +96,6 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
public String traced() {
Trace trace = this.traceManager.startSpan("customTraceEndpoint",
new AlwaysSampler(), null);
final Random random = new Random();
int millis = random.nextInt(1000);
log.info("Sleeping for {} millis", millis);
Thread.sleep(millis);
@@ -113,7 +110,6 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
@SneakyThrows
@RequestMapping("/start")
public String start() {
final Random random = new Random();
int millis = random.nextInt(1000);
log.info("Sleeping for {} millis", millis);
Thread.sleep(millis);

View File

@@ -15,30 +15,20 @@
*/
package tools;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.BDDAssertions.then;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import com.jayway.awaitility.Awaitility;
import com.jayway.awaitility.core.ConditionFactory;
import io.zipkin.Codec;
import io.zipkin.Span;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.BDDAssertions.then;
/**
* @author Marcin Grzejszczak
@@ -47,31 +37,13 @@ import lombok.extern.slf4j.Slf4j;
public abstract class AbstractIntegrationTest {
protected static int pollInterval = 1;
protected static int timeout = 120;
protected static int timeout = 20;
protected RestTemplate restTemplate = new AssertingRestTemplate();
public static ConditionFactory await() {
return Awaitility.await().pollInterval(pollInterval, SECONDS).atMost(timeout, SECONDS);
}
protected long zipkinHashedTraceId(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 String zipkinHashedHexStringTraceId(String traceId) {
long hashedTraceId = zipkinHashedTraceId(traceId);
return Long.toHexString(hashedTraceId);
}
protected Runnable zipkinQueryServerIsUp() {
return checkServerHealth("Zipkin Query Server", this::endpointToCheckZipkinQueryHealth);
}
@@ -109,10 +81,9 @@ public abstract class AbstractIntegrationTest {
return 9411;
}
protected ResponseEntity<String> checkStateOfTheTraceId(String traceId) {
String hexTraceId = zipkinHashedHexStringTraceId(traceId);
URI uri = URI.create(getZipkinTraceQueryUrl() + hexTraceId);
log.info("Sending request to the Zipkin query service [{}]. Checking presence of trace id [{}] and its hex version [{}]", uri, traceId, hexTraceId);
protected ResponseEntity<String> checkStateOfTheTraceId(long traceId) {
URI uri = URI.create(getZipkinTraceQueryUrl() + Long.toHexString(traceId));
log.info("Sending request to the Zipkin query service [{}]. Checking presence of trace id [{}]", uri, traceId);
return exchangeRequest(uri);
}
@@ -130,11 +101,11 @@ public abstract class AbstractIntegrationTest {
return "http://localhost:"+getZipkinServerPort()+"/api/v1/services";
}
protected Runnable httpMessageWithTraceIdInHeadersIsSuccessfullySent(String endpoint, String traceId) {
protected Runnable httpMessageWithTraceIdInHeadersIsSuccessfullySent(String endpoint, long traceId) {
return new RequestSendingRunnable(this.restTemplate, endpoint, traceId);
}
protected Runnable allSpansWereRegisteredInZipkinWithTraceIdEqualTo(String traceId) {
protected Runnable allSpansWereRegisteredInZipkinWithTraceIdEqualTo(long traceId) {
return () -> {
ResponseEntity<String> response = checkStateOfTheTraceId(traceId);
log.info("Response from the Zipkin query service about the trace id [{}] for trace with id [{}]", response, traceId);

View File

@@ -16,6 +16,7 @@
package tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Trace;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
@@ -34,9 +35,9 @@ import static org.assertj.core.api.BDDAssertions.then;
public class RequestSendingRunnable implements Runnable {
private final RestTemplate restTemplate;
private final String url;
private final String traceId;
private final long traceId;
public RequestSendingRunnable(RestTemplate restTemplate, String url, String traceId) {
public RequestSendingRunnable(RestTemplate restTemplate, String url, long traceId) {
this.restTemplate = restTemplate;
this.url = url;
this.traceId = traceId;
@@ -50,9 +51,9 @@ public class RequestSendingRunnable implements Runnable {
log.info("Received the following response [{}]", responseEntity);
}
private RequestEntity requestWithTraceId(String traceId) {
private RequestEntity requestWithTraceId(long traceId) {
HttpHeaders headers = new HttpHeaders();
headers.add(Trace.TRACE_ID_NAME, traceId);
headers.add(Trace.TRACE_ID_NAME, Span.IdConverter.toHex(traceId));
URI uri = URI.create(url);
RequestEntity requestEntity = new RequestEntity<>(headers, HttpMethod.GET, uri);
log.info("Request [" + requestEntity + "] is ready");

View File

@@ -15,8 +15,8 @@
*/
package integration;
import java.util.Arrays;
import example.ZipkinStreamServerApplication;
import lombok.SneakyThrows;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -34,12 +34,11 @@ import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageChannel;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.JdkIdGenerator;
import example.ZipkinStreamServerApplication;
import lombok.SneakyThrows;
import tools.AbstractIntegrationTest;
import java.util.Collections;
import java.util.Random;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { TestSupportBinderAutoConfiguration.class,
ZipkinStreamServerApplication.class })
@@ -60,13 +59,13 @@ public class ZipkinStreamTests extends AbstractIntegrationTest {
await().until(zipkinServerIsUp());
String traceId = new JdkIdGenerator().generateId().toString();
long traceId = new Random().nextLong();
Span span = MilliSpan.builder().traceId(traceId).spanId(traceId).name("test")
.build();
span.tag(getRequiredBinaryAnnotationName(), "10131");
this.input.send(MessageBuilder.withPayload(
new Spans(new Host(getAppName(), "127.0.0.1", 8080), Arrays.asList(span)))
new Spans(new Host(getAppName(), "127.0.0.1", 8080), Collections.singletonList(span)))
.build());
await().until(allSpansWereRegisteredInZipkinWithTraceIdEqualTo(traceId));

View File

@@ -16,15 +16,14 @@
package sample;
import java.util.Random;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.TraceManager;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.Random;
/**
* @author Spencer Gibb
*/
@@ -33,11 +32,12 @@ public class SampleBackground {
@Autowired
private TraceManager traceManager;
@Autowired
private Random random;
@SneakyThrows
@Async
public void background() {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
this.traceManager.addAnnotation("background-sleep-millis", String.valueOf(millis));

View File

@@ -16,9 +16,8 @@
package sample;
import java.util.Random;
import java.util.concurrent.Callable;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.cloud.sleuth.Span;
@@ -31,8 +30,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.util.Random;
import java.util.concurrent.Callable;
/**
* @author Spencer Gibb
@@ -49,12 +48,13 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
private TraceAccessor accessor;
@Autowired
private SampleBackground controller;
@Autowired
private Random random;
private int port;
@SneakyThrows
@RequestMapping("/")
public String hi() {
final Random random = new Random();
Thread.sleep(random.nextInt(1000));
String s = this.restTemplate.getForObject("http://localhost:" + this.port
@@ -67,7 +67,6 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
return new Callable<String>() {
@Override
public String call() throws Exception {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
SampleController.this.traceManager.addAnnotation("callable-sleep-millis", String.valueOf(millis));
@@ -86,7 +85,6 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
@SneakyThrows
@RequestMapping("/hi2")
public String hi2() {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
this.traceManager.addAnnotation("random-sleep-millis", String.valueOf(millis));
@@ -98,7 +96,6 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
public String traced() {
Trace trace = this.traceManager.startSpan("customTraceEndpoint",
new AlwaysSampler(), null);
final Random random = new Random();
int millis = random.nextInt(1000);
log.info("Sleeping for {} millis", millis);
Thread.sleep(millis);
@@ -113,7 +110,6 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
@SneakyThrows
@RequestMapping("/start")
public String start() {
final Random random = new Random();
int millis = random.nextInt(1000);
log.info("Sleeping for {} millis", millis);
Thread.sleep(millis);

View File

@@ -31,10 +31,11 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.JdkIdGenerator;
import sample.SampleZipkinApplication;
import tools.AbstractIntegrationTest;
import java.util.Random;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { WaitUntilZipkinIsUpConfig.class,
SampleZipkinApplication.class })
@@ -53,9 +54,8 @@ public class ZipkinTests extends AbstractIntegrationTest {
}
@Test
@SneakyThrows
public void should_propagate_spans_to_zipkin() {
String traceId = new JdkIdGenerator().generateId().toString();
long traceId = new Random().nextLong();
await().until(httpMessageWithTraceIdInHeadersIsSuccessfullySent(
sampleAppUrl + "/hi2", traceId));

View File

@@ -16,15 +16,14 @@
package sample;
import java.util.Random;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.TraceManager;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.Random;
/**
* @author Spencer Gibb
*/
@@ -33,11 +32,12 @@ public class SampleBackground {
@Autowired
private TraceManager traceManager;
@Autowired
private Random random;
@SneakyThrows
@Async
public void background() {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
this.traceManager.addAnnotation("background-sleep-millis", String.valueOf(millis));

View File

@@ -16,9 +16,8 @@
package sample;
import java.util.Random;
import java.util.concurrent.Callable;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.cloud.sleuth.Span;
@@ -31,8 +30,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.util.Random;
import java.util.concurrent.Callable;
/**
* @author Spencer Gibb
@@ -49,12 +48,13 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
private TraceAccessor accessor;
@Autowired
private SampleBackground controller;
@Autowired
private Random random;
private int port;
@SneakyThrows
@RequestMapping("/")
public String hi() {
final Random random = new Random();
Thread.sleep(random.nextInt(1000));
String s = this.restTemplate.getForObject("http://localhost:" + this.port
@@ -67,7 +67,6 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
return new Callable<String>() {
@Override
public String call() throws Exception {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
SampleController.this.traceManager.addAnnotation("callable-sleep-millis", String.valueOf(millis));
@@ -86,7 +85,6 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
@SneakyThrows
@RequestMapping("/hi2")
public String hi2() {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
this.traceManager.addAnnotation("random-sleep-millis", String.valueOf(millis));
@@ -98,7 +96,6 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
public String traced() {
Trace trace = this.traceManager.startSpan("customTraceEndpoint",
new AlwaysSampler(), null);
final Random random = new Random();
int millis = random.nextInt(1000);
log.info("Sleeping for {} millis", millis);
Thread.sleep(millis);
@@ -113,7 +110,6 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
@SneakyThrows
@RequestMapping("/start")
public String start() {
final Random random = new Random();
int millis = random.nextInt(1000);
log.info("Sleeping for {} millis", millis);
Thread.sleep(millis);