Copy servlet listener over to new context on dispatch.
This allows it to finish the span with the correct response.
MODIFY
dd-java-agent/instrumentation/servlet-3/src/main/java/datadog/trace/instrumentation/servlet3/TagSettingAsyncListener.java
dd-java-agent/instrumentation/servlet-3/src/main/java/datadog/trace/instrumentation/servlet3/TagSettingAsyncListener.java
@@ -58,9 +58,9 @@ public class TagSettingAsyncListener implements AsyncListener {
}
}
- /** Finish current span on dispatch. New listener will be attached by Servlet3Advice */
+ /** Transfer the listener over to the new context. */
@Override
public void onStartAsync(final AsyncEvent event) throws IOException {
- onComplete(event);
+ event.getAsyncContext().addListener(this);
}
}

MODIFY
dd-java-agent/instrumentation/servlet-3/src/test/groovy/JettyServlet3Test.groovy
dd-java-agent/instrumentation/servlet-3/src/test/groovy/JettyServlet3Test.groovy
@@ -137,25 +137,24 @@ class JettyServlet3TestDispatchImmediate extends JettyDispatchTest {
}
}
-// TODO: Behavior in this test is pretty inconsistent with expectations. Fix and reenable.
-//class JettyServlet3TestDispatchAsync extends JettyDispatchTest {
-// @Override
-// Class<Servlet> servlet() {
-// TestServlet3.Async
-// }
-//
-// @Override
-// protected void setupServlets(ServletContextHandler context) {
-// super.setupServlets(context)
-//
-// addServlet(context, "/dispatch" + SUCCESS.path, TestServlet3.DispatchAsync)
-// addServlet(context, "/dispatch" + ERROR.path, TestServlet3.DispatchAsync)
-// addServlet(context, "/dispatch" + EXCEPTION.path, TestServlet3.DispatchAsync)
-// addServlet(context, "/dispatch" + REDIRECT.path, TestServlet3.DispatchAsync)
-// addServlet(context, "/dispatch" + AUTH_REQUIRED.path, TestServlet3.DispatchAsync)
-// addServlet(context, "/dispatch/recursive", TestServlet3.DispatchRecursive)
-// }
-//}
+class JettyServlet3TestDispatchAsync extends JettyDispatchTest {
+ @Override
+ Class<Servlet> servlet() {
+ TestServlet3.Async
+ }
+
+ @Override
+ protected void setupServlets(ServletContextHandler context) {
+ super.setupServlets(context)
+
+ addServlet(context, "/dispatch" + SUCCESS.path, TestServlet3.DispatchAsync)
+ addServlet(context, "/dispatch" + ERROR.path, TestServlet3.DispatchAsync)
+ addServlet(context, "/dispatch" + EXCEPTION.path, TestServlet3.DispatchAsync)
+ addServlet(context, "/dispatch" + REDIRECT.path, TestServlet3.DispatchAsync)
+ addServlet(context, "/dispatch" + AUTH_REQUIRED.path, TestServlet3.DispatchAsync)
+ addServlet(context, "/dispatch/recursive", TestServlet3.DispatchRecursive)
+ }
+}
abstract class JettyDispatchTest extends JettyServlet3Test {
@Override

MODIFY
dd-java-agent/instrumentation/servlet-3/src/test/groovy/TomcatServlet3Test.groovy
dd-java-agent/instrumentation/servlet-3/src/test/groovy/TomcatServlet3Test.groovy
@@ -178,25 +178,24 @@ class TomcatServlet3TestDispatchImmediate extends TomcatDispatchTest {
}
}
-// TODO: Behavior in this test is pretty inconsistent with expectations. Fix and reenable.
-//class TomcatServlet3TestDispatchAsync extends TomcatDispatchTest {
-// @Override
-// Class<Servlet> servlet() {
-// TestServlet3.Async
-// }
-//
-// @Override
-// protected void setupServlets(Context context) {
-// super.setupServlets(context)
-//
-// addServlet(context, "/dispatch" + SUCCESS.path, TestServlet3.DispatchAsync)
-// addServlet(context, "/dispatch" + ERROR.path, TestServlet3.DispatchAsync)
-// addServlet(context, "/dispatch" + EXCEPTION.path, TestServlet3.DispatchAsync)
-// addServlet(context, "/dispatch" + REDIRECT.path, TestServlet3.DispatchAsync)
-// addServlet(context, "/dispatch" + AUTH_REQUIRED.path, TestServlet3.DispatchAsync)
-// addServlet(context, "/dispatch/recursive", TestServlet3.DispatchRecursive)
-// }
-//}
+class TomcatServlet3TestDispatchAsync extends TomcatDispatchTest {
+ @Override
+ Class<Servlet> servlet() {
+ TestServlet3.Async
+ }
+
+ @Override
+ protected void setupServlets(Context context) {
+ super.setupServlets(context)
+
+ addServlet(context, "/dispatch" + SUCCESS.path, TestServlet3.DispatchAsync)
+ addServlet(context, "/dispatch" + ERROR.path, TestServlet3.DispatchAsync)
+ addServlet(context, "/dispatch" + EXCEPTION.path, TestServlet3.DispatchAsync)
+ addServlet(context, "/dispatch" + REDIRECT.path, TestServlet3.DispatchAsync)
+ addServlet(context, "/dispatch" + AUTH_REQUIRED.path, TestServlet3.DispatchAsync)
+ addServlet(context, "/dispatch/recursive", TestServlet3.DispatchRecursive)
+ }
+}
abstract class TomcatDispatchTest extends TomcatServlet3Test {
@Override

Copy servlet listener over to new context on dispatch.

This allows it to finish the span with the correct response.