Merge pull request #462 from DataDog/ark/root-span

Add getRootSpan() to MutableSpan
This commit is contained in:
Andrew Kent 2018-08-27 11:51:06 -07:00 committed by GitHub
commit 79a33d7ac5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 0 deletions

View File

@ -41,4 +41,6 @@ public interface MutableSpan {
Boolean isError();
MutableSpan setError(boolean value);
MutableSpan getRootSpan();
}

View File

@ -125,6 +125,11 @@ public class DDSpan implements Span, MutableSpan {
return this;
}
@Override
public MutableSpan getRootSpan() {
return context().getTrace().getRootSpan();
}
public void setErrorMeta(final Throwable error) {
setError(true);

View File

@ -40,6 +40,16 @@ public class PendingTrace extends ConcurrentLinkedDeque<DDSpan> {
Collections.newSetFromMap(new ConcurrentHashMap<WeakReference<?>, Boolean>());
private final AtomicInteger pendingReferenceCount = new AtomicInteger(0);
/**
* During a trace there are cases where the root span must be accessed (e.g. priority sampling and
* trace-search tags).
*
* <p>Use a weak ref because we still need to handle buggy cases where the root span is not
* correctly closed (see SpanCleaner).
*
* <p>The root span will be available in non-buggy cases because it has either finished and
* strongly ref'd in this queue or is unfinished and ref'd in a ContinuableScope.
*/
private final AtomicReference<WeakReference<DDSpan>> rootSpan = new AtomicReference<>();
/** Ensure a trace is never written multiple times */

View File

@ -183,4 +183,18 @@ class DDSpanTest extends Specification {
child2.getMetrics().get(DDSpanContext.PRIORITY_SAMPLING_KEY) == null
child2.getMetrics().get(DDSpanContext.SAMPLE_RATE_KEY) == null
}
def "getRootSpan returns the root span"() {
setup:
def root = tracer.buildSpan("root").start()
def child = tracer.buildSpan("child").asChildOf(root).start()
expect:
root.getRootSpan() == root
child.getRootSpan() == root
cleanup:
child.finish()
root.finish()
}
}