Skip to content

Commit 13ffdc5

Browse files
authored
Merge pull request #131 from DataDog/tyler/handle-double-registration
Prevent NPE for double registrations
2 parents e24b1a9 + b430eed commit 13ffdc5

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed

dd-java-agent-ittests/dd-java-agent-ittests.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ test {
4545

4646
doFirst {
4747
// Defining here to allow jacoco to be first on the command line.
48-
jvmArgs "-javaagent:${project(':dd-java-agent').buildDir}/libs/dd-java-agent-${project.version}.jar"
48+
jvmArgs "-javaagent:${project(':dd-java-agent').tasks.shadowJar.archivePath}"
4949
}
50-
50+
5151
testLogging {
5252
events "started"
5353
}

dd-java-agent/integrations/helpers/src/main/java/com/datadoghq/agent/integration/JettyServletHelper.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
/** Patch the Jetty Servlet during the init steps */
1111
public class JettyServletHelper extends DDAgentTracingHelper<ServletContextHandler> {
1212

13-
public JettyServletHelper(Rule rule) {
13+
private static final String pattern = "/*";
14+
15+
public JettyServletHelper(final Rule rule) {
1416
super(rule);
1517
}
1618

@@ -21,18 +23,19 @@ public JettyServletHelper(Rule rule) {
2123
* @return The same current contextHandler but patched
2224
* @throws Exception
2325
*/
24-
protected ServletContextHandler doPatch(ServletContextHandler contextHandler) throws Exception {
25-
26-
String[] patterns = {"/*"};
27-
28-
Filter filter = new TracingFilter(tracer);
29-
FilterRegistration.Dynamic registration =
30-
contextHandler.getServletContext().addFilter("tracingFilter", filter);
31-
registration.setAsyncSupported(true);
32-
registration.addMappingForUrlPatterns(
33-
EnumSet.allOf(javax.servlet.DispatcherType.class), true, patterns);
34-
35-
setState(contextHandler.getServletContext(), 1);
26+
@Override
27+
protected ServletContextHandler doPatch(final ServletContextHandler contextHandler)
28+
throws Exception {
29+
if (contextHandler.getServletContext().getFilterRegistration("tracingFilter") == null) {
30+
final Filter filter = new TracingFilter(tracer);
31+
final FilterRegistration.Dynamic registration =
32+
contextHandler.getServletContext().addFilter("tracingFilter", filter);
33+
if (registration != null) { // filter of that name must already be registered.
34+
registration.setAsyncSupported(true);
35+
registration.addMappingForUrlPatterns(
36+
EnumSet.allOf(javax.servlet.DispatcherType.class), true, pattern);
37+
}
38+
}
3639
return contextHandler;
3740
}
3841
}

dd-java-agent/integrations/helpers/src/main/java/com/datadoghq/agent/integration/TomcatServletHelper.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
import org.apache.catalina.core.ApplicationContext;
88
import org.jboss.byteman.rule.Rule;
99

10-
/** Patch the Jetty Servlet during the init steps */
10+
/** Patch the Tomcat Servlet during the init steps */
1111
public class TomcatServletHelper extends DDAgentTracingHelper<ApplicationContext> {
1212

13-
public TomcatServletHelper(Rule rule) {
13+
private static final String pattern = "/*";
14+
15+
public TomcatServletHelper(final Rule rule) {
1416
super(rule);
1517
}
1618

@@ -21,16 +23,18 @@ public TomcatServletHelper(Rule rule) {
2123
* @return The same current contextHandler but patched
2224
* @throws Exception
2325
*/
24-
protected ApplicationContext doPatch(ApplicationContext contextHandler) throws Exception {
25-
26-
String[] patterns = {"/*"};
27-
28-
Filter filter = new TracingFilter(tracer);
29-
FilterRegistration.Dynamic registration = contextHandler.addFilter("tracingFilter", filter);
30-
registration.setAsyncSupported(true);
31-
registration.addMappingForUrlPatterns(
32-
EnumSet.allOf(javax.servlet.DispatcherType.class), true, patterns);
33-
26+
@Override
27+
protected ApplicationContext doPatch(final ApplicationContext contextHandler) throws Exception {
28+
if (contextHandler.getFilterRegistration("tracingFilter") == null) {
29+
final Filter filter = new TracingFilter(tracer);
30+
final FilterRegistration.Dynamic registration =
31+
contextHandler.addFilter("tracingFilter", filter);
32+
if (registration != null) { // filter of that name must already be registered.
33+
registration.setAsyncSupported(true);
34+
registration.addMappingForUrlPatterns(
35+
EnumSet.allOf(javax.servlet.DispatcherType.class), true, pattern);
36+
}
37+
}
3438
return contextHandler;
3539
}
3640
}

0 commit comments

Comments
 (0)