11package datadog .trace .instrumentation .jetty9 ;
22
3+ import datadog .context .Context ;
34import datadog .trace .api .gateway .Flow ;
45import datadog .trace .bootstrap .instrumentation .api .AgentSpan ;
56import datadog .trace .instrumentation .jetty .JettyBlockingHelper ;
2526 * </code> is replaced with: <code>
2627 * case REQUEST_DISPATCH:
2728 * // ...
29+ * AgentSpan span = Java8BytecodeBridge(context);
2830 * if (span != null && span.getBlockingAction() != null &&
2931 * JettyBlockingHelper.block(this.getRequest(), this.getResponse())) {
3032 * // nothing
@@ -82,7 +84,7 @@ public class HandleVisitor extends MethodVisitor {
8284 private static final Logger log = LoggerFactory .getLogger (HandleVisitor .class );
8385
8486 private boolean lookForStore ;
85- private int agentSpanVar = -1 ;
87+ private int contextVar = -1 ;
8688 private boolean success ;
8789 private final String methodName ;
8890
@@ -98,12 +100,12 @@ DelayCertainInsMethodVisitor delayVisitorDelegate() {
98100 @ Override
99101 public void visitMethodInsn (
100102 int opcode , String owner , String name , String descriptor , boolean isInterface ) {
101- if (agentSpanVar == -1 ) {
103+ if (contextVar == -1 ) {
102104 lookForStore =
103105 !lookForStore
104106 && opcode == Opcodes .INVOKEVIRTUAL
105107 && name .equals ("startSpan" )
106- && descriptor .endsWith ("Ldatadog/trace/bootstrap/instrumentation/api/AgentSpan ;" );
108+ && descriptor .endsWith ("Ldatadog.context.Context ;" );
107109 } else if (!success
108110 && opcode == Opcodes .INVOKEVIRTUAL
109111 && owner .equals ("org/eclipse/jetty/server/Server" )
@@ -128,9 +130,17 @@ public void visitMethodInsn(
128130 Label beforeHandle = new Label ();
129131 Label afterHandle = new Label ();
130132
131- super .visitVarInsn (Opcodes .ALOAD , agentSpanVar );
133+ super .visitVarInsn (Opcodes .ALOAD , contextVar );
134+ super .visitMethodInsn (
135+ Opcodes .INVOKESTATIC ,
136+ "datadog/trace/bootstrap/instrumentation/api/Java8BytecodeBridge" ,
137+ "spanFromContext" ,
138+ "(" + Type .getDescriptor (Context .class ) + ")" + Type .getDescriptor (AgentSpan .class ),
139+ false );
140+ super .visitVarInsn (Opcodes .ASTORE , contextVar );
141+ super .visitVarInsn (Opcodes .ALOAD , contextVar );
132142 super .visitJumpInsn (Opcodes .IFNULL , beforeHandle );
133- super .visitVarInsn (Opcodes .ALOAD , agentSpanVar );
143+ super .visitVarInsn (Opcodes .ALOAD , contextVar );
134144 super .visitMethodInsn (
135145 Opcodes .INVOKEINTERFACE ,
136146 "datadog/trace/bootstrap/instrumentation/api/AgentSpan" ,
@@ -156,14 +166,14 @@ public void visitMethodInsn(
156166 "getResponse" ,
157167 "()Lorg/eclipse/jetty/server/Response;" ,
158168 false );
159- super .visitVarInsn (Opcodes .ALOAD , agentSpanVar );
169+ super .visitVarInsn (Opcodes .ALOAD , contextVar );
160170 super .visitMethodInsn (
161171 Opcodes .INVOKEINTERFACE ,
162172 "datadog/trace/bootstrap/instrumentation/api/AgentSpan" ,
163173 "getRequestBlockingAction" ,
164174 "()" + Type .getDescriptor (Flow .Action .RequestBlockingAction .class ),
165175 true );
166- super .visitVarInsn (Opcodes .ALOAD , agentSpanVar );
176+ super .visitVarInsn (Opcodes .ALOAD , contextVar );
167177 super .visitMethodInsn (
168178 Opcodes .INVOKESTATIC ,
169179 Type .getInternalName (JettyBlockingHelper .class ),
@@ -214,9 +224,9 @@ public void visitMethodInsn(
214224 Label beforeRegularDispatch = new Label ();
215225 Label afterRegularDispatch = new Label ();
216226
217- super .visitVarInsn (Opcodes .ALOAD , agentSpanVar );
227+ super .visitVarInsn (Opcodes .ALOAD , contextVar );
218228 super .visitJumpInsn (Opcodes .IFNULL , beforeRegularDispatch );
219- super .visitVarInsn (Opcodes .ALOAD , agentSpanVar );
229+ super .visitVarInsn (Opcodes .ALOAD , contextVar );
220230 super .visitMethodInsn (
221231 Opcodes .INVOKEINTERFACE ,
222232 "datadog/trace/bootstrap/instrumentation/api/AgentSpan" ,
@@ -248,14 +258,14 @@ public void visitMethodInsn(
248258 "getResponse" ,
249259 "()Lorg/eclipse/jetty/server/Response;" ,
250260 false );
251- super .visitVarInsn (Opcodes .ALOAD , agentSpanVar );
261+ super .visitVarInsn (Opcodes .ALOAD , contextVar );
252262 super .visitMethodInsn (
253263 Opcodes .INVOKEINTERFACE ,
254264 "datadog/trace/bootstrap/instrumentation/api/AgentSpan" ,
255265 "getRequestBlockingAction" ,
256266 "()" + Type .getDescriptor (Flow .Action .RequestBlockingAction .class ),
257267 true );
258- super .visitVarInsn (Opcodes .ALOAD , agentSpanVar );
268+ super .visitVarInsn (Opcodes .ALOAD , contextVar );
259269
260270 // create the lambda
261271 super .visitInvokeDynamicInsn (
@@ -337,7 +347,7 @@ private boolean checkDispatchMethodState(final List<Function> savedVisitations)
337347 @ Override
338348 public void visitVarInsn (int opcode , int varIndex ) {
339349 if (lookForStore && opcode == Opcodes .ASTORE ) {
340- agentSpanVar = varIndex ;
350+ contextVar = varIndex ;
341351 lookForStore = false ;
342352 }
343353
0 commit comments