-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2050 from DataDog/ban/akka-http-bah
Akka Actor and Akka HTTP bindAndHandle instrumentation
- Loading branch information
Showing
25 changed files
with
1,289 additions
and
509 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 67 additions & 20 deletions
87
dd-java-agent/instrumentation/akka-concurrent/src/akka23Test/groovy/AkkaActorTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,82 @@ | ||
import datadog.trace.core.DDSpan | ||
import datadog.trace.bootstrap.instrumentation.api.Tags | ||
import datadog.trace.agent.test.AgentTestRunner | ||
import spock.lang.Shared | ||
|
||
class AkkaActorTest extends AgentTestRunner { | ||
@Shared | ||
def akkaTester = new AkkaActors() | ||
|
||
def "akka #testMethod"() { | ||
def "akka actor send #name #iterations"() { | ||
setup: | ||
AkkaActors akkaTester = new AkkaActors() | ||
akkaTester."$testMethod"() | ||
def barrier = akkaTester.block(name) | ||
|
||
TEST_WRITER.waitForTraces(1) | ||
List<DDSpan> trace = TEST_WRITER.get(0) | ||
when: | ||
(1..iterations).each {i -> | ||
akkaTester.send(name, "$who $i") | ||
} | ||
barrier.release() | ||
|
||
expect: | ||
TEST_WRITER.size() == 1 | ||
trace.size() == 2 | ||
trace[0].resourceName.toString() == "AkkaActors.$testMethod" | ||
findSpan(trace, "$expectedGreeting, Akka").context().getParentId() == trace[0].getSpanId() | ||
then: | ||
assertTraces(iterations) { | ||
(1..iterations).each {i -> | ||
trace(2) { | ||
sortSpansByStart() | ||
span { | ||
resourceName "AkkaActors.send" | ||
operationName "$name" | ||
tags { | ||
"$Tags.COMPONENT" "trace" | ||
defaultTags() | ||
} | ||
} | ||
span { | ||
resourceName "Receiver.tracedChild" | ||
operationName "$expectedGreeting, $who $i" | ||
childOf(span(0)) | ||
tags { | ||
"$Tags.COMPONENT" "trace" | ||
defaultTags() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
where: | ||
testMethod | expectedGreeting | ||
"basicTell" | "Howdy" | ||
"basicAsk" | "Howdy" | ||
"basicForward" | "Hello" | ||
name | who | expectedGreeting | iterations | ||
"tell" | "Akka" | "Howdy" | 1 | ||
"ask" | "Makka" | "Hi-diddly-ho" | 1 | ||
"forward" | "Pakka" | "Hello" | 1 | ||
"tell" | "Pakka" | "Howdy" | 10 | ||
"ask" | "Makka" | "Hi-diddly-ho" | 10 | ||
"forward" | "Akka" | "Hello" | 10 | ||
} | ||
|
||
private DDSpan findSpan(List<DDSpan> trace, String opName) { | ||
for (DDSpan span : trace) { | ||
if (span.getOperationName() == opName) { | ||
return span | ||
def "actor message handling should close leaked scopes"() { | ||
when: | ||
akkaTester.leak("Leaker", "drip") | ||
|
||
then: | ||
assertTraces(1) { | ||
trace(2) { | ||
sortSpansByStart() | ||
span { | ||
resourceName "AkkaActors.leak" | ||
operationName "leak all the things" | ||
tags { | ||
"$Tags.COMPONENT" "trace" | ||
defaultTags() | ||
} | ||
} | ||
span { | ||
resourceName("drip") | ||
operationName "Howdy, Leaker" | ||
childOf(span(0)) | ||
tags { | ||
defaultTags() | ||
} | ||
} | ||
} | ||
} | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...main/java/datadog/trace/instrumentation/akka/concurrent/AkkaActorCellInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package datadog.trace.instrumentation.akka.concurrent; | ||
|
||
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; | ||
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope; | ||
import static java.util.Collections.singletonMap; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
import akka.dispatch.Envelope; | ||
import com.google.auto.service.AutoService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.bootstrap.InstrumentationContext; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentTracer; | ||
import datadog.trace.bootstrap.instrumentation.java.concurrent.AdviceUtils; | ||
import datadog.trace.bootstrap.instrumentation.java.concurrent.State; | ||
import datadog.trace.context.TraceScope; | ||
import java.util.Map; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
@AutoService(Instrumenter.class) | ||
public class AkkaActorCellInstrumentation extends Instrumenter.Default { | ||
|
||
public AkkaActorCellInstrumentation() { | ||
super("java_concurrent", "akka_concurrent", "akka_actor"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<? super TypeDescription> typeMatcher() { | ||
return named("akka.actor.ActorCell"); | ||
} | ||
|
||
@Override | ||
public Map<String, String> contextStore() { | ||
return singletonMap("akka.dispatch.Envelope", State.class.getName()); | ||
} | ||
|
||
@Override | ||
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() { | ||
return singletonMap(isMethod().and(named("invoke")), getClass().getName() + "$InvokeAdvice"); | ||
} | ||
|
||
/** | ||
* This instrumentation is defensive and closes all scopes on the scope stack that were not there | ||
* when we started processing this actor message. The reason for that is twofold. | ||
* | ||
* <p>1) An actor is self contained, and driven by a thread that could serve many other purposes, | ||
* and a scope should not leak out after a message has been processed. | ||
* | ||
* <p>2) We rely on this cleanup mechanism to be able to intentionally leak the scope in the | ||
* {@code AkkaHttpServerInstrumentation} so that it propagates to the user provided request | ||
* handling code that will execute on the same thread in the same actor. | ||
*/ | ||
public static class InvokeAdvice { | ||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static TraceScope enter(@Advice.Argument(value = 0) Envelope envelope) { | ||
TraceScope scope = | ||
AdviceUtils.startTaskScope( | ||
InstrumentationContext.get(Envelope.class, State.class), envelope); | ||
if (scope != null) { | ||
return scope; | ||
} | ||
// If there is no scope created from the envelope, we create our own noopSpan to make sure | ||
// that we can close all scopes up until this position after exit. | ||
AgentSpan span = new AgentTracer.NoopAgentSpan(); | ||
return activateSpan(span, false); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void exit(@Advice.Enter TraceScope scope) { | ||
// Clean up any leaking scopes from akka-streams/akka-http et.c. | ||
TraceScope activeScope = activeScope(); | ||
while (activeScope != null && activeScope != scope) { | ||
activeScope.close(); | ||
activeScope = activeScope(); | ||
} | ||
while (activeScope == scope) { | ||
scope.close(); | ||
activeScope = activeScope(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.