Strict context check for tomcat and jetty
MODIFY
instrumentation/servlet/servlet-3.0/javaagent/src/test/groovy/AbstractServlet3Test.groovy
instrumentation/servlet/servlet-3.0/javaagent/src/test/groovy/AbstractServlet3Test.groovy
@@ -10,11 +10,13 @@ import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEn
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.QUERY_PARAM
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.REDIRECT
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.SUCCESS
+import static org.awaitility.Awaitility.await
import io.opentelemetry.instrumentation.test.AgentTestTrait
import io.opentelemetry.instrumentation.test.asserts.TraceAssert
import io.opentelemetry.instrumentation.test.base.HttpServerTest
import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpRequest
+import java.util.concurrent.TimeUnit
import javax.servlet.Servlet
abstract class AbstractServlet3Test<SERVER, CONTEXT> extends HttpServerTest<SERVER> implements AgentTestTrait {
@@ -45,6 +47,25 @@ abstract class AbstractServlet3Test<SERVER, CONTEXT> extends HttpServerTest<SERV
addServlet(context, INDEXED_CHILD.path, servlet)
}
+ def cleanup() {
+ // wait for async request threads to complete
+ await()
+ .atMost(15, TimeUnit.SECONDS)
+ .until({ !isRequestRunning() })
+ }
+
+ static boolean isRequestRunning() {
+ def result = Thread.getAllStackTraces().values().find {stackTrace ->
+ def element = stackTrace.find {
+ return ((it.className == "org.apache.catalina.core.AsyncContextImpl\$RunnableWrapper" && it.methodName == "run")
+ || ((it.className == "org.eclipse.jetty.server.AsyncContinuation\$1" && it.methodName == "run"))
+ || ((it.className == "org.eclipse.jetty.server.AsyncContextState\$1" && it.methodName == "run")))
+ }
+ element != null
+ }
+ return result != null
+ }
+
protected ServerEndpoint lastRequest
@Override

MODIFY
instrumentation/servlet/servlet-5.0/javaagent/build.gradle.kts
instrumentation/servlet/servlet-5.0/javaagent/build.gradle.kts
@@ -21,7 +21,3 @@ dependencies {
testLibrary("org.apache.tomcat.embed:tomcat-embed-core:10.0.0")
testLibrary("org.apache.tomcat.embed:tomcat-embed-jasper:10.0.0")
}
-
-tasks.withType<Test>().configureEach {
- jvmArgs("-Dio.opentelemetry.javaagent.shaded.io.opentelemetry.context.enableStrictContext=false")
-}

MODIFY
instrumentation/servlet/servlet-5.0/javaagent/src/test/groovy/AbstractServlet5Test.groovy
instrumentation/servlet/servlet-5.0/javaagent/src/test/groovy/AbstractServlet5Test.groovy
@@ -10,12 +10,14 @@ import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEn
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.QUERY_PARAM
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.REDIRECT
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.SUCCESS
+import static org.awaitility.Awaitility.await
import io.opentelemetry.instrumentation.test.AgentTestTrait
import io.opentelemetry.instrumentation.test.asserts.TraceAssert
import io.opentelemetry.instrumentation.test.base.HttpServerTest
import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpRequest
import jakarta.servlet.Servlet
+import java.util.concurrent.TimeUnit
abstract class AbstractServlet5Test<SERVER, CONTEXT> extends HttpServerTest<SERVER> implements AgentTestTrait {
@Override
@@ -45,6 +47,24 @@ abstract class AbstractServlet5Test<SERVER, CONTEXT> extends HttpServerTest<SERV
addServlet(context, INDEXED_CHILD.path, servlet)
}
+ def cleanup() {
+ // wait for async request threads to complete
+ await()
+ .atMost(15, TimeUnit.SECONDS)
+ .until({ !isRequestRunning() })
+ }
+
+ static boolean isRequestRunning() {
+ def result = Thread.getAllStackTraces().values().find {stackTrace ->
+ def element = stackTrace.find {
+ return ((it.className == "org.apache.catalina.core.AsyncContextImpl\$RunnableWrapper" && it.methodName == "run")
+ || ((it.className == "org.eclipse.jetty.server.AsyncContextState\$1" && it.methodName == "run")))
+ }
+ element != null
+ }
+ return result != null
+ }
+
protected ServerEndpoint lastRequest
@Override

