Ensure sampler is called with the new span if it is to be started

This commit is contained in:
Dave Syer
2016-02-01 18:01:34 +00:00
parent 6d3b8ffc60
commit d8b72473d1
4 changed files with 21 additions and 8 deletions

View File

@@ -71,14 +71,18 @@ public class DefaultTracer implements Tracer {
@Override
public Span startTrace(String name, Sampler sampler) {
Span span;
if (isTracing() || sampler.isSampled(getCurrentSpan())) {
if (isTracing()) {
span = createChild(getCurrentSpan(), name);
}
else {
// Non-exportable so we keep the trace but not other data
long id = createId();
span = Span.builder().begin(System.currentTimeMillis()).name(name).traceId(id)
.spanId(id).exportable(false).build();
.spanId(id).build();
if (!sampler.isSampled(span)) {
// Non-exportable so we keep the trace but not other data
span = Span.builder().begin(span.getBegin()).name(name).traceId(id)
.spanId(id).exportable(false).build();
}
this.publisher.publishEvent(new SpanAcquiredEvent(this, span));
}
return continueSpan(span);

View File

@@ -4,3 +4,6 @@ server:
spring:
application:
name: testSleuthApp
sleuth:
sampler:
percentage: 1.0

View File

@@ -16,8 +16,9 @@
package sample;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.util.Random;
import java.util.concurrent.Callable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.cloud.sleuth.Span;
@@ -29,8 +30,8 @@ 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;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
/**
* @author Spencer Gibb
@@ -55,7 +56,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
@RequestMapping("/")
public String hi() {
Thread.sleep(this.random.nextInt(1000));
log.info("Home page");
String s = this.restTemplate.getForObject("http://localhost:" + this.port
+ "/hi2", String.class);
return "hi/" + s;
@@ -77,6 +78,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
@RequestMapping("/async")
public String async() {
log.info("async");
this.controller.background();
return "ho";
}
@@ -84,6 +86,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
@SneakyThrows
@RequestMapping("/hi2")
public String hi2() {
log.info("hi2");
int millis = this.random.nextInt(1000);
Thread.sleep(millis);
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));

View File

@@ -8,6 +8,9 @@ spring:
zipkin:
# Uncomment to send to zipkin, replacing 192.168.99.100 with your zipkin IP address
# baseUrl: http://192.168.99.100:9411/
sleuth:
sampler:
percentage: 1.0
sample:
zipkin: