Skip to content

Commit 01769fd

Browse files
authored
Add call depth check to executor instrumentation (#14546)
1 parent 2dcda32 commit 01769fd

File tree

1 file changed

+53
-8
lines changed

1 file changed

+53
-8
lines changed

instrumentation/executors/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/executors/JavaExecutorInstrumentation.java

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.opentelemetry.context.Context;
1919
import io.opentelemetry.instrumentation.api.util.VirtualField;
20+
import io.opentelemetry.javaagent.bootstrap.CallDepth;
2021
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
2122
import io.opentelemetry.javaagent.bootstrap.executors.ContextPropagatingRunnable;
2223
import io.opentelemetry.javaagent.bootstrap.executors.ExecutorAdviceHelper;
@@ -89,7 +90,13 @@ public static class SetExecuteRunnableStateAdvice {
8990

9091
@Advice.OnMethodEnter(suppress = Throwable.class)
9192
public static PropagatedContext enterJobSubmit(
92-
@Advice.Argument(value = 0, readOnly = false) Runnable task) {
93+
@Advice.This Object executor,
94+
@Advice.Argument(value = 0, readOnly = false) Runnable task,
95+
@Advice.Local("otelCallDepth") CallDepth callDepth) {
96+
callDepth = CallDepth.forClass(executor.getClass());
97+
if (callDepth.getAndIncrement() > 0) {
98+
return null;
99+
}
93100
Context context = Java8BytecodeBridge.currentContext();
94101
if (!ExecutorAdviceHelper.shouldPropagateContext(context, task)) {
95102
return null;
@@ -107,7 +114,11 @@ public static PropagatedContext enterJobSubmit(
107114
public static void exitJobSubmit(
108115
@Advice.Argument(0) Runnable task,
109116
@Advice.Enter PropagatedContext propagatedContext,
110-
@Advice.Thrown Throwable throwable) {
117+
@Advice.Thrown Throwable throwable,
118+
@Advice.Local("otelCallDepth") CallDepth callDepth) {
119+
if (callDepth.decrementAndGet() > 0) {
120+
return;
121+
}
111122
VirtualField<Runnable, PropagatedContext> virtualField =
112123
VirtualField.find(Runnable.class, PropagatedContext.class);
113124
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, task);
@@ -144,7 +155,13 @@ public static class SetSubmitRunnableStateAdvice {
144155

145156
@Advice.OnMethodEnter(suppress = Throwable.class)
146157
public static PropagatedContext enterJobSubmit(
147-
@Advice.Argument(value = 0, readOnly = false) Runnable task) {
158+
@Advice.This Object executor,
159+
@Advice.Argument(value = 0, readOnly = false) Runnable task,
160+
@Advice.Local("otelCallDepth") CallDepth callDepth) {
161+
callDepth = CallDepth.forClass(executor.getClass());
162+
if (callDepth.getAndIncrement() > 0) {
163+
return null;
164+
}
148165
Context context = Java8BytecodeBridge.currentContext();
149166
if (ExecutorAdviceHelper.shouldPropagateContext(context, task)) {
150167
VirtualField<Runnable, PropagatedContext> virtualField =
@@ -159,7 +176,11 @@ public static void exitJobSubmit(
159176
@Advice.Argument(0) Runnable task,
160177
@Advice.Enter PropagatedContext propagatedContext,
161178
@Advice.Thrown Throwable throwable,
162-
@Advice.Return Future<?> future) {
179+
@Advice.Return Future<?> future,
180+
@Advice.Local("otelCallDepth") CallDepth callDepth) {
181+
if (callDepth.decrementAndGet() > 0) {
182+
return;
183+
}
163184
if (propagatedContext != null && future != null) {
164185
VirtualField<Future<?>, PropagatedContext> virtualField =
165186
VirtualField.find(Future.class, PropagatedContext.class);
@@ -175,7 +196,14 @@ public static void exitJobSubmit(
175196
public static class SetCallableStateAdvice {
176197

177198
@Advice.OnMethodEnter(suppress = Throwable.class)
178-
public static PropagatedContext enterJobSubmit(@Advice.Argument(0) Callable<?> task) {
199+
public static PropagatedContext enterJobSubmit(
200+
@Advice.This Object executor,
201+
@Advice.Argument(0) Callable<?> task,
202+
@Advice.Local("otelCallDepth") CallDepth callDepth) {
203+
callDepth = CallDepth.forClass(executor.getClass());
204+
if (callDepth.getAndIncrement() > 0) {
205+
return null;
206+
}
179207
Context context = Java8BytecodeBridge.currentContext();
180208
if (ExecutorAdviceHelper.shouldPropagateContext(context, task)) {
181209
VirtualField<Callable<?>, PropagatedContext> virtualField =
@@ -190,7 +218,11 @@ public static void exitJobSubmit(
190218
@Advice.Argument(0) Callable<?> task,
191219
@Advice.Enter PropagatedContext propagatedContext,
192220
@Advice.Thrown Throwable throwable,
193-
@Advice.Return Future<?> future) {
221+
@Advice.Return Future<?> future,
222+
@Advice.Local("otelCallDepth") CallDepth callDepth) {
223+
if (callDepth.decrementAndGet() > 0) {
224+
return;
225+
}
194226
if (propagatedContext != null && future != null) {
195227
VirtualField<Future<?>, PropagatedContext> virtualField =
196228
VirtualField.find(Future.class, PropagatedContext.class);
@@ -207,11 +239,18 @@ public static class SetCallableStateForCallableCollectionAdvice {
207239

208240
@Advice.OnMethodEnter(suppress = Throwable.class)
209241
public static Collection<?> submitEnter(
210-
@Advice.Argument(0) Collection<? extends Callable<?>> tasks) {
242+
@Advice.This Object executor,
243+
@Advice.Argument(0) Collection<? extends Callable<?>> tasks,
244+
@Advice.Local("otelCallDepth") CallDepth callDepth) {
211245
if (tasks == null) {
212246
return Collections.emptyList();
213247
}
214248

249+
callDepth = CallDepth.forClass(executor.getClass());
250+
if (callDepth.getAndIncrement() > 0) {
251+
return Collections.emptyList();
252+
}
253+
215254
Context context = Java8BytecodeBridge.currentContext();
216255
for (Callable<?> task : tasks) {
217256
if (ExecutorAdviceHelper.shouldPropagateContext(context, task)) {
@@ -228,7 +267,13 @@ public static Collection<?> submitEnter(
228267

229268
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
230269
public static void submitExit(
231-
@Advice.Enter Collection<? extends Callable<?>> tasks, @Advice.Thrown Throwable throwable) {
270+
@Advice.Enter Collection<? extends Callable<?>> tasks,
271+
@Advice.Thrown Throwable throwable,
272+
@Advice.Local("otelCallDepth") CallDepth callDepth) {
273+
if (callDepth.decrementAndGet() > 0) {
274+
return;
275+
}
276+
232277
/*
233278
Note1: invokeAny doesn't return any futures so all we need to do for it
234279
is to make sure we close all scopes in case of an exception.

0 commit comments

Comments
 (0)