diff --git a/instrumentation/java.completable-future-jdk8/README.md b/instrumentation/java.completable-future-jdk8/README.md new file mode 100644 index 0000000000..d38aa4039c --- /dev/null +++ b/instrumentation/java.completable-future-jdk8/README.md @@ -0,0 +1,2 @@ +# java.completable-future-jdk8 + diff --git a/instrumentation/java.completable-future-jdk8/src/main/java/java/util/concurrent/CompletableFuture_Instrumentation.java b/instrumentation/java.completable-future-jdk8/src/main/java/java/util/concurrent/CompletableFuture_Instrumentation.java index 59acbe084e..500668883e 100644 --- a/instrumentation/java.completable-future-jdk8/src/main/java/java/util/concurrent/CompletableFuture_Instrumentation.java +++ b/instrumentation/java.completable-future-jdk8/src/main/java/java/util/concurrent/CompletableFuture_Instrumentation.java @@ -12,40 +12,46 @@ import util.TokenAwareRunnable; import util.TokenDelegateExecutor; - @Weave(type = MatchType.ExactClass, originalName = "java.util.concurrent.CompletableFuture") public class CompletableFuture_Instrumentation { - @Weave(type = MatchType.BaseClass, originalName = "java.util.concurrent.CompletableFuture$Async") - abstract static class Async extends ForkJoinTask - implements Runnable, CompletableFuture.AsynchronousCompletionTask { - public final Void getRawResult() { return null; } - public final void setRawResult(Void v) { } - public final void run() { exec(); } - } - - private static boolean noParallelism(Executor e) { - return (e == ForkJoinPool.commonPool() && - ForkJoinPool.getCommonPoolParallelism() <= 1); - } - - private static Executor useTokenDelegateExecutor(Executor e) { - if (null == e || e instanceof TokenDelegateExecutor) { - return e; - } else { - return new TokenDelegateExecutor(e); + @Weave(type = MatchType.BaseClass, originalName = "java.util.concurrent.CompletableFuture$Async") + abstract static class Async extends ForkJoinTask + implements Runnable, CompletableFuture.AsynchronousCompletionTask { + public final Void getRawResult() { + return null; + } + + public final void setRawResult(Void v) { + } + + public final void run() { + exec(); + } + } + + private static boolean noParallelism(Executor e) { + return (e == ForkJoinPool.commonPool() && + ForkJoinPool.getCommonPoolParallelism() <= 1); + } + + private static Executor useTokenDelegateExecutor(Executor e) { + if (null == e || e instanceof TokenDelegateExecutor) { + return e; + } else { + return new TokenDelegateExecutor(e); + } } - } - - static void execAsync(Executor e, CompletableFuture_Instrumentation.Async r) { - if (noParallelism(e)) - new Thread(new TokenAwareRunnable(r)).start(); - else { - Executor tde = useTokenDelegateExecutor(e); - if(null != tde) { - tde.execute(r); + + static void execAsync(Executor e, CompletableFuture_Instrumentation.Async r) { + if (noParallelism(e)) { + new Thread(new TokenAwareRunnable(r)).start(); + } else { + Executor tde = useTokenDelegateExecutor(e); + if (null != tde) { + tde.execute(r); + } } } - } } diff --git a/instrumentation/java.completable-future-jdk8/src/main/java/util/TokenAndRefUtils.java b/instrumentation/java.completable-future-jdk8/src/main/java/util/TokenAndRefUtils.java index 372f2df2c4..c2cf202439 100644 --- a/instrumentation/java.completable-future-jdk8/src/main/java/util/TokenAndRefUtils.java +++ b/instrumentation/java.completable-future-jdk8/src/main/java/util/TokenAndRefUtils.java @@ -9,43 +9,43 @@ public class TokenAndRefUtils { - public static AgentBridge.TokenAndRefCount getThreadTokenAndRefCount() { - AgentBridge.TokenAndRefCount tokenAndRefCount = AgentBridge.activeToken.get(); - if (tokenAndRefCount == null) { - Transaction tx = AgentBridge.getAgent().getTransaction(false); - if (tx != null) { - tokenAndRefCount = new AgentBridge.TokenAndRefCount(tx.getToken(), - AgentBridge.getAgent().getTracedMethod(), new AtomicInteger(1)); - } - } else { - tokenAndRefCount.refCount.incrementAndGet(); + public static AgentBridge.TokenAndRefCount getThreadTokenAndRefCount() { + AgentBridge.TokenAndRefCount tokenAndRefCount = AgentBridge.activeToken.get(); + if (tokenAndRefCount == null) { + Transaction tx = AgentBridge.getAgent().getTransaction(false); + if (tx != null) { + tokenAndRefCount = new AgentBridge.TokenAndRefCount(tx.getToken(), + AgentBridge.getAgent().getTracedMethod(), new AtomicInteger(1)); + } + } else { + tokenAndRefCount.refCount.incrementAndGet(); + } + return tokenAndRefCount; } - return tokenAndRefCount; - } - public static void setThreadTokenAndRefCount(AgentBridge.TokenAndRefCount tokenAndRefCount) { - if (tokenAndRefCount != null) { - AgentBridge.activeToken.set(tokenAndRefCount); - tokenAndRefCount.token.link(); + public static void setThreadTokenAndRefCount(AgentBridge.TokenAndRefCount tokenAndRefCount) { + if (tokenAndRefCount != null) { + AgentBridge.activeToken.set(tokenAndRefCount); + tokenAndRefCount.token.link(); + } } - } - public static void clearThreadTokenAndRefCountAndTxn(AgentBridge.TokenAndRefCount tokenAndRefCount) { - AgentBridge.activeToken.remove(); - if (tokenAndRefCount != null && tokenAndRefCount.refCount.decrementAndGet() == 0) { - tokenAndRefCount.token.expire(); - tokenAndRefCount.token = null; + public static void clearThreadTokenAndRefCountAndTxn(AgentBridge.TokenAndRefCount tokenAndRefCount) { + AgentBridge.activeToken.remove(); + if (tokenAndRefCount != null && tokenAndRefCount.refCount.decrementAndGet() == 0) { + tokenAndRefCount.token.expire(); + tokenAndRefCount.token = null; + } } - } - - public static void logTokenInfo(AgentBridge.TokenAndRefCount tokenAndRefCount, String msg) { - if (AgentBridge.getAgent().getLogger().isLoggable(Level.FINEST)) { - String tokenMsg = (tokenAndRefCount != null && tokenAndRefCount.token != null) - ? String.format("[%s:%s:%d]", tokenAndRefCount.token, tokenAndRefCount.token.getTransaction(), - tokenAndRefCount.refCount.get()) - : "[Empty token]"; - AgentBridge.getAgent().getLogger().log(Level.FINEST, MessageFormat.format("{0}: token info {1}", tokenMsg, msg)); + + public static void logTokenInfo(AgentBridge.TokenAndRefCount tokenAndRefCount, String msg) { + if (AgentBridge.getAgent().getLogger().isLoggable(Level.FINEST)) { + String tokenMsg = (tokenAndRefCount != null && tokenAndRefCount.token != null) + ? String.format("[%s:%s:%d]", tokenAndRefCount.token, tokenAndRefCount.token.getTransaction(), + tokenAndRefCount.refCount.get()) + : "[Empty token]"; + AgentBridge.getAgent().getLogger().log(Level.FINEST, MessageFormat.format("{0}: token info {1}", tokenMsg, msg)); + } } - } } diff --git a/instrumentation/java.completable-future-jdk8/src/main/java/util/TokenAwareRunnable.java b/instrumentation/java.completable-future-jdk8/src/main/java/util/TokenAwareRunnable.java index e6f47e2d56..0e6f1f3ee5 100644 --- a/instrumentation/java.completable-future-jdk8/src/main/java/util/TokenAwareRunnable.java +++ b/instrumentation/java.completable-future-jdk8/src/main/java/util/TokenAwareRunnable.java @@ -2,32 +2,34 @@ import com.newrelic.agent.bridge.AgentBridge; -import static util.TokenAndRefUtils.*; - +import static util.TokenAndRefUtils.clearThreadTokenAndRefCountAndTxn; +import static util.TokenAndRefUtils.getThreadTokenAndRefCount; +import static util.TokenAndRefUtils.logTokenInfo; +import static util.TokenAndRefUtils.setThreadTokenAndRefCount; public final class TokenAwareRunnable implements Runnable { - private final Runnable delegate; + private final Runnable delegate; - private AgentBridge.TokenAndRefCount tokenAndRefCount; + private AgentBridge.TokenAndRefCount tokenAndRefCount; - public TokenAwareRunnable(Runnable delegate) { - this.delegate = delegate; - //get token state from calling Thread - this.tokenAndRefCount = getThreadTokenAndRefCount(); - logTokenInfo(tokenAndRefCount, "TokenAwareRunnable token info set"); - } + public TokenAwareRunnable(Runnable delegate) { + this.delegate = delegate; + //get token state from calling Thread + this.tokenAndRefCount = getThreadTokenAndRefCount(); + logTokenInfo(tokenAndRefCount, "TokenAwareRunnable token info set"); + } - @Override - public void run() { - try { - if (delegate != null) { - logTokenInfo(tokenAndRefCount, "Token info set in thread"); - setThreadTokenAndRefCount(tokenAndRefCount); - delegate.run(); - } - } finally { - logTokenInfo(tokenAndRefCount, "Clearing token info from thread "); - clearThreadTokenAndRefCountAndTxn(tokenAndRefCount); + @Override + public void run() { + try { + if (delegate != null) { + logTokenInfo(tokenAndRefCount, "Token info set in thread"); + setThreadTokenAndRefCount(tokenAndRefCount); + delegate.run(); + } + } finally { + logTokenInfo(tokenAndRefCount, "Clearing token info from thread "); + clearThreadTokenAndRefCountAndTxn(tokenAndRefCount); + } } - } } diff --git a/instrumentation/java.completable-future-jdk8/src/main/java/util/TokenDelegateExecutor.java b/instrumentation/java.completable-future-jdk8/src/main/java/util/TokenDelegateExecutor.java index e9343c4d57..ab16289a1e 100644 --- a/instrumentation/java.completable-future-jdk8/src/main/java/util/TokenDelegateExecutor.java +++ b/instrumentation/java.completable-future-jdk8/src/main/java/util/TokenDelegateExecutor.java @@ -3,15 +3,15 @@ import java.util.concurrent.Executor; public class TokenDelegateExecutor implements Executor { - public final Executor delegate; + public final Executor delegate; - public TokenDelegateExecutor(final Executor delegate) { - this.delegate = delegate; - } + public TokenDelegateExecutor(final Executor delegate) { + this.delegate = delegate; + } - @Override - public void execute(Runnable runnable) { - runnable = new TokenAwareRunnable(runnable); - delegate.execute(runnable); - } + @Override + public void execute(Runnable runnable) { + runnable = new TokenAwareRunnable(runnable); + delegate.execute(runnable); + } } diff --git a/instrumentation/java.completable-future-jdk8u40/README.md b/instrumentation/java.completable-future-jdk8u40/README.md new file mode 100644 index 0000000000..f5ac086388 --- /dev/null +++ b/instrumentation/java.completable-future-jdk8u40/README.md @@ -0,0 +1,2 @@ +# java.completable-future-jdk8u40 + diff --git a/instrumentation/java.completable-future-jdk8u40/src/main/java/java/util/concurrent/CompletableFuture_Instrumentation.java b/instrumentation/java.completable-future-jdk8u40/src/main/java/java/util/concurrent/CompletableFuture_Instrumentation.java index bdaa951ca3..e0c28e82a9 100644 --- a/instrumentation/java.completable-future-jdk8u40/src/main/java/java/util/concurrent/CompletableFuture_Instrumentation.java +++ b/instrumentation/java.completable-future-jdk8u40/src/main/java/java/util/concurrent/CompletableFuture_Instrumentation.java @@ -18,102 +18,100 @@ import java.util.function.Function; import java.util.function.Supplier; - @Weave(type = MatchType.ExactClass, originalName = "java.util.concurrent.CompletableFuture") public class CompletableFuture_Instrumentation { - private static Executor useTokenDelegateExecutor(Executor e) { - if (null == e || e instanceof TokenDelegateExecutor) { - return e; - } else { - return new TokenDelegateExecutor(e); + private static Executor useTokenDelegateExecutor(Executor e) { + if (null == e || e instanceof TokenDelegateExecutor) { + return e; + } else { + return new TokenDelegateExecutor(e); + } + } + + private CompletableFuture uniApplyStage( + Executor e, Function f) { + e = useTokenDelegateExecutor(e); + return Weaver.callOriginal(); + } + + private CompletableFuture uniAcceptStage(Executor e, + Consumer f) { + e = useTokenDelegateExecutor(e); + return Weaver.callOriginal(); + } + + private CompletableFuture uniRunStage(Executor e, Runnable f) { + e = useTokenDelegateExecutor(e); + return Weaver.callOriginal(); + } + + private CompletableFuture uniWhenCompleteStage( + Executor e, BiConsumer f) { + e = useTokenDelegateExecutor(e); + return Weaver.callOriginal(); + } + + private CompletableFuture uniHandleStage( + Executor e, BiFunction f) { + e = useTokenDelegateExecutor(e); + return Weaver.callOriginal(); + } + + private CompletableFuture uniComposeStage( + Executor e, Function> f) { + e = useTokenDelegateExecutor(e); + return Weaver.callOriginal(); + } + + private CompletableFuture biApplyStage( + Executor e, CompletionStage o, + BiFunction f) { + e = useTokenDelegateExecutor(e); + return Weaver.callOriginal(); + } + + private CompletableFuture biAcceptStage( + Executor e, CompletionStage o, + BiConsumer f) { + e = useTokenDelegateExecutor(e); + return Weaver.callOriginal(); + } + + private CompletableFuture biRunStage(Executor e, CompletionStage o, + Runnable f) { + e = useTokenDelegateExecutor(e); + return Weaver.callOriginal(); + } + + private CompletableFuture orApplyStage( + Executor e, CompletionStage o, + Function f) { + e = useTokenDelegateExecutor(e); + return Weaver.callOriginal(); + } + + private CompletableFuture orAcceptStage( + Executor e, CompletionStage o, Consumer f) { + e = useTokenDelegateExecutor(e); + return Weaver.callOriginal(); + } + + private CompletableFuture orRunStage(Executor e, CompletionStage o, + Runnable f) { + e = useTokenDelegateExecutor(e); + return Weaver.callOriginal(); + } + + static CompletableFuture asyncSupplyStage(Executor e, + Supplier f) { + e = useTokenDelegateExecutor(e); + return Weaver.callOriginal(); + } + + static CompletableFuture asyncRunStage(Executor e, Runnable f) { + e = useTokenDelegateExecutor(e); + return Weaver.callOriginal(); } - } - - private CompletableFuture uniApplyStage( - Executor e, Function f) { - e = useTokenDelegateExecutor(e); - return Weaver.callOriginal(); - } - - private CompletableFuture uniAcceptStage(Executor e, - Consumer f) { - e = useTokenDelegateExecutor(e); - return Weaver.callOriginal(); - } - - private CompletableFuture uniRunStage(Executor e, Runnable f) { - e = useTokenDelegateExecutor(e); - return Weaver.callOriginal(); - } - - private CompletableFuture uniWhenCompleteStage( - Executor e, BiConsumer f) { - e = useTokenDelegateExecutor(e); - return Weaver.callOriginal(); - } - - private CompletableFuture uniHandleStage( - Executor e, BiFunction f) { - e = useTokenDelegateExecutor(e); - return Weaver.callOriginal(); - } - - private CompletableFuture uniComposeStage( - Executor e, Function> f) { - e = useTokenDelegateExecutor(e); - return Weaver.callOriginal(); - } - - - private CompletableFuture biApplyStage( - Executor e, CompletionStage o, - BiFunction f) { - e = useTokenDelegateExecutor(e); - return Weaver.callOriginal(); - } - - private CompletableFuture biAcceptStage( - Executor e, CompletionStage o, - BiConsumer f) { - e = useTokenDelegateExecutor(e); - return Weaver.callOriginal(); - } - - private CompletableFuture biRunStage(Executor e, CompletionStage o, - Runnable f) { - e = useTokenDelegateExecutor(e); - return Weaver.callOriginal(); - } - - private CompletableFuture orApplyStage( - Executor e, CompletionStage o, - Function f) { - e = useTokenDelegateExecutor(e); - return Weaver.callOriginal(); - } - - private CompletableFuture orAcceptStage( - Executor e, CompletionStage o, Consumer f) { - e = useTokenDelegateExecutor(e); - return Weaver.callOriginal(); - } - - private CompletableFuture orRunStage(Executor e, CompletionStage o, - Runnable f) { - e = useTokenDelegateExecutor(e); - return Weaver.callOriginal(); - } - - static CompletableFuture asyncSupplyStage(Executor e, - Supplier f) { - e = useTokenDelegateExecutor(e); - return Weaver.callOriginal(); - } - - static CompletableFuture asyncRunStage(Executor e, Runnable f) { - e = useTokenDelegateExecutor(e); - return Weaver.callOriginal(); - } } diff --git a/instrumentation/java.completable-future-jdk8u40/src/main/java/util/TokenAndRefUtils.java b/instrumentation/java.completable-future-jdk8u40/src/main/java/util/TokenAndRefUtils.java index 372f2df2c4..c2cf202439 100644 --- a/instrumentation/java.completable-future-jdk8u40/src/main/java/util/TokenAndRefUtils.java +++ b/instrumentation/java.completable-future-jdk8u40/src/main/java/util/TokenAndRefUtils.java @@ -9,43 +9,43 @@ public class TokenAndRefUtils { - public static AgentBridge.TokenAndRefCount getThreadTokenAndRefCount() { - AgentBridge.TokenAndRefCount tokenAndRefCount = AgentBridge.activeToken.get(); - if (tokenAndRefCount == null) { - Transaction tx = AgentBridge.getAgent().getTransaction(false); - if (tx != null) { - tokenAndRefCount = new AgentBridge.TokenAndRefCount(tx.getToken(), - AgentBridge.getAgent().getTracedMethod(), new AtomicInteger(1)); - } - } else { - tokenAndRefCount.refCount.incrementAndGet(); + public static AgentBridge.TokenAndRefCount getThreadTokenAndRefCount() { + AgentBridge.TokenAndRefCount tokenAndRefCount = AgentBridge.activeToken.get(); + if (tokenAndRefCount == null) { + Transaction tx = AgentBridge.getAgent().getTransaction(false); + if (tx != null) { + tokenAndRefCount = new AgentBridge.TokenAndRefCount(tx.getToken(), + AgentBridge.getAgent().getTracedMethod(), new AtomicInteger(1)); + } + } else { + tokenAndRefCount.refCount.incrementAndGet(); + } + return tokenAndRefCount; } - return tokenAndRefCount; - } - public static void setThreadTokenAndRefCount(AgentBridge.TokenAndRefCount tokenAndRefCount) { - if (tokenAndRefCount != null) { - AgentBridge.activeToken.set(tokenAndRefCount); - tokenAndRefCount.token.link(); + public static void setThreadTokenAndRefCount(AgentBridge.TokenAndRefCount tokenAndRefCount) { + if (tokenAndRefCount != null) { + AgentBridge.activeToken.set(tokenAndRefCount); + tokenAndRefCount.token.link(); + } } - } - public static void clearThreadTokenAndRefCountAndTxn(AgentBridge.TokenAndRefCount tokenAndRefCount) { - AgentBridge.activeToken.remove(); - if (tokenAndRefCount != null && tokenAndRefCount.refCount.decrementAndGet() == 0) { - tokenAndRefCount.token.expire(); - tokenAndRefCount.token = null; + public static void clearThreadTokenAndRefCountAndTxn(AgentBridge.TokenAndRefCount tokenAndRefCount) { + AgentBridge.activeToken.remove(); + if (tokenAndRefCount != null && tokenAndRefCount.refCount.decrementAndGet() == 0) { + tokenAndRefCount.token.expire(); + tokenAndRefCount.token = null; + } } - } - - public static void logTokenInfo(AgentBridge.TokenAndRefCount tokenAndRefCount, String msg) { - if (AgentBridge.getAgent().getLogger().isLoggable(Level.FINEST)) { - String tokenMsg = (tokenAndRefCount != null && tokenAndRefCount.token != null) - ? String.format("[%s:%s:%d]", tokenAndRefCount.token, tokenAndRefCount.token.getTransaction(), - tokenAndRefCount.refCount.get()) - : "[Empty token]"; - AgentBridge.getAgent().getLogger().log(Level.FINEST, MessageFormat.format("{0}: token info {1}", tokenMsg, msg)); + + public static void logTokenInfo(AgentBridge.TokenAndRefCount tokenAndRefCount, String msg) { + if (AgentBridge.getAgent().getLogger().isLoggable(Level.FINEST)) { + String tokenMsg = (tokenAndRefCount != null && tokenAndRefCount.token != null) + ? String.format("[%s:%s:%d]", tokenAndRefCount.token, tokenAndRefCount.token.getTransaction(), + tokenAndRefCount.refCount.get()) + : "[Empty token]"; + AgentBridge.getAgent().getLogger().log(Level.FINEST, MessageFormat.format("{0}: token info {1}", tokenMsg, msg)); + } } - } } diff --git a/instrumentation/java.completable-future-jdk8u40/src/main/java/util/TokenAwareRunnable.java b/instrumentation/java.completable-future-jdk8u40/src/main/java/util/TokenAwareRunnable.java index e6f47e2d56..0e6f1f3ee5 100644 --- a/instrumentation/java.completable-future-jdk8u40/src/main/java/util/TokenAwareRunnable.java +++ b/instrumentation/java.completable-future-jdk8u40/src/main/java/util/TokenAwareRunnable.java @@ -2,32 +2,34 @@ import com.newrelic.agent.bridge.AgentBridge; -import static util.TokenAndRefUtils.*; - +import static util.TokenAndRefUtils.clearThreadTokenAndRefCountAndTxn; +import static util.TokenAndRefUtils.getThreadTokenAndRefCount; +import static util.TokenAndRefUtils.logTokenInfo; +import static util.TokenAndRefUtils.setThreadTokenAndRefCount; public final class TokenAwareRunnable implements Runnable { - private final Runnable delegate; + private final Runnable delegate; - private AgentBridge.TokenAndRefCount tokenAndRefCount; + private AgentBridge.TokenAndRefCount tokenAndRefCount; - public TokenAwareRunnable(Runnable delegate) { - this.delegate = delegate; - //get token state from calling Thread - this.tokenAndRefCount = getThreadTokenAndRefCount(); - logTokenInfo(tokenAndRefCount, "TokenAwareRunnable token info set"); - } + public TokenAwareRunnable(Runnable delegate) { + this.delegate = delegate; + //get token state from calling Thread + this.tokenAndRefCount = getThreadTokenAndRefCount(); + logTokenInfo(tokenAndRefCount, "TokenAwareRunnable token info set"); + } - @Override - public void run() { - try { - if (delegate != null) { - logTokenInfo(tokenAndRefCount, "Token info set in thread"); - setThreadTokenAndRefCount(tokenAndRefCount); - delegate.run(); - } - } finally { - logTokenInfo(tokenAndRefCount, "Clearing token info from thread "); - clearThreadTokenAndRefCountAndTxn(tokenAndRefCount); + @Override + public void run() { + try { + if (delegate != null) { + logTokenInfo(tokenAndRefCount, "Token info set in thread"); + setThreadTokenAndRefCount(tokenAndRefCount); + delegate.run(); + } + } finally { + logTokenInfo(tokenAndRefCount, "Clearing token info from thread "); + clearThreadTokenAndRefCountAndTxn(tokenAndRefCount); + } } - } } diff --git a/instrumentation/java.completable-future-jdk8u40/src/main/java/util/TokenDelegateExecutor.java b/instrumentation/java.completable-future-jdk8u40/src/main/java/util/TokenDelegateExecutor.java index e9343c4d57..ab16289a1e 100644 --- a/instrumentation/java.completable-future-jdk8u40/src/main/java/util/TokenDelegateExecutor.java +++ b/instrumentation/java.completable-future-jdk8u40/src/main/java/util/TokenDelegateExecutor.java @@ -3,15 +3,15 @@ import java.util.concurrent.Executor; public class TokenDelegateExecutor implements Executor { - public final Executor delegate; + public final Executor delegate; - public TokenDelegateExecutor(final Executor delegate) { - this.delegate = delegate; - } + public TokenDelegateExecutor(final Executor delegate) { + this.delegate = delegate; + } - @Override - public void execute(Runnable runnable) { - runnable = new TokenAwareRunnable(runnable); - delegate.execute(runnable); - } + @Override + public void execute(Runnable runnable) { + runnable = new TokenAwareRunnable(runnable); + delegate.execute(runnable); + } }