Skip to content

Commit 5191434

Browse files
committed
Add call depth check to executor instrumentation
1 parent 0b0c59c commit 5191434

File tree

1 file changed

+49
-8
lines changed

1 file changed

+49
-8
lines changed

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

Lines changed: 49 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;
@@ -26,6 +27,7 @@
2627
import java.util.Collection;
2728
import java.util.Collections;
2829
import java.util.concurrent.Callable;
30+
import java.util.concurrent.Executor;
2931
import java.util.concurrent.ForkJoinTask;
3032
import java.util.concurrent.Future;
3133
import net.bytebuddy.asm.Advice;
@@ -89,7 +91,12 @@ public static class SetExecuteRunnableStateAdvice {
8991

9092
@Advice.OnMethodEnter(suppress = Throwable.class)
9193
public static PropagatedContext enterJobSubmit(
92-
@Advice.Argument(value = 0, readOnly = false) Runnable task) {
94+
@Advice.Argument(value = 0, readOnly = false) Runnable task,
95+
@Advice.Local("otelCallDepth") CallDepth callDepth) {
96+
callDepth = CallDepth.forClass(Executor.class);
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,12 @@ 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.Argument(value = 0, readOnly = false) Runnable task,
159+
@Advice.Local("otelCallDepth") CallDepth callDepth) {
160+
callDepth = CallDepth.forClass(Executor.class);
161+
if (callDepth.getAndIncrement() > 0) {
162+
return null;
163+
}
148164
Context context = Java8BytecodeBridge.currentContext();
149165
if (ExecutorAdviceHelper.shouldPropagateContext(context, task)) {
150166
VirtualField<Runnable, PropagatedContext> virtualField =
@@ -159,7 +175,11 @@ public static void exitJobSubmit(
159175
@Advice.Argument(0) Runnable task,
160176
@Advice.Enter PropagatedContext propagatedContext,
161177
@Advice.Thrown Throwable throwable,
162-
@Advice.Return Future<?> future) {
178+
@Advice.Return Future<?> future,
179+
@Advice.Local("otelCallDepth") CallDepth callDepth) {
180+
if (callDepth.decrementAndGet() > 0) {
181+
return;
182+
}
163183
if (propagatedContext != null && future != null) {
164184
VirtualField<Future<?>, PropagatedContext> virtualField =
165185
VirtualField.find(Future.class, PropagatedContext.class);
@@ -175,7 +195,12 @@ public static void exitJobSubmit(
175195
public static class SetCallableStateAdvice {
176196

177197
@Advice.OnMethodEnter(suppress = Throwable.class)
178-
public static PropagatedContext enterJobSubmit(@Advice.Argument(0) Callable<?> task) {
198+
public static PropagatedContext enterJobSubmit(
199+
@Advice.Argument(0) Callable<?> task, @Advice.Local("otelCallDepth") CallDepth callDepth) {
200+
callDepth = CallDepth.forClass(Executor.class);
201+
if (callDepth.getAndIncrement() > 0) {
202+
return null;
203+
}
179204
Context context = Java8BytecodeBridge.currentContext();
180205
if (ExecutorAdviceHelper.shouldPropagateContext(context, task)) {
181206
VirtualField<Callable<?>, PropagatedContext> virtualField =
@@ -190,7 +215,11 @@ public static void exitJobSubmit(
190215
@Advice.Argument(0) Callable<?> task,
191216
@Advice.Enter PropagatedContext propagatedContext,
192217
@Advice.Thrown Throwable throwable,
193-
@Advice.Return Future<?> future) {
218+
@Advice.Return Future<?> future,
219+
@Advice.Local("otelCallDepth") CallDepth callDepth) {
220+
if (callDepth.decrementAndGet() > 0) {
221+
return;
222+
}
194223
if (propagatedContext != null && future != null) {
195224
VirtualField<Future<?>, PropagatedContext> virtualField =
196225
VirtualField.find(Future.class, PropagatedContext.class);
@@ -207,11 +236,17 @@ public static class SetCallableStateForCallableCollectionAdvice {
207236

208237
@Advice.OnMethodEnter(suppress = Throwable.class)
209238
public static Collection<?> submitEnter(
210-
@Advice.Argument(0) Collection<? extends Callable<?>> tasks) {
239+
@Advice.Argument(0) Collection<? extends Callable<?>> tasks,
240+
@Advice.Local("otelCallDepth") CallDepth callDepth) {
211241
if (tasks == null) {
212242
return Collections.emptyList();
213243
}
214244

245+
callDepth = CallDepth.forClass(Executor.class);
246+
if (callDepth.getAndIncrement() > 0) {
247+
return Collections.emptyList();
248+
}
249+
215250
Context context = Java8BytecodeBridge.currentContext();
216251
for (Callable<?> task : tasks) {
217252
if (ExecutorAdviceHelper.shouldPropagateContext(context, task)) {
@@ -228,7 +263,13 @@ public static Collection<?> submitEnter(
228263

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

0 commit comments

Comments
 (0)