1
1
package datadog .trace .instrumentation .jetty ;
2
2
3
+ import static net .bytebuddy .jar .asm .Opcodes .INVOKESTATIC ;
4
+
3
5
import datadog .context .Context ;
6
+ import datadog .trace .bootstrap .instrumentation .api .Java8BytecodeBridge ;
7
+ import java .io .BufferedWriter ;
8
+ import java .io .IOException ;
4
9
import java .util .List ;
5
10
import net .bytebuddy .jar .asm .Label ;
6
11
import net .bytebuddy .jar .asm .MethodVisitor ;
26
31
public class HandleRequestVisitor extends MethodVisitor {
27
32
private static final Logger log = LoggerFactory .getLogger (HandleRequestVisitor .class );
28
33
29
- private boolean lookForStore ;
30
- private int contextVar = -1 ;
34
+ // private boolean lookForStore;
35
+ // private int contextVar = -1;
31
36
private final int classVersion ;
32
37
private final String connClassInternalName ;
33
38
private boolean success ;
34
39
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
+
35
53
public HandleRequestVisitor (
36
54
int api ,
37
55
int classVersion ,
@@ -40,6 +58,18 @@ public HandleRequestVisitor(
40
58
super (api , methodVisitor );
41
59
this .classVersion = classVersion ;
42
60
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
+ // }
43
73
}
44
74
45
75
DelayLoadsMethodVisitor delayVisitorDelegate () {
@@ -49,18 +79,30 @@ DelayLoadsMethodVisitor delayVisitorDelegate() {
49
79
@ Override
50
80
public void visitMethodInsn (
51
81
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
59
90
&& owner .equals ("org/eclipse/jetty/server/Server" )
60
91
&& name .equals ("handle" )
61
92
&& descriptor .equals ("(L" + this .connClassInternalName + ";)V" )) {
62
93
DelayLoadsMethodVisitor mv = delayVisitorDelegate ();
63
94
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 ());
64
106
if (savedLoads .size () != 2 ) {
65
107
mv .commitLoads (savedLoads );
66
108
super .visitMethodInsn (opcode , owner , name , descriptor , isInterface );
@@ -83,7 +125,15 @@ public void visitMethodInsn(
83
125
"getResponse" ,
84
126
"()Lorg/eclipse/jetty/server/Response;" ,
85
127
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
+
87
137
super .visitMethodInsn (
88
138
Opcodes .INVOKESTATIC ,
89
139
Type .getInternalName (JettyBlockingHelper .class ),
@@ -106,23 +156,29 @@ public void visitMethodInsn(
106
156
107
157
super .visitMethodInsn (opcode , owner , name , descriptor , isInterface );
108
158
}
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
+ // }
119
169
120
170
@ Override
121
171
public void visitEnd () {
122
172
if (!success ) {
123
173
log .warn (
124
174
"Transformation of Jetty's connection class was not successful. Blocking will likely not work" );
125
175
}
176
+ if (this .debugWriter != null ) {
177
+ try {
178
+ this .debugWriter .close ();
179
+ } catch (IOException ignored ) {
180
+ }
181
+ }
126
182
super .visitEnd ();
127
183
}
128
184
0 commit comments