Skip to content

Commit a2a194d

Browse files
gary-huangtylerbenson
authored andcommitted
Fix latestDepTest for JSP instrumentation.
Normalize jsp.requestUrl tag value because Tomcat 9 seems to add relative path symbols of jsp files. Tomcat 9 doesn't automatically create a connector, causing failures in latestDepTest. Tomcat 9 also seems to have small changes that effect some of the tests, such as changing the exception type and also removal of the exception message in one of the tests.
1 parent 9bd2b86 commit a2a194d

3 files changed

Lines changed: 49 additions & 18 deletions

File tree

dd-java-agent/instrumentation/jsp-2.3/src/main/java/datadog/trace/instrumentation/jsp/JSPInstrumentation.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@
1616
import io.opentracing.Span;
1717
import io.opentracing.tag.Tags;
1818
import io.opentracing.util.GlobalTracer;
19+
import java.net.URI;
20+
import java.net.URISyntaxException;
1921
import java.util.Collections;
2022
import java.util.HashMap;
2123
import java.util.Map;
2224
import javax.servlet.RequestDispatcher;
2325
import javax.servlet.http.HttpServletRequest;
2426
import javax.servlet.http.HttpServletResponse;
27+
import javax.servlet.jsp.HttpJspPage;
2528
import net.bytebuddy.asm.Advice;
2629
import net.bytebuddy.description.type.TypeDescription;
2730
import net.bytebuddy.matcher.ElementMatcher;
31+
import org.slf4j.LoggerFactory;
2832

