Sampling child span

when parent is null, the created child span will be sampled

    fixes #271
This commit is contained in:
Marcin Grzejszczak
2016-05-09 18:01:01 +02:00
parent 8154bab922
commit 657bba35b3
2 changed files with 22 additions and 5 deletions

View File

@@ -82,11 +82,7 @@ public class DefaultTracer implements Tracer {
if (sampler==null) {
sampler = this.defaultSampler;
}
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();
}
span = sampledSpan(name, id, span, sampler);
this.spanLogger.logStartedSpan(null, span);
}
return continueSpan(span);
@@ -147,6 +143,7 @@ public class DefaultTracer implements Tracer {
if (parent == null) {
Span span = Span.builder().begin(System.currentTimeMillis()).name(name)
.traceId(id).spanId(id).build();
span = sampledSpan(name, id, span, this.defaultSampler);
this.spanLogger.logStartedSpan(null, span);
return span;
}
@@ -163,6 +160,15 @@ public class DefaultTracer implements Tracer {
}
}
private Span sampledSpan(String name, long id, Span span, Sampler sampler) {
if (!sampler.isSampled(span)) {
// Non-exportable so we keep the trace but not other data
return Span.builder().begin(span.getBegin()).name(name).traceId(id)
.spanId(id).exportable(false).build();
}
return span;
}
private long createId() {
return this.random.nextLong();
}

View File

@@ -30,6 +30,7 @@ import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.SpanNamer;
import org.springframework.cloud.sleuth.SpanReporter;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.assertions.SleuthAssertions;
import org.springframework.cloud.sleuth.log.SpanLogger;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.cloud.sleuth.sampler.NeverSampler;
@@ -161,6 +162,16 @@ public class DefaultTracerTests {
assertThat(tracer.getCurrentSpan(), is(equalTo(grandParent)));
}
@Test
public void samplingIsRanAgainstChildSpanWhenThereIsNoParent() {
DefaultTracer tracer = new DefaultTracer(new NeverSampler(), new Random(),
this.spanNamer, this.spanLogger, this.spanReporter);
Span span = tracer.createChild(null, "childName");
SleuthAssertions.assertThat(span.isExportable()).isFalse();
}
@Test
public void shouldUpdateLogsInSpanWhenItGetsContinued() {
DefaultTracer tracer = new DefaultTracer(new AlwaysSampler(), new Random(),