Skip to content

Commit be57c26

Browse files
committed
feat(asm): Clean up visitors
1 parent 365bc7f commit be57c26

File tree

2 files changed

+19
-198
lines changed

2 files changed

+19
-198
lines changed

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

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
import datadog.context.Context;
66
import datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge;
7-
import java.io.BufferedWriter;
8-
import java.io.IOException;
97
import java.util.List;
108
import net.bytebuddy.jar.asm.Label;
119
import net.bytebuddy.jar.asm.MethodVisitor;
@@ -19,7 +17,7 @@
1917
* and replaces it with:
2018
*
2119
* <pre>
22-
* if (JettyBlockingHelper.block(this.getRequest(), this.getResponse(), context) {
20+
* if (JettyBlockingHelper.block(this.getRequest(), this.getResponse(), Java8BytecodeBridge.getCurrentContext()) {
2321
* // nothing
2422
* } else {
2523
* server.handle(this);
@@ -31,25 +29,10 @@
3129
public class HandleRequestVisitor extends MethodVisitor {
3230
private static final Logger log = LoggerFactory.getLogger(HandleRequestVisitor.class);
3331

34-
// private boolean lookForStore;
35-
// private int contextVar = -1;
3632
private final int classVersion;
3733
private final String connClassInternalName;
3834
private boolean success;
3935

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-
5336
public HandleRequestVisitor(
5437
int api,
5538
int classVersion,
@@ -58,18 +41,6 @@ public HandleRequestVisitor(
5841
super(api, methodVisitor);
5942
this.classVersion = classVersion;
6043
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-
// }
7344
}
7445

7546
DelayLoadsMethodVisitor delayVisitorDelegate() {
@@ -79,30 +50,12 @@ DelayLoadsMethodVisitor delayVisitorDelegate() {
7950
@Override
8051
public void visitMethodInsn(
8152
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
8953
if (opcode == Opcodes.INVOKEVIRTUAL
9054
&& owner.equals("org/eclipse/jetty/server/Server")
9155
&& name.equals("handle")
9256
&& descriptor.equals("(L" + this.connClassInternalName + ";)V")) {
9357
DelayLoadsMethodVisitor mv = delayVisitorDelegate();
9458
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());
10659
if (savedLoads.size() != 2) {
10760
mv.commitLoads(savedLoads);
10861
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
@@ -111,6 +64,7 @@ public void visitMethodInsn(
11164

11265
Label afterHandle = new Label();
11366

67+
// Add Request, Response and Context onto the stack
11468
super.visitVarInsn(Opcodes.ALOAD, 0);
11569
super.visitMethodInsn(
11670
Opcodes.INVOKEVIRTUAL,
@@ -132,8 +86,7 @@ public void visitMethodInsn(
13286
"getCurrentContext",
13387
"()Ldatadog/context/Context;",
13488
false);
135-
// super.visitVarInsn(Opcodes.ALOAD, contextVar);
136-
89+
// Call JettyBlockingHelper.block(request, response, context)
13790
super.visitMethodInsn(
13891
Opcodes.INVOKESTATIC,
13992
Type.getInternalName(JettyBlockingHelper.class),
@@ -142,10 +95,13 @@ public void visitMethodInsn(
14295
+ Type.getDescriptor(Context.class)
14396
+ ")Z",
14497
false);
98+
// Jump after handle if blocked
14599
super.visitJumpInsn(Opcodes.IFNE, afterHandle);
146100

101+
// Inject default handle instructions
147102
mv.commitLoads(savedLoads);
148103
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
104+
// Add after handle label
149105
super.visitLabel(afterHandle);
150106
if (needsStackFrames()) {
151107
super.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
@@ -156,29 +112,13 @@ public void visitMethodInsn(
156112

157113
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
158114
}
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-
// }
169115

170116
@Override
171117
public void visitEnd() {
172118
if (!success) {
173119
log.warn(
174120
"Transformation of Jetty's connection class was not successful. Blocking will likely not work");
175121
}
176-
if (this.debugWriter != null) {
177-
try {
178-
this.debugWriter.close();
179-
} catch (IOException ignored) {
180-
}
181-
}
182122
super.visitEnd();
183123
}
184124

0 commit comments

Comments
 (0)