MODIFY
instrumentation/tomcat/tomcat-10.0/javaagent/build.gradle.kts
instrumentation/tomcat/tomcat-10.0/javaagent/build.gradle.kts
@@ -17,7 +17,3 @@ dependencies {
// Make sure nothing breaks due to both 7.0 and 10.0 modules being present together
testInstrumentation(project(":instrumentation:tomcat:tomcat-7.0:javaagent"))
}
-
-tasks.withType<Test>().configureEach {
- jvmArgs("-Dio.opentelemetry.javaagent.shaded.io.opentelemetry.context.enableStrictContext=false")
-}

MODIFY
instrumentation/tomcat/tomcat-10.0/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/tomcat/v10_0/TomcatAsyncTest.groovy
instrumentation/tomcat/tomcat-10.0/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/tomcat/v10_0/TomcatAsyncTest.groovy
@@ -13,6 +13,7 @@ import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEn
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.QUERY_PARAM
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.REDIRECT
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.SUCCESS
+import static org.awaitility.Awaitility.await
import io.opentelemetry.instrumentation.test.AgentTestTrait
import io.opentelemetry.instrumentation.test.asserts.TraceAssert
@@ -20,6 +21,7 @@ import io.opentelemetry.instrumentation.test.base.HttpServerTest
import jakarta.servlet.Servlet
import jakarta.servlet.ServletException
import java.nio.file.Files
+import java.util.concurrent.TimeUnit
import org.apache.catalina.Context
import org.apache.catalina.startup.Tomcat
import org.apache.tomcat.JarScanFilter
@@ -67,6 +69,23 @@ class TomcatAsyncTest extends HttpServerTest<Tomcat> implements AgentTestTrait {
server.destroy()
}
+ def cleanup() {
+ // wait for async request threads to complete
+ await()
+ .atMost(15, TimeUnit.SECONDS)
+ .until({ !isRequestRunning() })
+ }
+
+ static boolean isRequestRunning() {
+ def result = Thread.getAllStackTraces().values().find {stackTrace ->
+ def element = stackTrace.find {
+ return it.className == "org.apache.catalina.core.AsyncContextImpl\$RunnableWrapper" && it.methodName == "run"
+ }
+ element != null
+ }
+ return result != null
+ }
+
@Override
String getContextPath() {
return "/tomcat-context"

MODIFY
instrumentation/tomcat/tomcat-7.0/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/tomcat/v7_0/TomcatAsyncTest.groovy
instrumentation/tomcat/tomcat-7.0/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/tomcat/v7_0/TomcatAsyncTest.groovy
@@ -13,11 +13,13 @@ import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEn
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.QUERY_PARAM
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.REDIRECT
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.SUCCESS
+import static org.awaitility.Awaitility.await
import io.opentelemetry.instrumentation.test.AgentTestTrait
import io.opentelemetry.instrumentation.test.asserts.TraceAssert
import io.opentelemetry.instrumentation.test.base.HttpServerTest
import java.nio.file.Files
+import java.util.concurrent.TimeUnit
import javax.servlet.Servlet
import javax.servlet.ServletException
import org.apache.catalina.Context
@@ -26,7 +28,6 @@ import org.apache.tomcat.JarScanFilter
import org.apache.tomcat.JarScanType
import spock.lang.Unroll
-
@Unroll
class TomcatAsyncTest extends HttpServerTest<Tomcat> implements AgentTestTrait {
@@ -68,6 +69,23 @@ class TomcatAsyncTest extends HttpServerTest<Tomcat> implements AgentTestTrait {
server.destroy()
}
+ def cleanup() {
+ // wait for async request threads to complete
+ await()
+ .atMost(15, TimeUnit.SECONDS)
+ .until({ !isRequestRunning() })
+ }
+
+ static boolean isRequestRunning() {
+ def result = Thread.getAllStackTraces().values().find {stackTrace ->
+ def element = stackTrace.find {
+ return it.className == "org.apache.catalina.core.AsyncContextImpl\$RunnableWrapper" && it.methodName == "run"
+ }
+ element != null
+ }
+ return result != null
+ }
+
@Override
String getContextPath() {
return "/tomcat-context"

Strict context check for tomcat and jetty (#3923)