2933
@AutoService(Instrumenter.class)
3034
public final class JSPInstrumentation extends Instrumenter.Default {
@@ -68,19 +72,28 @@ public static Scope startSpan(
6872
// get the JSP file name being rendered in an include action
6973
final Object includeServletPath = req.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
7074
String resourceName = req.getServletPath();
71-
if (includeServletPath != null && includeServletPath instanceof String) {
75+
if (includeServletPath instanceof String) {
7276
resourceName = includeServletPath.toString();
7377
}
7478
span.setTag(DDTags.RESOURCE_NAME, resourceName);
7579

7680
final Object forwardOrigin = req.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH);
77-
if (forwardOrigin != null && forwardOrigin instanceof String) {
81+
if (forwardOrigin instanceof String) {
7882
span.setTag("jsp.forwardOrigin", forwardOrigin.toString());
7983
}
8084

8185
// add the request URL as a tag to provide better context when looking at spans produced by
82-
// actions
83-
span.setTag("jsp.requestURL", req.getRequestURL().toString());
86+
// actions. Tomcat 9 has relative path symbols in the value returned from
87+
// HttpServletRequest#getRequestURL(),
88+
// normalizing the URL should remove those symbols for readability and consistency
89+
try {
90+
span.setTag(
91+
"jsp.requestURL", (new URI(req.getRequestURL().toString())).normalize().toString());
92+
} catch (final URISyntaxException uriSE) {
93+
LoggerFactory.getLogger(HttpJspPage.class)
94+
.warn("Failed to get and normalize request URL: " + uriSE.getMessage());
95+
}
96+
8497
Tags.COMPONENT.set(span, "jsp-http-servlet");
8598

8699
return scope;

dd-java-agent/instrumentation/jsp-2.3/src/test/groovy/JSPInstrumentationBasicTests.groovy

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,22 @@ class JSPInstrumentationBasicTests extends AgentTestRunner {
4545
OkHttpClient client = OkHttpUtils.client()
4646

4747
def setupSpec() {
48+
baseDir = Files.createTempDir()
49+
baseDir.deleteOnExit()
50+
expectedJspClassFilesDir = baseDir.getCanonicalFile().getAbsolutePath() + expectedJspClassFilesDir
51+
4852
port = TestUtils.randomOpenPort()
53+
4954
tomcatServer = new Tomcat()
55+
tomcatServer.setBaseDir(baseDir.getAbsolutePath())
5056
tomcatServer.setPort(port)
5157
// comment to debug
5258
tomcatServer.setSilent(true)
53-
54-
baseDir = Files.createTempDir()
55-
baseDir.deleteOnExit()
56-
expectedJspClassFilesDir = baseDir.getCanonicalFile().getAbsolutePath() + expectedJspClassFilesDir
59+
// this is needed in tomcat 9, this triggers the creation of a connector, will not
60+
// affect tomcat 7 and 8
61+
// https://stackoverflow.com/questions/48998387/code-works-with-embedded-apache-tomcat-8-but-not-with-9-whats-changed
62+
tomcatServer.getConnector()
5763
baseUrl = "http://localhost:$port/$jspWebappContext"
58-
tomcatServer.setBaseDir(baseDir.getAbsolutePath())
5964

6065
appContext = tomcatServer.addWebapp("/$jspWebappContext",
6166
JSPInstrumentationBasicTests.getResource("/webapps/jsptest").getPath())
@@ -344,7 +349,14 @@ class JSPInstrumentationBasicTests extends AgentTestRunner {
344349
"span.origin.type" jspClassName
345350
"servlet.context" "/$jspWebappContext"
346351
"jsp.requestURL" reqUrl
347-
errorTags(exceptionClass, errorMessage)
352+
"error" true
353+
"error.type" { String tagExceptionType ->
354+
return tagExceptionType == exceptionClass.getName() || tagExceptionType.contains(exceptionClass.getSimpleName())
355+
}
356+
"error.msg" { String tagErrorMsg ->
357+
return errorMessageOptional || tagErrorMsg instanceof String
358+
}
359+
"error.stack" String
348360
defaultTags()
349361
}
350362
}
@@ -373,10 +385,10 @@ class JSPInstrumentationBasicTests extends AgentTestRunner {
373385
res.close()
374386

375387
where:
376-
test | jspFileName | jspClassName | exceptionClass | errorMessage
377-
"java runtime error" | "runtimeError.jsp" | "runtimeError_jsp" | ArithmeticException | String
378-
"invalid write" | "invalidWrite.jsp" | "invalidWrite_jsp" | StringIndexOutOfBoundsException | String
379-
"missing query gives null" | "getQuery.jsp" | "getQuery_jsp" | NullPointerException | null
388+
test | jspFileName | jspClassName | exceptionClass | errorMessageOptional
389+
"java runtime error" | "runtimeError.jsp" | "runtimeError_jsp" | ArithmeticException | false
390+
"invalid write" | "invalidWrite.jsp" | "invalidWrite_jsp" | IndexOutOfBoundsException | true
391+
"missing query gives null" | "getQuery.jsp" | "getQuery_jsp" | NullPointerException | true
380392
}
381393

382394
def "non-erroneous include plain HTML GET"() {

dd-java-agent/instrumentation/jsp-2.3/src/test/groovy/JSPInstrumentationForwardTests.groovy

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,23 @@ class JSPInstrumentationForwardTests extends AgentTestRunner {
4343
OkHttpClient client = OkHttpUtils.client()
4444

4545
def setupSpec() {
46+
baseDir = Files.createTempDir()
47+
baseDir.deleteOnExit()
48+
expectedJspClassFilesDir = baseDir.getCanonicalFile().getAbsolutePath() + expectedJspClassFilesDir
49+
4650
port = TestUtils.randomOpenPort()
51+
4752
tomcatServer = new Tomcat()
53+
tomcatServer.setBaseDir(baseDir.getAbsolutePath())
4854
tomcatServer.setPort(port)
4955
// comment to debug
5056
tomcatServer.setSilent(true)
57+
// this is needed in tomcat 9, this triggers the creation of a connector, will not
58+
// affect tomcat 7 and 8
59+
// https://stackoverflow.com/questions/48998387/code-works-with-embedded-apache-tomcat-8-but-not-with-9-whats-changed
60+
tomcatServer.getConnector()
5161

52-
baseDir = Files.createTempDir()
53-
baseDir.deleteOnExit()
54-
expectedJspClassFilesDir = baseDir.getCanonicalFile().getAbsolutePath() + expectedJspClassFilesDir
5562
baseUrl = "http://localhost:$port/$jspWebappContext"
56-
tomcatServer.setBaseDir(baseDir.getAbsolutePath())
5763

5864
appContext = tomcatServer.addWebapp("/$jspWebappContext",
5965
JSPInstrumentationForwardTests.getResource("/webapps/jsptest").getPath())

0 commit comments

Comments
 (0)