Skip to content

Commit

Permalink
migrate to indy scala concurrent plugin (#1367)
Browse files Browse the repository at this point in the history
Co-authored-by: kananindzya <[email protected]>
Co-authored-by: Sylvain Juge <[email protected]>
  • Loading branch information
3 people authored Sep 15, 2020
1 parent dd90bc4 commit d0068d8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
* under the License.
* #L%
*/
package co.elastic.apm.agent.scala.concurrent;
package co.elastic.apm.agent.scalaconcurrent;

import co.elastic.apm.agent.bci.TracerAwareInstrumentation;
import co.elastic.apm.agent.bci.VisibleForAdvice;
import co.elastic.apm.agent.impl.transaction.AbstractSpan;
import com.blogspot.mydailyjava.weaklockfree.WeakConcurrentMap;
import net.bytebuddy.asm.Advice;
Expand All @@ -44,7 +43,6 @@

public abstract class FutureInstrumentation extends TracerAwareInstrumentation {

@VisibleForAdvice
@SuppressWarnings("WeakerAccess")
public static final WeakConcurrentMap<Object, AbstractSpan<?>> promisesToContext =
new WeakConcurrentMap.WithInlinedExpunction<>();
Expand All @@ -55,11 +53,6 @@ public Collection<String> getInstrumentationGroupNames() {
return Arrays.asList("scala-future", "experimental");
}

@Override
public boolean indyPlugin() {
return false;
}

public static class ConstructorInstrumentation extends FutureInstrumentation {

@Override
Expand All @@ -72,7 +65,7 @@ public ElementMatcher<? super MethodDescription> getMethodMatcher() {
return isConstructor();
}

@Advice.OnMethodExit(suppress = Throwable.class)
@Advice.OnMethodExit(suppress = Throwable.class, inline = false)
public static void onExit(@Advice.This Object thiz) {
final AbstractSpan<?> context = tracer.getActive();
if (context != null) {
Expand All @@ -97,25 +90,25 @@ public ElementMatcher<? super MethodDescription> getMethodMatcher() {
return named("run").and(returns(void.class));
}

@VisibleForAdvice
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(@Advice.This Object thiz, @Nullable @Advice.Local("context") AbstractSpan<?> context) {
context = promisesToContext.remove(thiz);
@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
public static Object onEnter(@Advice.This Object thiz) {
AbstractSpan<?> context = promisesToContext.remove(thiz);
if (context != null) {
context.activate();
// decrements the reference we incremented to avoid that the parent context gets recycled before the promise is run
// because we have activated it, we can be sure it doesn't get recycled until we deactivate in the OnMethodExit advice
context.decrementReferences();
}
return context;
}

@Advice.OnMethodExit(suppress = Throwable.class)
public static void onExit(@Nullable @Advice.Local("context") AbstractSpan<?> context) {
if (context != null) {
@Advice.OnMethodExit(suppress = Throwable.class, inline = false)
public static void onExit(@Advice.Enter @Nullable Object abstractSpanObj) {
if (abstractSpanObj instanceof AbstractSpan<?>) {
AbstractSpan<?> context = (AbstractSpan<?>) abstractSpanObj;
context.deactivate();
}
}

}

}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
co.elastic.apm.agent.scala.concurrent.FutureInstrumentation$ConstructorInstrumentation
co.elastic.apm.agent.scala.concurrent.FutureInstrumentation$RunInstrumentation
co.elastic.apm.agent.scalaconcurrent.FutureInstrumentation$ConstructorInstrumentation
co.elastic.apm.agent.scalaconcurrent.FutureInstrumentation$RunInstrumentation
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package co.elastic.apm.agent.scala.concurrent
package co.elastic.apm.agent.scalaconcurrent

import java.util.concurrent.Executors

Expand Down

0 comments on commit d0068d8

Please sign in to comment.