Skip to content

Commit 90e85d5

Browse files
committed
feat(asm): Improve block check injection
Clean up local var hook and use Context.current() instead Refactor helper to use Context instead of AgentSpan and RequestBlockingAction
1 parent e0ed96a commit 90e85d5

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

dd-java-agent/instrumentation/jetty-common/src/main/java/datadog/trace/instrumentation/jetty/JettyBlockingHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ public static boolean block(Request request, Response response, Context context)
242242
rba.getExtraHeaders());
243243
}
244244

245+
public static boolean hasRequestBlockingAction(Context context) {
246+
AgentSpan span = Java8BytecodeBridge.spanFromContext(context);
247+
return span != null && span.getRequestBlockingAction() != null;
248+
}
249+
245250
public static void blockAndThrowOnFailure(Request request, Response response, Context context) {
246251
if (!block(request, response, context)) {
247252
throw new BlockingException("Throwing after being unable to commit blocking response");

dd-java-agent/instrumentation/jetty-common/src/main/java/datadog/trace/instrumentation/jetty9/HandleVisitor.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import static net.bytebuddy.jar.asm.Opcodes.ALOAD;
44
import static net.bytebuddy.jar.asm.Opcodes.F_SAME;
5-
import static net.bytebuddy.jar.asm.Opcodes.GOTO;
65
import static net.bytebuddy.jar.asm.Opcodes.H_INVOKESTATIC;
6+
import static net.bytebuddy.jar.asm.Opcodes.IFEQ;
77
import static net.bytebuddy.jar.asm.Opcodes.IFNE;
88
import static net.bytebuddy.jar.asm.Opcodes.INVOKESTATIC;
99
import static net.bytebuddy.jar.asm.Opcodes.INVOKEVIRTUAL;
@@ -275,6 +275,7 @@ public void visitMethodInsn(
275275
return;
276276
}
277277

278+
// TODO Could be moved to #checkDispatchMethodState
278279
DelayCertainInsMethodVisitor.GetStaticFieldInsn getStaticFieldInsn =
279280
(DelayCertainInsMethodVisitor.GetStaticFieldInsn) savedVisitations.get(1);
280281
if ((!getStaticFieldInsn.owner.equals("javax/servlet/DispatcherType")
@@ -284,9 +285,26 @@ public void visitMethodInsn(
284285
return;
285286
}
286287

287-
Label doBlockLabel = new Label();
288+
// Label doBlockLabel = new Label();
288289
Label beforeRegularDispatch = new Label();
289-
Label afterRegularDispatch = new Label();
290+
// Label afterRegularDispatch = new Label();
291+
292+
// Add current context to the stack
293+
super.visitMethodInsn(
294+
INVOKESTATIC,
295+
Type.getInternalName(Java8BytecodeBridge.class),
296+
"getCurrentContext",
297+
"()Ldatadog/context/Context;",
298+
false);
299+
// Call JettyBlockingHelper.hasRequestBlockingAction(context)
300+
super.visitMethodInsn(
301+
INVOKESTATIC,
302+
Type.getInternalName(JettyBlockingHelper.class),
303+
"hasRequestBlockingAction",
304+
"(" + Type.getDescriptor(Context.class) + ")Z",
305+
false);
306+
// If no request blocking action, jump befor the regular dispatch
307+
super.visitJumpInsn(IFEQ, beforeRegularDispatch);
290308

291309
// super.visitVarInsn(ALOAD, CONTEXT_VAR);
292310
// super.visitJumpInsn(Opcodes.IFNULL, beforeRegularDispatch);
@@ -300,14 +318,13 @@ public void visitMethodInsn(
300318
// super.visitJumpInsn(Opcodes.IFNONNULL, doBlockLabel);
301319
// super.visitJumpInsn(GOTO, beforeRegularDispatch);
302320

303-
super.visitLabel(doBlockLabel);
304-
super.visitFrame(F_SAME, 0, null, 0, null);
321+
// super.visitLabel(doBlockLabel);
322+
// super.visitFrame(F_SAME, 0, null, 0, null);
305323
// dispatch with a Dispatchable created from JettyBlockingHelper::block
306324
// first set up the first two arguments to dispatch (this and DispatcherType.REQUEST)
307325
List<Runnable> loadThisAndEnum = new ArrayList<>(savedVisitations.subList(0, 2));
308326
mv.commitVisitations(loadThisAndEnum);
309-
// set up the arguments to the method underlying the lambda (Request, Response,
310-
// RequestBlockingAction, AgentSpan)
327+
// set up the arguments to the method underlying the lambda (Request, Response, Context)
311328
super.visitVarInsn(ALOAD, 0);
312329
super.visitMethodInsn(
313330
INVOKEVIRTUAL,
@@ -369,14 +386,14 @@ public void visitMethodInsn(
369386
// invoke the dispatch method
370387
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
371388

372-
super.visitJumpInsn(GOTO, afterRegularDispatch);
389+
// super.visitJumpInsn(GOTO, afterRegularDispatch);
373390

374391
super.visitLabel(beforeRegularDispatch);
375392
super.visitFrame(F_SAME, 0, null, 0, null);
376393
mv.commitVisitations(savedVisitations);
377394
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
378-
super.visitLabel(afterRegularDispatch);
379-
super.visitFrame(F_SAME, 0, null, 0, null);
395+
// super.visitLabel(afterRegularDispatch);
396+
// super.visitFrame(F_SAME, 0, null, 0, null);
380397
this.success = true;
381398
return;
382399
}

0 commit comments

Comments
 (0)