11package datadog .trace .instrumentation .jetty ;
22
3+ import static net .bytebuddy .jar .asm .Opcodes .INVOKESTATIC ;
4+
35import datadog .context .Context ;
6+ import datadog .trace .bootstrap .instrumentation .api .Java8BytecodeBridge ;
7+ import java .io .BufferedWriter ;
8+ import java .io .IOException ;
49import java .util .List ;
510import net .bytebuddy .jar .asm .Label ;
611import net .bytebuddy .jar .asm .MethodVisitor ;
2631public class HandleRequestVisitor extends MethodVisitor {
2732 private static final Logger log = LoggerFactory .getLogger (HandleRequestVisitor .class );
2833
29- private boolean lookForStore ;
30- private int contextVar = -1 ;
34+ // private boolean lookForStore;
35+ // private int contextVar = -1;
3136 private final int classVersion ;
3237 private final String connClassInternalName ;
3338 private boolean success ;
3439
40+ private BufferedWriter debugWriter ;
41+
42+ private void debug (String msg ) {
43+ if (debugWriter == null ) {
44+ return ;
45+ }
46+ try {
47+ debugWriter .write (msg );
48+ debugWriter .newLine ();
49+ } catch (IOException ignored ) {
50+ }
51+ }
52+
3553 public HandleRequestVisitor (
3654 int api ,
3755 int classVersion ,
@@ -40,6 +58,18 @@ public HandleRequestVisitor(
4058 super (api , methodVisitor );
4159 this .classVersion = classVersion ;
4260 this .connClassInternalName = connClassInternalName ;
61+
62+ // try {
63+ // String path =
64+ //
65+ // "/Users/bruce.bujon/go/src/github.com/DataDog/dd-trace-java/bbujon/debug/HandleRequestVisitor-"
66+ // + System.nanoTime()
67+ // + ".txt";
68+ // this.debugWriter = new BufferedWriter(new OutputStreamWriter(new
69+ // FileOutputStream(path)));
70+ // debug("Initializing");
71+ // } catch (IOException ignored) {
72+ // }
4373 }
4474
4575 DelayLoadsMethodVisitor delayVisitorDelegate () {
@@ -49,18 +79,30 @@ DelayLoadsMethodVisitor delayVisitorDelegate() {
4979 @ Override
5080 public void visitMethodInsn (
5181 int opcode , String owner , String name , String descriptor , boolean isInterface ) {
52- if (contextVar == -1 ) {
53- lookForStore =
54- !lookForStore
55- && opcode == Opcodes .INVOKEVIRTUAL
56- && name .equals ("startSpan" )
57- && descriptor .endsWith ("Ldatadog.context.Context;" );
58- } else if (opcode == Opcodes .INVOKEVIRTUAL
82+ // if (contextVar == -1) {
83+ // lookForStore =
84+ // !lookForStore
85+ // && opcode == Opcodes.INVOKEVIRTUAL
86+ // && name.equals("startSpan")
87+ // && descriptor.endsWith("Ldatadog.context.Context;");
88+ // } else
89+ if (opcode == Opcodes .INVOKEVIRTUAL
5990 && owner .equals ("org/eclipse/jetty/server/Server" )
6091 && name .equals ("handle" )
6192 && descriptor .equals ("(L" + this .connClassInternalName + ";)V" )) {
6293 DelayLoadsMethodVisitor mv = delayVisitorDelegate ();
6394 List <Integer > savedLoads = mv .transferLoads ();
95+ debug ("visitMethodInsn" );
96+ debug (
97+ "opcode: "
98+ + opcode
99+ + " owner: "
100+ + owner
101+ + " name: "
102+ + name
103+ + " descriptor: "
104+ + descriptor );
105+ debug ("savedLoads size: " + savedLoads .size ());
64106 if (savedLoads .size () != 2 ) {
65107 mv .commitLoads (savedLoads );
66108 super .visitMethodInsn (opcode , owner , name , descriptor , isInterface );
@@ -83,7 +125,15 @@ public void visitMethodInsn(
83125 "getResponse" ,
84126 "()Lorg/eclipse/jetty/server/Response;" ,
85127 false );
86- super .visitVarInsn (Opcodes .ALOAD , contextVar );
128+
129+ super .visitMethodInsn (
130+ INVOKESTATIC ,
131+ Type .getInternalName (Java8BytecodeBridge .class ),
132+ "getCurrentContext" ,
133+ "()Ldatadog/context/Context;" ,
134+ false );
135+ // super.visitVarInsn(Opcodes.ALOAD, contextVar);
136+
87137 super .visitMethodInsn (
88138 Opcodes .INVOKESTATIC ,
89139 Type .getInternalName (JettyBlockingHelper .class ),
@@ -106,23 +156,29 @@ public void visitMethodInsn(
106156
107157 super .visitMethodInsn (opcode , owner , name , descriptor , isInterface );
108158 }
109-
110- @ Override
111- public void visitVarInsn (int opcode , int varIndex ) {
112- if (lookForStore && opcode == Opcodes .ASTORE ) {
113- contextVar = varIndex ;
114- lookForStore = false ;
115- }
116-
117- super .visitVarInsn (opcode , varIndex );
118- }
159+ //
160+ // @Override
161+ // public void visitVarInsn(int opcode, int varIndex) {
162+ // if (lookForStore && opcode == Opcodes.ASTORE) {
163+ // contextVar = varIndex;
164+ // lookForStore = false;
165+ // }
166+ //
167+ // super.visitVarInsn(opcode, varIndex);
168+ // }
119169
120170 @ Override
121171 public void visitEnd () {
122172 if (!success ) {
123173 log .warn (
124174 "Transformation of Jetty's connection class was not successful. Blocking will likely not work" );
125175 }
176+ if (this .debugWriter != null ) {
177+ try {
178+ this .debugWriter .close ();
179+ } catch (IOException ignored ) {
180+ }
181+ }
126182 super .visitEnd ();
127183 }
128184
0 commit comments