4
4
5
5
import datadog .context .Context ;
6
6
import datadog .trace .bootstrap .instrumentation .api .Java8BytecodeBridge ;
7
- import java .io .BufferedWriter ;
8
- import java .io .IOException ;
9
7
import java .util .List ;
10
8
import net .bytebuddy .jar .asm .Label ;
11
9
import net .bytebuddy .jar .asm .MethodVisitor ;
19
17
* and replaces it with:
20
18
*
21
19
* <pre>
22
- * if (JettyBlockingHelper.block(this.getRequest(), this.getResponse(), context ) {
20
+ * if (JettyBlockingHelper.block(this.getRequest(), this.getResponse(), Java8BytecodeBridge.getCurrentContext() ) {
23
21
* // nothing
24
22
* } else {
25
23
* server.handle(this);
31
29
public class HandleRequestVisitor extends MethodVisitor {
32
30
private static final Logger log = LoggerFactory .getLogger (HandleRequestVisitor .class );
33
31
34
- // private boolean lookForStore;
35
- // private int contextVar = -1;
36
32
private final int classVersion ;
37
33
private final String connClassInternalName ;
38
34
private boolean success ;
39
35
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
-
53
36
public HandleRequestVisitor (
54
37
int api ,
55
38
int classVersion ,
@@ -58,18 +41,6 @@ public HandleRequestVisitor(
58
41
super (api , methodVisitor );
59
42
this .classVersion = classVersion ;
60
43
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
- // }
73
44
}
74
45
75
46
DelayLoadsMethodVisitor delayVisitorDelegate () {
@@ -79,30 +50,12 @@ DelayLoadsMethodVisitor delayVisitorDelegate() {
79
50
@ Override
80
51
public void visitMethodInsn (
81
52
int opcode , String owner , String name , String descriptor , boolean isInterface ) {
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
53
if (opcode == Opcodes .INVOKEVIRTUAL
90
54
&& owner .equals ("org/eclipse/jetty/server/Server" )
91
55
&& name .equals ("handle" )
92
56
&& descriptor .equals ("(L" + this .connClassInternalName + ";)V" )) {
93
57
DelayLoadsMethodVisitor mv = delayVisitorDelegate ();
94
58
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 ());
106
59
if (savedLoads .size () != 2 ) {
107
60
mv .commitLoads (savedLoads );
108
61
super .visitMethodInsn (opcode , owner , name , descriptor , isInterface );
@@ -111,6 +64,7 @@ public void visitMethodInsn(
111
64
112
65
Label afterHandle = new Label ();
113
66
67
+ // Add Request, Response and Context onto the stack
114
68
super .visitVarInsn (Opcodes .ALOAD , 0 );
115
69
super .visitMethodInsn (
116
70
Opcodes .INVOKEVIRTUAL ,
@@ -132,8 +86,7 @@ public void visitMethodInsn(
132
86
"getCurrentContext" ,
133
87
"()Ldatadog/context/Context;" ,
134
88
false );
135
- // super.visitVarInsn(Opcodes.ALOAD, contextVar);
136
-
89
+ // Call JettyBlockingHelper.block(request, response, context)
137
90
super .visitMethodInsn (
138
91
Opcodes .INVOKESTATIC ,
139
92
Type .getInternalName (JettyBlockingHelper .class ),
@@ -142,10 +95,13 @@ public void visitMethodInsn(
142
95
+ Type .getDescriptor (Context .class )
143
96
+ ")Z" ,
144
97
false );
98
+ // Jump after handle if blocked
145
99
super .visitJumpInsn (Opcodes .IFNE , afterHandle );
146
100
101
+ // Inject default handle instructions
147
102
mv .commitLoads (savedLoads );
148
103
super .visitMethodInsn (opcode , owner , name , descriptor , isInterface );
104
+ // Add after handle label
149
105
super .visitLabel (afterHandle );
150
106
if (needsStackFrames ()) {
151
107
super .visitFrame (Opcodes .F_SAME , 0 , null , 0 , null );
@@ -156,29 +112,13 @@ public void visitMethodInsn(
156
112
157
113
super .visitMethodInsn (opcode , owner , name , descriptor , isInterface );
158
114
}
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
- // }
169
115
170
116
@ Override
171
117
public void visitEnd () {
172
118
if (!success ) {
173
119
log .warn (
174
120
"Transformation of Jetty's connection class was not successful. Blocking will likely not work" );
175
121
}
176
- if (this .debugWriter != null ) {
177
- try {
178
- this .debugWriter .close ();
179
- } catch (IOException ignored ) {
180
- }
181
- }
182
122
super .visitEnd ();
183
123
}
184
124
0 commit comments