Rename ContextPropagator to TraceScope

This commit is contained in:
Andrew Kent 2018-03-12 10:24:43 -07:00
parent 45aff57740
commit 428e304164
4 changed files with 25 additions and 24 deletions

View File

@ -70,6 +70,7 @@ class ExecutorInstrumentationTest extends Specification {
trace.size() == 2 trace.size() == 2
trace.get(0).operationName == "parent" trace.get(0).operationName == "parent"
trace.get(1).operationName == "asyncChild" trace.get(1).operationName == "asyncChild"
trace.get(1).parentId == trace.get(0).spanId
cleanup: cleanup:
pool?.shutdown() pool?.shutdown()

View File

@ -11,7 +11,7 @@ import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.DDAdvice; import datadog.trace.agent.tooling.DDAdvice;
import datadog.trace.agent.tooling.HelperInjector; import datadog.trace.agent.tooling.HelperInjector;
import datadog.trace.agent.tooling.Instrumenter; import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.context.ContextPropagator; import datadog.trace.context.TraceScope;
import io.opentracing.Scope; import io.opentracing.Scope;
import io.opentracing.util.GlobalTracer; import io.opentracing.util.GlobalTracer;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -137,8 +137,8 @@ public final class ExecutorInstrumentation extends Instrumenter.Configurable {
public static DatadogWrapper wrapJob( public static DatadogWrapper wrapJob(
@Advice.Argument(value = 0, readOnly = false) Runnable task) { @Advice.Argument(value = 0, readOnly = false) Runnable task) {
final Scope scope = GlobalTracer.get().scopeManager().active(); final Scope scope = GlobalTracer.get().scopeManager().active();
if (scope instanceof ContextPropagator && task != null && !(task instanceof DatadogWrapper)) { if (scope instanceof TraceScope && task != null && !(task instanceof DatadogWrapper)) {
task = new RunnableWrapper(task, (ContextPropagator) scope); task = new RunnableWrapper(task, (TraceScope) scope);
return (DatadogWrapper) task; return (DatadogWrapper) task;
} }
return null; return null;
@ -158,8 +158,8 @@ public final class ExecutorInstrumentation extends Instrumenter.Configurable {
public static DatadogWrapper wrapJob( public static DatadogWrapper wrapJob(
@Advice.Argument(value = 0, readOnly = false) Callable task) { @Advice.Argument(value = 0, readOnly = false) Callable task) {
final Scope scope = GlobalTracer.get().scopeManager().active(); final Scope scope = GlobalTracer.get().scopeManager().active();
if (scope instanceof ContextPropagator && task != null && !(task instanceof DatadogWrapper)) { if (scope instanceof TraceScope && task != null && !(task instanceof DatadogWrapper)) {
task = new CallableWrapper(task, (ContextPropagator) scope); task = new CallableWrapper(task, (TraceScope) scope);
return (DatadogWrapper) task; return (DatadogWrapper) task;
} }
return null; return null;
@ -179,12 +179,12 @@ public final class ExecutorInstrumentation extends Instrumenter.Configurable {
public static Collection<?> wrapJob( public static Collection<?> wrapJob(
@Advice.Argument(value = 0, readOnly = false) Collection<? extends Callable<?>> tasks) { @Advice.Argument(value = 0, readOnly = false) Collection<? extends Callable<?>> tasks) {
final Scope scope = GlobalTracer.get().scopeManager().active(); final Scope scope = GlobalTracer.get().scopeManager().active();
if (scope instanceof ContextPropagator) { if (scope instanceof TraceScope) {
Collection<Callable<?>> wrappedTasks = new ArrayList<>(tasks.size()); Collection<Callable<?>> wrappedTasks = new ArrayList<>(tasks.size());
for (Callable task : tasks) { for (Callable task : tasks) {
if (task != null) { if (task != null) {
if (!(task instanceof CallableWrapper)) { if (!(task instanceof CallableWrapper)) {
task = new CallableWrapper(task, (ContextPropagator) scope); task = new CallableWrapper(task, (TraceScope) scope);
} }
wrappedTasks.add(task); wrappedTasks.add(task);
} }
@ -211,9 +211,9 @@ public final class ExecutorInstrumentation extends Instrumenter.Configurable {
/** Marker interface for tasks which are wrapped to propagate the trace context. */ /** Marker interface for tasks which are wrapped to propagate the trace context. */
@Slf4j @Slf4j
public abstract static class DatadogWrapper { public abstract static class DatadogWrapper {
protected final ContextPropagator.Continuation continuation; protected final TraceScope.Continuation continuation;
public DatadogWrapper(ContextPropagator scope) { public DatadogWrapper(TraceScope scope) {
continuation = scope.capture(true); continuation = scope.capture(true);
log.debug("created continuation {} from scope {}", continuation, scope); log.debug("created continuation {} from scope {}", continuation, scope);
} }
@ -230,14 +230,14 @@ public final class ExecutorInstrumentation extends Instrumenter.Configurable {
public static class RunnableWrapper extends DatadogWrapper implements Runnable { public static class RunnableWrapper extends DatadogWrapper implements Runnable {
private final Runnable delegatee; private final Runnable delegatee;
public RunnableWrapper(Runnable toWrap, ContextPropagator scope) { public RunnableWrapper(Runnable toWrap, TraceScope scope) {
super(scope); super(scope);
delegatee = toWrap; delegatee = toWrap;
} }
@Override @Override
public void run() { public void run() {
final ContextPropagator context = continuation.activate(); final TraceScope context = continuation.activate();
try { try {
delegatee.run(); delegatee.run();
} finally { } finally {
@ -250,14 +250,14 @@ public final class ExecutorInstrumentation extends Instrumenter.Configurable {
public static class CallableWrapper<T> extends DatadogWrapper implements Callable<T> { public static class CallableWrapper<T> extends DatadogWrapper implements Callable<T> {
private final Callable<T> delegatee; private final Callable<T> delegatee;
public CallableWrapper(Callable<T> toWrap, ContextPropagator scope) { public CallableWrapper(Callable<T> toWrap, TraceScope scope) {
super(scope); super(scope);
delegatee = toWrap; delegatee = toWrap;
} }
@Override @Override
public T call() throws Exception { public T call() throws Exception {
final ContextPropagator context = continuation.activate(); final TraceScope context = continuation.activate();
try { try {
return delegatee.call(); return delegatee.call();
} finally { } finally {

View File

@ -1,10 +1,10 @@
package datadog.trace.context; package datadog.trace.context;
/** An object when can propagate a datadog trace across multiple threads. */ /** An object when can propagate a datadog trace across multiple threads. */
public interface ContextPropagator { public interface TraceScope {
/** /**
* Prevent the trace attached to this ContextPropagator from reporting until the returned * Prevent the trace attached to this TraceScope from reporting until the returned Continuation
* Continuation finishes. * finishes.
* *
* <p>Should be called on the parent thread. * <p>Should be called on the parent thread.
*/ */
@ -20,6 +20,6 @@ public interface ContextPropagator {
* *
* <p>Should be called on the child thread. * <p>Should be called on the child thread.
*/ */
ContextPropagator activate(); TraceScope activate();
} }
} }

View File

@ -2,7 +2,7 @@ package datadog.opentracing.scopemanager;
import datadog.opentracing.DDSpanContext; import datadog.opentracing.DDSpanContext;
import datadog.opentracing.PendingTrace; import datadog.opentracing.PendingTrace;
import datadog.trace.context.ContextPropagator; import datadog.trace.context.TraceScope;
import io.opentracing.Scope; import io.opentracing.Scope;
import io.opentracing.Span; import io.opentracing.Span;
import io.opentracing.noop.NoopScopeManager; import io.opentracing.noop.NoopScopeManager;
@ -13,7 +13,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class ContinuableScope implements Scope, ContextPropagator { public class ContinuableScope implements Scope, TraceScope {
final ContextualScopeManager scopeManager; final ContextualScopeManager scopeManager;
final AtomicInteger refCount; final AtomicInteger refCount;
private final Span wrapped; private final Span wrapped;
@ -67,7 +67,7 @@ public class ContinuableScope implements Scope, ContextPropagator {
return new Continuation(this.finishOnClose && finishOnClose); return new Continuation(this.finishOnClose && finishOnClose);
} }
public class Continuation implements Closeable, ContextPropagator.Continuation { public class Continuation implements Closeable, TraceScope.Continuation {
public WeakReference<Continuation> ref; public WeakReference<Continuation> ref;
private final AtomicBoolean used = new AtomicBoolean(false); private final AtomicBoolean used = new AtomicBoolean(false);
@ -109,7 +109,7 @@ public class ContinuableScope implements Scope, ContextPropagator {
} }
} }
private class ClosingScope implements Scope, ContextPropagator { private class ClosingScope implements Scope, TraceScope {
private final Scope wrappedScope; private final Scope wrappedScope;
private ClosingScope(final Scope wrappedScope) { private ClosingScope(final Scope wrappedScope) {
@ -118,11 +118,11 @@ public class ContinuableScope implements Scope, ContextPropagator {
@Override @Override
public Continuation capture(boolean finishOnClose) { public Continuation capture(boolean finishOnClose) {
if (wrappedScope instanceof ContextPropagator) { if (wrappedScope instanceof TraceScope) {
return ((ContextPropagator) wrappedScope).capture(finishOnClose); return ((TraceScope) wrappedScope).capture(finishOnClose);
} else { } else {
log.debug( log.debug(
"{} Failed to capture. ClosingScope does not wrap a ContextPropagator: {}.", "{} Failed to capture. ClosingScope does not wrap a TraceScope: {}.",
this, this,
wrappedScope); wrappedScope);
return null; return null;