22
33import static net .bytebuddy .jar .asm .Opcodes .ALOAD ;
44import static net .bytebuddy .jar .asm .Opcodes .F_SAME ;
5- import static net .bytebuddy .jar .asm .Opcodes .GOTO ;
65import static net .bytebuddy .jar .asm .Opcodes .H_INVOKESTATIC ;
6+ import static net .bytebuddy .jar .asm .Opcodes .IFEQ ;
77import static net .bytebuddy .jar .asm .Opcodes .IFNE ;
88import static net .bytebuddy .jar .asm .Opcodes .INVOKESTATIC ;
99import 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