Skip to content

Commit

Permalink
Rewrite using ManagedScope abstraction for scope stack management
Browse files Browse the repository at this point in the history
Signed-off-by: monosoul <[email protected]>
  • Loading branch information
monosoul committed Dec 9, 2022
1 parent c7b4468 commit 35df53a
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 137 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package datadog.trace.core;

import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.ManagedScope;
import kotlin.coroutines.CoroutineContext;
import kotlin.jvm.functions.Function2;
import kotlinx.coroutines.ThreadContextElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class NoOpContextElement implements ThreadContextElement<Object> {
static final Key<NoOpContextElement> KEY = new Key<NoOpContextElement>() {};
public class ManagedScopeCoroutineContext implements ThreadContextElement<ManagedScope> {

public NoOpContextElement() {}
static final Key<ManagedScopeCoroutineContext> KEY = new Key<ManagedScopeCoroutineContext>() {};
private final ManagedScope managedScope = AgentTracer.get().delegateManagedScope();

@Override
public void restoreThreadContext(@NotNull CoroutineContext coroutineContext, Object oldState) {}
public void restoreThreadContext(
@NotNull CoroutineContext coroutineContext, ManagedScope oldState) {
oldState.activate();
}

@Override
public Object updateThreadContext(@NotNull CoroutineContext coroutineContext) {
return null;
public ManagedScope updateThreadContext(@NotNull CoroutineContext coroutineContext) {
final ManagedScope oldManagedScope = AgentTracer.get().delegateManagedScope();
oldManagedScope.fetch();

managedScope.activate();

return oldManagedScope;
}

@Nullable
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.instrumentation.kotlin.coroutines;

import datadog.trace.core.ScopeStackCoroutineContextHelper;
import datadog.trace.core.ManagedScopeCoroutineContext;
import kotlin.coroutines.CoroutineContext;
import net.bytebuddy.asm.Advice;

Expand All @@ -9,7 +9,7 @@ public class CoroutineContextAdvice {
public static void enter(
@Advice.Argument(value = 1, readOnly = false) CoroutineContext coroutineContext) {
if (coroutineContext != null) {
coroutineContext = ScopeStackCoroutineContextHelper.addScopeStackContext(coroutineContext);
coroutineContext = coroutineContext.plus(new ManagedScopeCoroutineContext());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package datadog.trace.instrumentation.kotlin.coroutines;

import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.util.Strings.getPackageName;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.core.CoreTracer;

@AutoService(Instrumenter.class)
public class KotlinCoroutinesInstrumentation extends Instrumenter.Tracing
Expand All @@ -17,13 +15,6 @@ public KotlinCoroutinesInstrumentation() {
super("kotlin-coroutines");
}

@Override
public String[] helperClassNames() {
return new String[] {
getPackageName(CoreTracer.class.getName()) + ".ScopeStackCoroutineContextHelper",
};
}

@Override
public String instrumentedType() {
return "kotlinx.coroutines.CoroutineContextKt";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ class KotlinCoroutineInstrumentationTest extends AgentTestRunner {
ThreadPoolDispatcherKt.newSingleThreadContext("Single-Thread")
)
int expectedNumberOfSpans = kotlinTest.tracedWithSuspendingCoroutines()
TEST_WRITER.toList().forEach {
it.forEach {
println it
}
}

expect:
assertTraces(1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import datadog.trace.bootstrap.instrumentation.api.AgentScopeManager;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.ManagedScope;
import datadog.trace.bootstrap.instrumentation.api.PathwayContext;
import datadog.trace.bootstrap.instrumentation.api.ProfilingContextIntegration;
import datadog.trace.bootstrap.instrumentation.api.ScopeSource;
Expand Down Expand Up @@ -199,6 +200,11 @@ public void onRootSpanStarted(AgentSpan root) {
endpointCheckpointer.onRootSpanStarted(root);
}

@Override
public ManagedScope delegateManagedScope() {
return scopeManager.delegateManagedScope();
}

public static class CoreTracerBuilder {

private Config config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static datadog.trace.api.ConfigDefaults.DEFAULT_ASYNC_PROPAGATING;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopAgentSpan;
import static datadog.trace.bootstrap.instrumentation.api.ScopeSource.INSTRUMENTATION;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;

Expand All @@ -15,6 +16,7 @@
import datadog.trace.bootstrap.instrumentation.api.AgentTrace;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.AttachableWrapper;
import datadog.trace.bootstrap.instrumentation.api.ManagedScope;
import datadog.trace.bootstrap.instrumentation.api.ProfilingContextIntegration;
import datadog.trace.bootstrap.instrumentation.api.ScopeSource;
import datadog.trace.util.AgentTaskScheduler;
Expand Down Expand Up @@ -253,6 +255,30 @@ ScopeStack scopeStack() {
return this.tlsScopeStack.get();
}

@Override
public ManagedScope delegateManagedScope() {
return new ContinuableManagedScope();
}

private class ContinuableManagedScope implements ManagedScope {

private ScopeStack localScopeStack = tlsScopeStack.initialValue();
private AgentSpan span = activeSpan();

@Override
public void activate() {
tlsScopeStack.set(localScopeStack);
if (localScopeStack.depth() == 0 && span != null) {
ContinuableScopeManager.this.activate(span, INSTRUMENTATION);
}
}

@Override
public void fetch() {
localScopeStack = tlsScopeStack.get();
}
}

private static class ContinuableScope implements AgentScope, AttachableWrapper {
private final ContinuableScopeManager scopeManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentScopeManager;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.ManagedScope;
import datadog.trace.bootstrap.instrumentation.api.ScopeSource;
import datadog.trace.util.AgentTaskScheduler;
import io.opentracing.Scope;
Expand Down Expand Up @@ -147,6 +148,26 @@ public AgentScope activateNext(final AgentSpan agentSpan) {
return converter.toAgentScope(span, scope);
}

@Override
public ManagedScope delegateManagedScope() {
return new CustomManagedScope();
}

private class CustomManagedScope implements ManagedScope {

private AgentSpan span = activeSpan();

@Override
public void activate() {
CustomScopeManagerWrapper.this.activate(span, ScopeSource.INSTRUMENTATION);
}

@Override
public void fetch() {
span = activeSpan();
}
}

private void scheduleIterationSpanCleanup(final AgentSpan span) {
if (iterationSpans == null) {
synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Allows custom scope managers. See OTScopeManager, CustomScopeManager, and ContextualScopeManager
*/
public interface AgentScopeManager {
public interface AgentScopeManager extends ManagedScopeAware {

AgentScope activate(AgentSpan span, ScopeSource source);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ public static TracerAPI get() {
private AgentTracer() {}

public interface TracerAPI
extends datadog.trace.api.Tracer, InternalTracer, AgentPropagation, EndpointCheckpointer {
extends datadog.trace.api.Tracer,
InternalTracer,
AgentPropagation,
EndpointCheckpointer,
ManagedScopeAware {
AgentSpan startSpan(CharSequence spanName);

AgentSpan startSpan(CharSequence spanName, long startTimeMicros);
Expand Down Expand Up @@ -376,6 +380,11 @@ public AgentSpan.Context notifyExtensionStart(Object event) {

@Override
public void notifyExtensionEnd(AgentSpan span, Object result, boolean isError) {}

@Override
public ManagedScope delegateManagedScope() {
return null;
}
}

public static final class NoopAgentSpan implements AgentSpan {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package datadog.trace.bootstrap.instrumentation.api;

public interface ManagedScope {
void activate();

void fetch();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package datadog.trace.bootstrap.instrumentation.api;

public interface ManagedScopeAware {
ManagedScope delegateManagedScope();
}

0 comments on commit 35df53a

Please sign in to comment.