Sleuth now uses Brave (#829)

with this pull request we have rewritten the whole Sleuth internals to use Brave. That way we can leverage all the functionalities & instrumentations that Brave already has (https://github.com/openzipkin/brave/tree/master/instrumentation).

Migration guide is available here: https://github.com/spring-cloud/spring-cloud-sleuth/wiki/Spring-Cloud-Sleuth-2.0-Migration-Guide

fixes #711 - Brave instrumentation
fixes #92 - we move to Brave's Sampler
fixes #143 - Brave is capable of passing context
fixes #255 - we've moved away from Zipkin Stream server
fixes #305 - Brave has GRPC instrumentation (https://github.com/openzipkin/brave/tree/master/instrumentation/grpc)
fixes #459 - Brave (openzipkin/brave#510) & Zipkin (openzipkin/zipkin#1754) will deal with the AWS XRay instrumentation
fixes #577 - Messaging instrumentation has been rewritten
This commit is contained in:
Marcin Grzejszczak
2018-01-19 22:45:47 +01:00
committed by GitHub
parent 9f716d7d92
commit 7eb374b5a5
370 changed files with 8165 additions and 18115 deletions

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- ~ Copyright 2013-2017 the original author or authors. ~ ~ Licensed under
<!-- ~ Copyright 2013-2018 the original author or authors. ~ ~ Licensed under
the Apache License, Version 2.0 (the "License"); ~ you may not use this file
except in compliance with the License. ~ You may obtain a copy of the License
at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2013-2017 the original author or authors.
~ Copyright 2013-2018 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,8 +18,8 @@ package sample;
import java.util.Random;
import brave.Tracer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@@ -31,14 +31,13 @@ public class SampleBackground {
@Autowired
private Tracer tracer;
@Autowired
private Random random;
private Random random = new Random();
@Async
public void background() throws InterruptedException {
int millis = this.random.nextInt(1000);
Thread.sleep(millis);
this.tracer.addTag("background-sleep-millis", String.valueOf(millis));
this.tracer.currentSpan().tag("background-sleep-millis", String.valueOf(millis));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,13 @@ import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;
import brave.sampler.Sampler;
import integration.MessagingApplicationTests.IntegrationSpanCollectorConfig;
import sample.SampleMessagingApplication;
import tools.AbstractIntegrationTest;
import tools.SpanUtil;
import zipkin2.Span;
import zipkin2.reporter.Reporter;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -31,12 +38,6 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import integration.MessagingApplicationTests.IntegrationSpanCollectorConfig;
import sample.SampleMessagingApplication;
import tools.AbstractIntegrationTest;
import zipkin2.Span;
import zipkin2.reporter.Reporter;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.BDDAssertions.then;
@@ -113,8 +114,7 @@ public class MessagingApplicationTests extends AbstractIntegrationTest {
.collect(Collectors.joining("\n")) + "\n]");
then(this.integrationTestSpanCollector.hashedSpans
.stream()
.filter(span ->
org.springframework.cloud.sleuth.Span.hexToId(span.traceId()) != traceId)
.filter(span -> !span.traceId().equals(SpanUtil.idToHex(traceId)))
.collect(Collectors.toList()))
.describedAs("All spans have same trace id [" + traceIdHex + "]")
.isEmpty();
@@ -124,11 +124,11 @@ public class MessagingApplicationTests extends AbstractIntegrationTest {
Optional<Span> firstHttpSpan = findFirstHttpRequestSpan();
List<Span> eventSpans = findAllEventRelatedSpans();
Optional<Span> eventSentSpan = findSpanWithKind(Span.Kind.SERVER);
Optional<Span> eventReceivedSpan = findSpanWithKind(Span.Kind.CLIENT);
Optional<Span> producerSpan = findSpanWithKind(Span.Kind.PRODUCER);
Optional<Span> lastHttpSpansParent = findLastHttpSpansParent();
// "http:/parent/" -> "message:messages" -> "http:/foo" (CS + CR) -> "http:/foo" (SS)
thenAllSpansArePresent(firstHttpSpan, eventSpans, lastHttpSpansParent, eventSentSpan, eventReceivedSpan);
then(this.integrationTestSpanCollector.hashedSpans).as("There were 4 spans").hasSize(4);
thenAllSpansArePresent(firstHttpSpan, eventSpans, lastHttpSpansParent, eventSentSpan, producerSpan);
then(this.integrationTestSpanCollector.hashedSpans).as("There were 3 spans").hasSize(3);
log.info("Checking the parent child structure");
List<Optional<Span>> parentChild = this.integrationTestSpanCollector.hashedSpans.stream()
.filter(span -> span.parentId() != null)
@@ -140,7 +140,7 @@ public class MessagingApplicationTests extends AbstractIntegrationTest {
private Optional<Span> findLastHttpSpansParent() {
return this.integrationTestSpanCollector.hashedSpans.stream()
.filter(span -> "http:/foo".equals(span.name()) && span.kind() != null).findFirst();
.filter(span -> "get".equals(span.name()) && span.kind() != null).findFirst();
}
private Optional<Span> findSpanWithKind(Span.Kind kind) {
@@ -151,7 +151,7 @@ public class MessagingApplicationTests extends AbstractIntegrationTest {
private List<Span> findAllEventRelatedSpans() {
return this.integrationTestSpanCollector.hashedSpans.stream()
.filter(span -> "message:messages".equals(span.name()) && span.parentId() != null).collect(
.filter(span -> "send".equals(span.name()) && span.parentId() != null).collect(
Collectors.toList());
}
@@ -186,5 +186,9 @@ public class MessagingApplicationTests extends AbstractIntegrationTest {
Reporter<Span> integrationTestZipkinSpanReporter() {
return new IntegrationTestZipkinSpanReporter();
}
@Bean Sampler sampler() {
return Sampler.ALWAYS_SAMPLE;
}
}
}

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2013-2017 the original author or authors.
~ Copyright 2013-2018 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,8 +32,7 @@ public class SampleController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private Random random;
private Random random = new Random();
@RequestMapping("/")
public String hi() throws InterruptedException {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2013-2017 the original author or authors.
~ Copyright 2013-2018 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.

View File

@@ -1,37 +0,0 @@
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth.trace;
import org.springframework.cloud.sleuth.Span;
/**
* @author Spencer Gibb
*/
public class IntegrationTestSpanContextHolder {
public static Span getCurrentSpan() {
return SpanContextHolder.getCurrentSpan();
}
public static void removeCurrentSpan() {
SpanContextHolder.removeCurrentSpan();
}
public static boolean isTracing() {
return SpanContextHolder.isTracing();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,13 +16,11 @@
package tools;
import java.lang.invoke.MethodHandles;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.awaitility.Awaitility;
import org.awaitility.core.ConditionFactory;
import org.junit.After;
import org.junit.Before;
import org.springframework.cloud.sleuth.trace.IntegrationTestSpanContextHolder;
import org.springframework.web.client.RestTemplate;
import static java.util.concurrent.TimeUnit.SECONDS;
@@ -38,16 +36,6 @@ public abstract class AbstractIntegrationTest {
protected static final int TIMEOUT = 20;
protected final RestTemplate restTemplate = new AssertingRestTemplate();
@Before
public void clearSpanBefore() {
IntegrationTestSpanContextHolder.removeCurrentSpan();
}
@After
public void clearSpanAfter() {
IntegrationTestSpanContextHolder.removeCurrentSpan();
}
protected Runnable httpMessageWithTraceIdInHeadersIsSuccessfullySent(String endpoint, long traceId) {
return new RequestSendingRunnable(this.restTemplate, endpoint, traceId, traceId);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@ import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.sleuth.Span;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
@@ -37,6 +36,8 @@ import static org.assertj.core.api.BDDAssertions.then;
* @author Marcin Grzejszczak
*/
public class RequestSendingRunnable implements Runnable {
static final String TRACE_ID_NAME = "X-B3-TraceId";
static final String SPAN_ID_NAME = "X-B3-SpanId";
private static final Log log = LogFactory.getLog(RequestSendingRunnable.class);
@@ -65,8 +66,8 @@ public class RequestSendingRunnable implements Runnable {
private RequestEntity<Void> requestWithTraceId() {
HttpHeaders headers = new HttpHeaders();
headers.add(Span.TRACE_ID_NAME, Span.idToHex(this.traceId));
headers.add(Span.SPAN_ID_NAME, Span.idToHex(this.spanId));
headers.add(TRACE_ID_NAME,SpanUtil.idToHex(this.traceId));
headers.add(SPAN_ID_NAME, SpanUtil.idToHex(this.spanId));
URI uri = URI.create(this.url);
RequestEntity<Void> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, uri);
log.info("Request [" + requestEntity + "] is ready");

View File

@@ -0,0 +1,33 @@
package tools;
public class SpanUtil {
/**
* Represents given long id as 16-character lower-hex string
*/
public static String idToHex(long id) {
char[] data = new char[16];
writeHexLong(data, 0, id);
return new String(data);
}
/** Inspired by {@code okio.Buffer.writeLong} */
static void writeHexLong(char[] data, int pos, long v) {
writeHexByte(data, pos + 0, (byte) ((v >>> 56L) & 0xff));
writeHexByte(data, pos + 2, (byte) ((v >>> 48L) & 0xff));
writeHexByte(data, pos + 4, (byte) ((v >>> 40L) & 0xff));
writeHexByte(data, pos + 6, (byte) ((v >>> 32L) & 0xff));
writeHexByte(data, pos + 8, (byte) ((v >>> 24L) & 0xff));
writeHexByte(data, pos + 10, (byte) ((v >>> 16L) & 0xff));
writeHexByte(data, pos + 12, (byte) ((v >>> 8L) & 0xff));
writeHexByte(data, pos + 14, (byte) (v & 0xff));
}
static void writeHexByte(char[] data, int pos, byte b) {
data[pos + 0] = HEX_DIGITS[(b >> 4) & 0xf];
data[pos + 1] = HEX_DIGITS[b & 0xf];
}
static final char[] HEX_DIGITS =
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
}

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2013-2017 the original author or authors.
~ Copyright 2013-2018 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2013-2017 the original author or authors.
~ Copyright 2013-2018 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,8 +18,8 @@ package sample;
import java.util.Random;
import brave.Tracer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@@ -31,14 +31,13 @@ public class SampleBackground {
@Autowired
private Tracer tracer;
@Autowired
private Random random;
private Random random = new Random();
@Async
public void background() throws InterruptedException {
int millis = this.random.nextInt(1000);
Thread.sleep(millis);
this.tracer.addTag("background-sleep-millis", String.valueOf(millis));
this.tracer.currentSpan().tag("background-sleep-millis", String.valueOf(millis));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,22 +16,20 @@
package sample;
import java.util.Random;
import java.util.concurrent.Callable;
import brave.Span;
import brave.Tracer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.SpanAccessor;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.context.ApplicationListener;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Random;
import java.util.concurrent.Callable;
/**
* @author Spencer Gibb
*/
@@ -46,11 +44,8 @@ ApplicationListener<ServletWebServerInitializedEvent> {
@Autowired
private Tracer tracer;
@Autowired
private SpanAccessor accessor;
@Autowired
private SampleBackground controller;
@Autowired
private Random random;
private Random random = new Random();
private int port;
@RequestMapping("/")
@@ -69,8 +64,8 @@ ApplicationListener<ServletWebServerInitializedEvent> {
public String call() throws Exception {
int millis = SampleController.this.random.nextInt(1000);
Thread.sleep(millis);
SampleController.this.tracer.addTag("callable-sleep-millis", String.valueOf(millis));
Span currentSpan = SampleController.this.accessor.getCurrentSpan();
Span currentSpan = SampleController.this.tracer.currentSpan();
currentSpan.tag("callable-sleep-millis", String.valueOf(millis));
return "async hi: " + currentSpan;
}
};
@@ -88,22 +83,21 @@ ApplicationListener<ServletWebServerInitializedEvent> {
log.info("hi2");
int millis = this.random.nextInt(1000);
Thread.sleep(millis);
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));
this.tracer.currentSpan().tag("random-sleep-millis", String.valueOf(millis));
return "hi2";
}
@RequestMapping("/traced")
public String traced() throws InterruptedException {
Span span = this.tracer.createSpan("http:customTraceEndpoint",
new AlwaysSampler());
Span span = this.tracer.nextSpan().name("http:customTraceEndpoint").start();
int millis = this.random.nextInt(1000);
log.info(String.format("Sleeping for [%d] millis", millis));
Thread.sleep(millis);
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));
this.tracer.currentSpan().tag("random-sleep-millis", String.valueOf(millis));
String s = this.restTemplate.getForObject("http://localhost:" + this.port
+ "/call", String.class);
this.tracer.close(span);
span.finish();
return "traced/" + s;
}
@@ -112,8 +106,7 @@ ApplicationListener<ServletWebServerInitializedEvent> {
int millis = this.random.nextInt(1000);
log.info(String.format("Sleeping for [%d] millis", millis));
Thread.sleep(millis);
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));
this.tracer.currentSpan().tag("random-sleep-millis", String.valueOf(millis));
String s = this.restTemplate.getForObject("http://localhost:" + this.port
+ "/call", String.class);
return "start/" + s;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,27 +16,22 @@
package sample;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import zipkin2.Span;
import zipkin2.reporter.Reporter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.client.RestTemplate;
import zipkin2.Span;
import zipkin2.reporter.Reporter;
/**
* @author Spencer Gibb
*/
@SpringBootApplication
@EnableAsync
public class SampleZipkinApplication {
private static final Log log = LogFactory.getLog(SampleZipkinApplication.class);
public static void main(String[] args) {
SpringApplication.run(SampleZipkinApplication.class, args);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,10 +23,16 @@ import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;
import brave.sampler.Sampler;
import integration.ZipkinTests.WaitUntilZipkinIsUpConfig;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import sample.SampleZipkinApplication;
import tools.AbstractIntegrationTest;
import tools.SpanUtil;
import zipkin2.Span;
import zipkin2.codec.SpanBytesDecoder;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -39,10 +45,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import sample.SampleZipkinApplication;
import tools.AbstractIntegrationTest;
import zipkin2.Span;
import zipkin2.codec.SpanBytesDecoder;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.BDDAssertions.then;
@@ -88,6 +90,10 @@ public class ZipkinTests extends AbstractIntegrationTest {
zipkinProperties.setBaseUrl(zipkin.url("/").toString());
return zipkinProperties;
}
@Bean Sampler sampler() {
return Sampler.ALWAYS_SAMPLE;
}
}
void spansSentToZipkin(MockWebServer zipkin, long traceId)
@@ -107,7 +113,7 @@ public class ZipkinTests extends AbstractIntegrationTest {
}
List<String> traceIdsNotFoundInZipkin(List<Span> spans, long traceId) {
String traceIdString = org.springframework.cloud.sleuth.Span.idToHex(traceId);
String traceIdString = SpanUtil.idToHex(traceId);
Optional<String> traceIds = spans.stream()
.map(Span::traceId)
.filter(traceIdString::equals)

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2013-2017 the original author or authors.
~ Copyright 2013-2018 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,8 +18,10 @@ package sample;
import java.util.Random;
import brave.Tracer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@@ -28,17 +30,18 @@ import org.springframework.stereotype.Component;
*/
@Component
public class SampleBackground {
private static final Log log = LogFactory.getLog(SampleBackground.class);
@Autowired
private Tracer tracer;
@Autowired
private Random random;
private Random random = new Random();
@Async
public void background() throws InterruptedException {
log.info("background");
int millis = this.random.nextInt(1000);
Thread.sleep(millis);
this.tracer.addTag("background-sleep-millis", String.valueOf(millis));
this.tracer.currentSpan().tag("background-sleep-millis", String.valueOf(millis));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,22 +16,20 @@
package sample;
import java.util.Random;
import java.util.concurrent.Callable;
import brave.Span;
import brave.Tracer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.SpanAccessor;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.context.ApplicationListener;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Random;
import java.util.concurrent.Callable;
/**
* @author Spencer Gibb
*/
@@ -44,8 +42,6 @@ public class SampleController
@Autowired
private Tracer tracer;
@Autowired
private SpanAccessor accessor;
@Autowired
private SampleBackground controller;
private final Random random = new Random();
@@ -53,6 +49,7 @@ public class SampleController
@RequestMapping("/")
public String hi() throws InterruptedException {
log.info("hi!");
Thread.sleep(this.random.nextInt(1000));
String s = this.restTemplate
@@ -65,51 +62,55 @@ public class SampleController
return new Callable<String>() {
@Override
public String call() throws Exception {
log.info("call");
int millis = SampleController.this.random.nextInt(1000);
Thread.sleep(millis);
SampleController.this.tracer.addTag("callable-sleep-millis",
SampleController.this.tracer.currentSpan().tag("callable-sleep-millis",
String.valueOf(millis));
Span currentSpan = SampleController.this.accessor.getCurrentSpan();
return "async hi: " + currentSpan;
Span span = SampleController.this.tracer.currentSpan();
return "async hi: " + span;
}
};
}
@RequestMapping("/async")
public String async() throws InterruptedException {
log.info("async");
this.controller.background();
return "ho";
}
@RequestMapping("/hi2")
public String hi2() throws InterruptedException {
log.info("hi2!");
int millis = this.random.nextInt(1000);
Thread.sleep(millis);
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));
this.tracer.currentSpan().tag("random-sleep-millis", String.valueOf(millis));
return "hi2";
}
@RequestMapping("/traced")
public String traced() throws InterruptedException {
Span span = this.tracer.createSpan("http:customTraceEndpoint",
new AlwaysSampler());
log.info("traced");
Span span = this.tracer.nextSpan().name("http:customTraceEndpoint");
int millis = this.random.nextInt(1000);
log.info(String.format("Sleeping for [%d] millis", millis));
Thread.sleep(millis);
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));
this.tracer.currentSpan().tag("random-sleep-millis", String.valueOf(millis));
String s = this.restTemplate
.getForObject("http://localhost:" + this.port + "/call", String.class);
this.tracer.close(span);
span.finish();
return "traced/" + s;
}
@RequestMapping("/start")
public String start() throws InterruptedException {
log.info("start");
int millis = this.random.nextInt(1000);
log.info(String.format("Sleeping for [%d] millis", millis));
Thread.sleep(millis);
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));
this.tracer.currentSpan().tag("random-sleep-millis", String.valueOf(millis));
String s = this.restTemplate
.getForObject("http://localhost:" + this.port + "/call", String.class);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package sample;
import brave.sampler.Sampler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@@ -26,7 +27,6 @@ import org.springframework.web.client.RestTemplate;
* @author Spencer Gibb
*/
@SpringBootApplication
@EnableAsync
public class SampleSleuthApplication {
@@ -40,6 +40,11 @@ public class SampleSleuthApplication {
return new SampleController();
}
@Bean
public Sampler sampler() {
return Sampler.ALWAYS_SAMPLE;
}
public static void main(String[] args) {
SpringApplication.run(SampleSleuthApplication.class, args);
}