Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datadog.trace.agent.test.InstrumentationSpecification
import datadog.trace.api.config.TraceInstrumentationConfig
import datadog.trace.bootstrap.instrumentation.api.Tags
import spock.lang.Shared

Expand Down Expand Up @@ -82,3 +83,11 @@ class AkkaActorTest extends InstrumentationSpecification {
}
}
}

class AkkaActorContextSwapTest extends AkkaActorTest {
@Override
void configurePreAgent() {
super.configurePreAgent()
injectSysConfig(TraceInstrumentationConfig.LEGACY_CONTEXT_MANAGER_ENABLED, "false")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.checkpointActiveForRollback;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.rollbackActiveToCheckpoint;
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.getCurrentContext;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;

import akka.dispatch.Envelope;
import com.google.auto.service.AutoService;
import datadog.context.Context;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import datadog.trace.api.InstrumenterConfig;
import datadog.trace.bootstrap.InstrumentationContext;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.java.concurrent.AdviceUtils;
Expand Down Expand Up @@ -54,24 +57,34 @@ public void methodAdvice(MethodTransformer transformer) {
*/
public static class InvokeAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope enter(@Advice.Argument(value = 0) Envelope envelope) {
public static Context enter(
@Advice.Argument(value = 0) Envelope envelope,
@Advice.Local("taskScope") AgentScope taskScope) {

// do this before checkpointing, as the envelope's task scope may already be active
AgentScope taskScope =
taskScope =
AdviceUtils.startTaskScope(
InstrumentationContext.get(Envelope.class, State.class), envelope);

// remember the currently active scope so we can roll back to this point
checkpointActiveForRollback();

return taskScope;
if (InstrumenterConfig.get().isLegacyContextManagerEnabled()) {
// remember the currently active scope so we can roll back to this point
checkpointActiveForRollback();
return null;
} else {
return getCurrentContext().swap();
}
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exit(@Advice.Enter AgentScope taskScope) {
public static void exit(
@Advice.Local("taskScope") AgentScope taskScope, @Advice.Enter Context checkpointContext) {

// Clean up any leaking scopes from akka-streams/akka-http etc.
rollbackActiveToCheckpoint();
if (checkpointContext == null) {
// Clean up any leaking scopes from akka-streams/akka-http etc.
rollbackActiveToCheckpoint();
} else {
checkpointContext.swap();
}

// close envelope's task scope if we previously started it
if (taskScope != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.checkpointActiveForRollback;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.rollbackActiveToCheckpoint;
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.getCurrentContext;
import static java.util.Collections.singletonList;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;

import com.google.auto.service.AutoService;
import datadog.context.Context;
import datadog.trace.agent.tooling.ExcludeFilterProvider;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import datadog.trace.api.InstrumenterConfig;
import datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter;
import java.util.Collection;
import java.util.EnumMap;
Expand Down Expand Up @@ -59,15 +62,24 @@ public void methodAdvice(MethodTransformer transformer) {
*/
public static final class SuppressMailboxRunAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void enter() {
// remember the currently active scope so we can roll back to this point
checkpointActiveForRollback();
public static Context enter() {
if (InstrumenterConfig.get().isLegacyContextManagerEnabled()) {
// remember the currently active scope so we can roll back to this point
checkpointActiveForRollback();
return null;
} else {
return getCurrentContext().swap();
}
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exit() {
// Clean up any leaking scopes from akka-streams/akka-http etc.
rollbackActiveToCheckpoint();
public static void exit(@Advice.Enter final Context checkpointContext) {
if (checkpointContext == null) {
// Clean up any leaking scopes from akka-streams/akka-http etc.
rollbackActiveToCheckpoint();
} else {
checkpointContext.swap();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public static MessageConsumerState beforeReceive(@Advice.This final MessageConsu
} else {
final AgentSpan previousSpan = spanFromContext(getRootContext().swap());
if (previousSpan != null) {
CONSUMER_DECORATE.beforeFinish(previousSpan);
previousSpan.finishWithEndToEnd();
}
}
Expand Down Expand Up @@ -178,6 +179,7 @@ public static void afterReceive(
} else {
final AgentSpan previousSpan = spanFromContext(span.swap());
if (previousSpan != null) {
CONSUMER_DECORATE.beforeFinish(previousSpan);
previousSpan.finishWithEndToEnd();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.checkpointActiveForRollback;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.rollbackActiveToCheckpoint;
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.getCurrentContext;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;

import com.google.auto.service.AutoService;
import datadog.context.Context;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import datadog.trace.api.InstrumenterConfig;
import datadog.trace.bootstrap.InstrumentationContext;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.java.concurrent.AdviceUtils;
Expand Down Expand Up @@ -54,24 +57,34 @@ public void methodAdvice(MethodTransformer transformer) {
*/
public static class InvokeAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope enter(@Advice.Argument(value = 0) Envelope envelope) {
public static Context enter(
@Advice.Argument(value = 0) Envelope envelope,
@Advice.Local("taskScope") AgentScope taskScope) {

// do this before checkpointing, as the envelope's task scope may already be active
AgentScope taskScope =
taskScope =
AdviceUtils.startTaskScope(
InstrumentationContext.get(Envelope.class, State.class), envelope);

// remember the currently active scope so we can roll back to this point
checkpointActiveForRollback();

return taskScope;
if (InstrumenterConfig.get().isLegacyContextManagerEnabled()) {
// remember the currently active scope so we can roll back to this point
checkpointActiveForRollback();
return null;
} else {
return getCurrentContext().swap();
}
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exit(@Advice.Enter AgentScope taskScope) {
public static void exit(
@Advice.Local("taskScope") AgentScope taskScope, @Advice.Enter Context checkpointContext) {

// Clean up any leaking scopes from pekko-streams/pekko-http etc.
rollbackActiveToCheckpoint();
if (checkpointContext == null) {
// Clean up any leaking scopes from pekko-streams/pekko-http etc.
rollbackActiveToCheckpoint();
} else {
checkpointContext.swap();
}

// close envelope's task scope if we previously started it
if (taskScope != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.checkpointActiveForRollback;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.rollbackActiveToCheckpoint;
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.getCurrentContext;
import static java.util.Collections.singletonList;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;

import com.google.auto.service.AutoService;
import datadog.context.Context;
import datadog.trace.agent.tooling.ExcludeFilterProvider;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import datadog.trace.api.InstrumenterConfig;
import datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter;
import java.util.Collection;
import java.util.EnumMap;
Expand Down Expand Up @@ -59,15 +62,24 @@ public void methodAdvice(MethodTransformer transformer) {
*/
public static final class SuppressMailboxRunAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void enter() {
// remember the currently active scope so we can roll back to this point
checkpointActiveForRollback();
public static Context enter() {
if (InstrumenterConfig.get().isLegacyContextManagerEnabled()) {
// remember the currently active scope so we can roll back to this point
checkpointActiveForRollback();
return null;
} else {
return getCurrentContext().swap();
}
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exit() {
// Clean up any leaking scopes from pekko-streams/pekko-http etc.
rollbackActiveToCheckpoint();
public static void exit(@Advice.Enter final Context checkpointContext) {
if (checkpointContext == null) {
// Clean up any leaking scopes from pekko-streams/pekko-http etc.
rollbackActiveToCheckpoint();
} else {
checkpointContext.swap();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,11 @@ class PekkoActorTest extends InstrumentationSpecification {
}
}
}

class PekkoActorContextSwapTest extends PekkoActorTest {
@Override
void configurePreAgent() {
super.configurePreAgent()
injectSysConfig(TraceInstrumentationConfig.LEGACY_CONTEXT_MANAGER_ENABLED, "false")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1171,11 +1171,19 @@ public AgentSpan activeSpan() {

@Override
public void checkpointActiveForRollback() {
if (!InstrumenterConfig.get().isLegacyContextManagerEnabled()) {
throw new IllegalStateException(
"checkpointActiveForRollback must not be called when context swap based logic is enabled");
}
this.scopeManager.checkpointActiveForRollback();
}

@Override
public void rollbackActiveToCheckpoint() {
if (!InstrumenterConfig.get().isLegacyContextManagerEnabled()) {
throw new IllegalStateException(
"rollbackActiveToCheckpoint must not be called when context swap based logic is enabled");
}
this.scopeManager.rollbackActiveToCheckpoint();
}

Expand Down
Loading