Skip to content
Open
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
Expand Up @@ -2,19 +2,27 @@

import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.currentContext;
import static datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter.ExcludeType.FORK_JOIN_TASK;
import static datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter.ExcludeType.RUNNABLE;
import static java.util.Collections.singletonList;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;

import com.google.auto.service.AutoService;
import datadog.context.ContextHandle;
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.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter.ExcludeType;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import net.bytebuddy.asm.Advice;

@AutoService(InstrumenterModule.class)
public class PekkoMailboxInstrumentation extends InstrumenterModule.ContextTracking
implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice {
implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice, ExcludeFilterProvider {

public PekkoMailboxInstrumentation() {
super("pekko_actor_mailbox", "pekko_actor", "pekko_concurrent", "java_concurrent");
Expand All @@ -25,6 +33,16 @@ public String instrumentedType() {
return "org.apache.pekko.dispatch.Mailbox";
}

@Override
public Map<ExcludeType, List<String>> excludedClasses() {
// A mailbox run can process several messages, each with its own context in the envelope.
List<String> mailboxTasks = singletonList("org.apache.pekko.dispatch.Dispatcher$$anon$1");
Map<ExcludeType, List<String>> exclusions = new EnumMap<>(ExcludeType.class);
exclusions.put(RUNNABLE, mailboxTasks);
exclusions.put(FORK_JOIN_TASK, mailboxTasks);
return exclusions;
}

@Override
public void methodAdvice(MethodTransformer transformer) {
transformer.applyAdvice(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package datadog.trace.instrumentation.pekko.concurrent;

import static datadog.trace.agent.test.assertions.SpanMatcher.span;
import static datadog.trace.agent.test.assertions.TraceAssertions.SORT_BY_START_TIME;
import static datadog.trace.agent.test.assertions.TraceMatcher.SORT_BY_ANCESTRY;
import static datadog.trace.agent.test.assertions.TraceMatcher.trace;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.typesafe.config.ConfigFactory;
import datadog.context.ContextScope;
import datadog.trace.agent.test.AbstractInstrumentationTest;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.apache.pekko.actor.AbstractActor;
import org.apache.pekko.actor.ActorCell;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorRefWithCell;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.actor.Props;
import org.apache.pekko.dispatch.Mailbox;
import org.apache.pekko.testkit.javadsl.TestKit;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class PekkoMailboxForkedTest extends AbstractInstrumentationTest {
@ParameterizedTest
@ValueSource(strings = {"thread-pool-executor", "fork-join-executor"})
void eachMessageUsesItsOwnTraceContext(String executor) throws Exception {
ActorSystem system =
ActorSystem.create(
"mailbox-test",
ConfigFactory.parseString(
"mailbox-dispatcher {\n"
+ " executor = "
+ executor
+ "\n"
+ " throughput = 2\n"
+ " throughput-deadline-time = 0ms\n"
+ "}"));
CompletableFuture<Void> releaseFirst = new CompletableFuture<>();
try {
ActorRef actor =
system.actorOf(Props.create(Receiver.class).withDispatcher("mailbox-dispatcher"));
CountDownLatch ready = new CountDownLatch(1);
actor.tell((Runnable) ready::countDown, ActorRef.noSender());
assertTrue(ready.await(10, TimeUnit.SECONDS), "Actor did not receive the warm-up message");

// Wait for the mailbox to become idle so that the traced send schedules it.
Mailbox mailbox = ((ActorCell) ((ActorRefWithCell) actor).underlying()).mailbox();
new TestKit(system).awaitCond(Duration.ofSeconds(10), () -> !mailbox.isScheduled());

CountDownLatch firstStarted = new CountDownLatch(1);
Runnable first =
() -> {
startSpan("test", "first").finish();
firstStarted.countDown();
releaseFirst.join();
};
AgentSpan parent = startSpan("test", "parent");
try (ContextScope ignored = activateSpan(parent)) {
actor.tell(first, ActorRef.noSender());
} finally {
parent.finish();
}

// Queue the second message while the mailbox is still processing the first.
assertTrue(
firstStarted.await(10, TimeUnit.SECONDS), "Actor did not receive the first message");
actor.tell((Runnable) () -> startSpan("test", "second").finish(), ActorRef.noSender());
releaseFirst.complete(null);

assertTraces(
SORT_BY_START_TIME,
trace(
SORT_BY_ANCESTRY,
span().root().operationName("parent"),
span().childOfPrevious().operationName("first")),
trace(span().root().operationName("second")));
} finally {
releaseFirst.complete(null);
system.terminate();
system.getWhenTerminated().toCompletableFuture().get(10, TimeUnit.SECONDS);
}
}

public static class Receiver extends AbstractActor {
@Override
public Receive createReceive() {
return receiveBuilder().match(Runnable.class, Runnable::run).build();
}
}
}
Loading