Move code from `WrapAdviceUtils` to `DatadogWrapper`
MODIFY
dd-java-agent/instrumentation/java-concurrent/src/main/java/datadog/trace/instrumentation/java/concurrent/ExecutorInstrumentation.java
dd-java-agent/instrumentation/java-concurrent/src/main/java/datadog/trace/instrumentation/java/concurrent/ExecutorInstrumentation.java
@@ -41,7 +41,6 @@ public final class ExecutorInstrumentation extends Instrumenter.Configurable {
public static final HelperInjector EXEC_HELPER_INJECTOR =
new HelperInjector(
ExecutorInstrumentation.class.getName() + "$ConcurrentUtils",
- ExecutorInstrumentation.class.getName() + "$WrapAdviceUtils",
ExecutorInstrumentation.class.getName() + "$DatadogWrapper",
ExecutorInstrumentation.class.getName() + "$CallableWrapper",
ExecutorInstrumentation.class.getName() + "$RunnableWrapper");
@@ -157,60 +156,6 @@ public final class ExecutorInstrumentation extends Instrumenter.Configurable {
.asDecorator();
}
- /** Utils class to provide helper methods to wrap Runnable and Callable */
- public static class WrapAdviceUtils {
-
- /**
- * Check if given call to executor is nested. We would like to ignore nested calls to execute to
- * avoid wrapping tasks twice. Note: this condition may lead to problems with executors that
- * 'fork' several tasks, but we do not have such executors at the moment. Note: this condition
- * is mutating and needs to be checked right before task is actually wrapped.
- *
- * @return true iff call is nested
- */
- @SuppressWarnings("WeakerAccess")
- public static boolean isTopLevelCall() {
- return CallDepthThreadLocalMap.incrementCallDepth(ExecutorService.class) <= 0;
- }
-
- /** Reset nested calls to executor. */
- @SuppressWarnings("WeakerAccess")
- public static void resetNestedCalls() {
- CallDepthThreadLocalMap.reset(ExecutorService.class);
- }
-
- /**
- * @param task tak object
- * @return true iff given task object should be wrapped
- */
- @SuppressWarnings("WeakerAccess")
- public static boolean shouldWrapTask(Object task) {
- final Scope scope = GlobalTracer.get().scopeManager().active();
- return (scope instanceof TraceScope
- && ((TraceScope) scope).isAsyncPropagating()
- && task != null
- && !(task instanceof DatadogWrapper)
- && isTopLevelCall());
- }
-
- /**
- * Clean up after job submittion method has exited
- *
- * @param wrapper task wrapper
- * @param throwable throwable that may have been thrown
- */
- @SuppressWarnings("WeakerAccess")
- public static void cleanUpOnMethodExit(
- final DatadogWrapper wrapper, final Throwable throwable) {
- if (null != wrapper) {
- resetNestedCalls();
- if (null != throwable) {
- wrapper.cancel();
- }
- }
- }
- }
-
public static class WrapRunnableAdvice {
@SuppressWarnings("unused")
@@ -218,7 +163,7 @@ public final class ExecutorInstrumentation extends Instrumenter.Configurable {
public static DatadogWrapper enterJobSubmit(
@Advice.Argument(value = 0, readOnly = false) Runnable task) {
final Scope scope = GlobalTracer.get().scopeManager().active();
- if (WrapAdviceUtils.shouldWrapTask(task)) {
+ if (DatadogWrapper.shouldWrapTask(task)) {
task = new RunnableWrapper(task, (TraceScope) scope);
return (RunnableWrapper) task;
}
@@ -229,7 +174,7 @@ public final class ExecutorInstrumentation extends Instrumenter.Configurable {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exitJobSubmit(
@Advice.Enter final DatadogWrapper wrapper, @Advice.Thrown final Throwable throwable) {
- WrapAdviceUtils.cleanUpOnMethodExit(wrapper, throwable);
+ DatadogWrapper.cleanUpOnMethodExit(wrapper, throwable);
}
}
@@ -241,7 +186,7 @@ public final class ExecutorInstrumentation extends Instrumenter.Configurable {
@Advice.Argument(value = 0, readOnly = false) Callable<?> task) {
final Scope scope = GlobalTracer.get().scopeManager().active();
- if (WrapAdviceUtils.shouldWrapTask(task)) {
+ if (DatadogWrapper.shouldWrapTask(task)) {
task = new CallableWrapper<>(task, (TraceScope) scope);
return (CallableWrapper) task;
}
@@ -252,7 +197,7 @@ public final class ExecutorInstrumentation extends Instrumenter.Configurable {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exitJobSubmit(
@Advice.Enter final DatadogWrapper wrapper, @Advice.Thrown final Throwable throwable) {
- WrapAdviceUtils.cleanUpOnMethodExit(wrapper, throwable);
+ DatadogWrapper.cleanUpOnMethodExit(wrapper, throwable);
}
}
@@ -266,7 +211,7 @@ public final class ExecutorInstrumentation extends Instrumenter.Configurable {
if (scope instanceof TraceScope
&& ((TraceScope) scope).isAsyncPropagating()
&& tasks != null
- && WrapAdviceUtils.isTopLevelCall()) {
+ && DatadogWrapper.isTopLevelCall()) {
final Collection<Callable<?>> wrappedTasks = new ArrayList<>(tasks.size());
for (Callable<?> task : tasks) {
if (task != null && !(task instanceof CallableWrapper)) {
@@ -284,7 +229,7 @@ public final class ExecutorInstrumentation extends Instrumenter.Configurable {
public static void checkCancel(
@Advice.Enter final Collection<?> wrappedJobs, @Advice.Thrown final Throwable throwable) {
if (null != wrappedJobs) {
- WrapAdviceUtils.resetNestedCalls();
+ DatadogWrapper.resetNestedCalls();
if (null != throwable) {
for (final Object wrapper : wrappedJobs) {
@@ -313,6 +258,56 @@ public final class ExecutorInstrumentation extends Instrumenter.Configurable {
log.debug("canceled continuation {}", continuation);
}
}
+
+ /**
+ * Check if given call to executor is nested. We would like to ignore nested calls to execute to
+ * avoid wrapping tasks twice. Note: this condition may lead to problems with executors that
+ * 'fork' several tasks, but we do not have such executors at the moment. Note: this condition
+ * is mutating and needs to be checked right before task is actually wrapped.
+ *
+ * @return true iff call is nested
+ */
+ @SuppressWarnings("WeakerAccess")
+ public static boolean isTopLevelCall() {
+ return CallDepthThreadLocalMap.incrementCallDepth(ExecutorService.class) <= 0;
+ }
+
+ /** Reset nested calls to executor. */
+ @SuppressWarnings("WeakerAccess")
+ public static void resetNestedCalls() {
+ CallDepthThreadLocalMap.reset(ExecutorService.class);
+ }
+
+ /**
+ * @param task tak object
+ * @return true iff given task object should be wrapped
+ */
+ @SuppressWarnings("WeakerAccess")
+ public static boolean shouldWrapTask(Object task) {
+ final Scope scope = GlobalTracer.get().scopeManager().active();
+ return (scope instanceof TraceScope
+ && ((TraceScope) scope).isAsyncPropagating()
+ && task != null
+ && !(task instanceof DatadogWrapper)
+ && isTopLevelCall());
+ }
+
+ /**
+ * Clean up after job submittion method has exited
+ *
+ * @param wrapper task wrapper
+ * @param throwable throwable that may have been thrown
+ */
+ @SuppressWarnings("WeakerAccess")
+ public static void cleanUpOnMethodExit(
+ final DatadogWrapper wrapper, final Throwable throwable) {
+ if (null != wrapper) {
+ resetNestedCalls();
+ if (null != throwable) {
+ wrapper.cancel();
+ }
+ }
+ }
}
@Slf4j

Move code from `WrapAdviceUtils` to `DatadogWrapper`