Fix latestDepTests for Jetty
MODIFY
dd-java-agent/instrumentation/jetty-8/src/test/groovy/JettyHandlerTest.groovy
dd-java-agent/instrumentation/jetty-8/src/test/groovy/JettyHandlerTest.groovy
import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.agent.test.TestUtils
import datadog.trace.agent.test.utils.OkHttpUtils
-import datadog.trace.api.Config
import datadog.trace.api.DDSpanTypes
import okhttp3.OkHttpClient
import org.eclipse.jetty.continuation.Continuation
@@ -11,9 +10,11 @@ import org.eclipse.jetty.server.Request
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.server.handler.AbstractHandler
+import javax.servlet.DispatcherType
import javax.servlet.ServletException
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
+import java.util.concurrent.atomic.AtomicBoolean
class JettyHandlerTest extends AgentTestRunner {
@@ -55,29 +56,29 @@ class JettyHandlerTest extends AgentTestRunner {
expect:
response.body().string().trim() == "Hello World"
- TEST_WRITER.waitForTraces(1)
- TEST_WRITER.size() == 1
- def trace = TEST_WRITER.firstTrace()
- trace.size() == 1
- def context = trace[0].context()
- context.serviceName == "unnamed-java-app"
- context.operationName == "jetty.request"
- context.resourceName == "GET ${handler.class.name}"
- context.spanType == DDSpanTypes.HTTP_SERVER
- !context.getErrorFlag()
- context.parentId == "0"
- def tags = context.tags
- tags["http.url"] == "http://localhost:$port/"
- tags["http.method"] == "GET"
- tags["span.kind"] == "server"
- tags["span.type"] == DDSpanTypes.HTTP_SERVER
- tags["component"] == "jetty-handler"
- tags["http.status_code"] == 200
- tags["thread.name"] != null
- tags["thread.id"] != null
- tags[Config.RUNTIME_ID_TAG] == Config.get().runtimeId
- tags["span.origin.type"] == handler.class.name
- tags.size() == 10
+
+ assertTraces(1) {
+ trace(0, 1) {
+ span(0) {
+ serviceName "unnamed-java-app"
+ operationName "jetty.request"
+ resourceName "GET ${handler.class.name}"
+ spanType DDSpanTypes.HTTP_SERVER
+ errored false
+ parent()
+ tags {
+ "http.url" "http://localhost:$port/"
+ "http.method" "GET"
+ "span.kind" "server"
+ "component" "jetty-handler"
+ "span.origin.type" handler.class.name
+ "span.type" DDSpanTypes.HTTP_SERVER
+ "http.status_code" 200
+ defaultTags()
+ }
+ }
+ }
+ }
}
def "handler instrumentation clears state after async request"() {
@@ -127,12 +128,18 @@ class JettyHandlerTest extends AgentTestRunner {
def "call to jetty with error creates a trace"() {
setup:
+ def errorHandlerCalled = new AtomicBoolean(false)
Handler handler = new AbstractHandler() {
@Override
void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
+ if (baseRequest.dispatcherType == DispatcherType.ERROR) {
+ errorHandlerCalled.set(true)
+ baseRequest.setHandled(true)
+ } else {
throw new RuntimeException()
}
}
+ }
server.setHandler(handler)
server.start()
def request = new okhttp3.Request.Builder()
@@ -143,31 +150,52 @@ class JettyHandlerTest extends AgentTestRunner {
expect:
response.body().string().trim() == ""
- TEST_WRITER.waitForTraces(1)
- TEST_WRITER.size() == 1
- def trace = TEST_WRITER.firstTrace()
- trace.size() == 1
- def context = trace[0].context()
- context.serviceName == "unnamed-java-app"
- context.operationName == "jetty.request"
- context.resourceName == "GET ${handler.class.name}"
- context.spanType == DDSpanTypes.HTTP_SERVER
- context.getErrorFlag()
- context.parentId == "0"
- def tags = context.tags
- tags["http.url"] == "http://localhost:$port/"
- tags["http.method"] == "GET"
- tags["span.kind"] == "server"
- tags["span.type"] == DDSpanTypes.HTTP_SERVER
- tags["component"] == "jetty-handler"
- tags["http.status_code"] == 500
- tags["thread.name"] != null
- tags["thread.id"] != null
- tags[Config.RUNTIME_ID_TAG] == Config.get().runtimeId
- tags["span.origin.type"] == handler.class.name
- tags["error"] == true
- tags["error.type"] == RuntimeException.name
- tags["error.stack"] != null
- tags.size() == 13
+
+ assertTraces(errorHandlerCalled.get() ? 2 : 1) {
+ trace(0, 1) {
+ span(0) {
+ serviceName "unnamed-java-app"
+ operationName "jetty.request"
+ resourceName "GET ${handler.class.name}"
+ spanType DDSpanTypes.HTTP_SERVER
+ errored true
+ parent()
+ tags {
+ "http.url" "http://localhost:$port/"
+ "http.method" "GET"
+ "span.kind" "server"
+ "component" "jetty-handler"
+ "span.origin.type" handler.class.name
+ "span.type" DDSpanTypes.HTTP_SERVER
+ "http.status_code" 500
+ errorTags RuntimeException
+ defaultTags()
+ }
+ }
+ }
+ if (errorHandlerCalled.get()) {
+ trace(1, 1) {
+ span(0) {
+ serviceName "unnamed-java-app"
+ operationName "jetty.request"
+ resourceName "GET ${handler.class.name}"
+ spanType DDSpanTypes.HTTP_SERVER
+ errored true
+ parent()
+ tags {
+ "http.url" "http://localhost:$port/"
+ "http.method" "GET"
+ "span.kind" "server"
+ "component" "jetty-handler"
+ "span.origin.type" handler.class.name
+ "span.type" DDSpanTypes.HTTP_SERVER
+ "http.status_code" 500
+ "error" true
+ defaultTags()
+ }
+ }
+ }
+ }
+ }
}
}

MODIFY
dd-java-agent/instrumentation/servlet-3/src/test/groovy/JettyServlet3Test.groovy
dd-java-agent/instrumentation/servlet-3/src/test/groovy/JettyServlet3Test.groovy
@@ -7,7 +7,6 @@ import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
-import org.eclipse.jetty.http.HttpHeaders
import org.eclipse.jetty.security.ConstraintMapping
import org.eclipse.jetty.security.ConstraintSecurityHandler
import org.eclipse.jetty.security.HashLoginService
@@ -16,6 +15,7 @@ import org.eclipse.jetty.security.authentication.BasicAuthenticator
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.ServletContextHandler
import org.eclipse.jetty.util.security.Constraint
+import spock.lang.Shared
class JettyServlet3Test extends AgentTestRunner {
@@ -29,11 +29,14 @@ class JettyServlet3Test extends AgentTestRunner {
})
.build()
+ @Shared
int port
+ @Shared
private Server jettyServer
+ @Shared
private ServletContextHandler servletContext
- def setup() {
+ def setupSpec() {
port = TestUtils.randomOpenPort()
jettyServer = new Server(port)
servletContext = new ServletContextHandler()
@@ -53,7 +56,7 @@ class JettyServlet3Test extends AgentTestRunner {
"Jetty server: http://localhost:" + port + "/")
}
- def cleanup() {
+ def cleanupSpec() {
jettyServer.stop()
jettyServer.destroy()
}
@@ -68,7 +71,7 @@ class JettyServlet3Test extends AgentTestRunner {
requestBuilder.header("x-datadog-parent-id", "456")
}
if (auth) {
- requestBuilder.header(HttpHeaders.AUTHORIZATION, Credentials.basic("user", "password"))
+ requestBuilder.header("Authorization", Credentials.basic("user", "password"))
}
def response = client.newCall(requestBuilder.build()).execute()

MODIFY
dd-java-agent/instrumentation/servlet-3/src/test/groovy/TomcatServlet3Test.groovy
dd-java-agent/instrumentation/servlet-3/src/test/groovy/TomcatServlet3Test.groovy
@@ -10,16 +10,20 @@ import org.apache.catalina.core.ApplicationFilterChain
import org.apache.catalina.startup.Tomcat
import org.apache.tomcat.JarScanFilter
import org.apache.tomcat.JarScanType
+import spock.lang.Shared
class TomcatServlet3Test extends AgentTestRunner {
OkHttpClient client = OkHttpUtils.client()
+ @Shared
int port
+ @Shared
Tomcat tomcatServer
+ @Shared
Context appContext
- def setup() {
+ def setupSpec() {
port = TestUtils.randomOpenPort()
tomcatServer = new Tomcat()
tomcatServer.setPort(port)
@@ -52,7 +56,7 @@ class TomcatServlet3Test extends AgentTestRunner {
"Tomcat server: http://" + tomcatServer.getHost().getName() + ":" + port + "/")
}
- def cleanup() {
+ def cleanupSpec() {
tomcatServer.stop()
tomcatServer.destroy()
}

Fix latestDepTests for Jetty