Refactoring Jetty and Tomcat
MODIFY
dd-java-agent/pom.xml
dd-java-agent/pom.xml
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>opentracing-agent-rules-java-okhttp</artifactId>
<version>${ot.agent.version}</version>
</dependency>
- <dependency>
- <groupId>io.opentracing.contrib</groupId>
- <artifactId>opentracing-agent-rules-java-web-servlet-filter</artifactId>
- <version>${ot.agent.version}</version>
- </dependency>
<!-- Used to find annotated methods -->
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-web-servlet-filter</artifactId>
<version>${ot.contrib.webservlet.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-servlet</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-servlet</artifactId>
+ <version>9.3.6.v20151106</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tomcat.embed</groupId>
+ <artifactId>tomcat-embed-core</artifactId>
+ <version>9.0.0.M1</version>
+ <scope>provided</scope>
</dependency>
<!-- Mongo instrumentation -->
</goals>
<configuration>
<transformers>
- <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
+ <transformer
+ implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>otarules.btm</resource>
</transformer>
- <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+ <transformer
+ implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Agent-Class>io.opentracing.contrib.agent.AnnotationsTracingAgent</Agent-Class>
<Premain-Class>io.opentracing.contrib.agent.AnnotationsTracingAgent

ADD
dd-java-agent/src/main/java/io/opentracing/contrib/agent/helper/JettyServletHelper.java
+package io.opentracing.contrib.agent.helper;
+
+import io.opentracing.contrib.web.servlet.filter.TracingFilter;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.jboss.byteman.rule.Rule;
+
+import javax.servlet.Filter;
+import java.util.EnumSet;
+
+/**
+ * Created by gpolaert on 6/15/17.
+ */
+public class JettyServletHelper extends DDTracingHelper<ServletContextHandler> {
+
+ public JettyServletHelper(Rule rule) {
+ super(rule);
+ }
+
+ @Override
+ protected ServletContextHandler doPatch(ServletContextHandler contextHandler) throws Exception {
+
+ String[] patterns = {"/*"};
+
+ Filter filter = new TracingFilter(tracer);
+ contextHandler
+ .getServletContext()
+ .addFilter("tracingFilter", filter)
+ .addMappingForUrlPatterns(EnumSet.allOf(javax.servlet.DispatcherType.class), true, patterns);
+
+ setState(contextHandler.getServletContext(), 1);
+ return contextHandler;
+ }
+}
+

ADD
dd-java-agent/src/main/java/io/opentracing/contrib/agent/helper/TomcatServletHelper.java
+package io.opentracing.contrib.agent.helper;
+
+import io.opentracing.contrib.web.servlet.filter.TracingFilter;
+import org.apache.catalina.core.ApplicationContext;
+import org.jboss.byteman.rule.Rule;
+
+import javax.servlet.Filter;
+import java.util.EnumSet;
+
+/**
+ * Created by gpolaert on 6/15/17.
+ */
+public class TomcatServletHelper extends DDTracingHelper<ApplicationContext> {
+
+ public TomcatServletHelper(Rule rule) {
+ super(rule);
+ }
+
+ @Override
+ protected ApplicationContext doPatch(ApplicationContext contextHandler) throws Exception {
+
+ String[] patterns = {"/*"};
+
+ Filter filter = new TracingFilter(tracer);
+ contextHandler
+ .addFilter("tracingFilter", filter)
+ .addMappingForUrlPatterns(EnumSet.allOf(javax.servlet.DispatcherType.class), true, patterns);
+
+ return contextHandler;
+ }
+}

MODIFY
dd-java-agent/src/main/resources/otarules.btm
dd-java-agent/src/main/resources/otarules.btm
@@ -59,3 +59,34 @@ IF TRUE
DO
$! = patch($!);
ENDRULE
+
+
+
+# Instrument Jetty
+# ===========================
+# State 0 - no filter installed
+# State 1 - filter installed
+
+RULE jetty
+CLASS org.eclipse.jetty.servlet.ServletContextHandler
+METHOD <init>
+HELPER io.opentracing.contrib.agent.helper.JettyServletHelper
+AT EXIT
+IF getState($0.getServletContext()) == 0
+DO
+ patch($this)
+ENDRULE
+
+
+# Instrument Tomcat
+# ===========================
+
+RULE tomcat
+CLASS org.apache.catalina.core.ApplicationContext
+METHOD <init>
+HELPER io.opentracing.contrib.agent.helper.TomcatServletHelper
+AT EXIT
+IF TRUE
+DO
+ patch($this)
+ENDRULE
\ No newline at end of file

Refactoring Jetty and Tomcat