Simplify tomcat version detection
MODIFY
instrumentation/tomcat/tomcat-10.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/v10_0/Tomcat10InstrumentationModule.java
instrumentation/tomcat/tomcat-10.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/v10_0/Tomcat10InstrumentationModule.java
package io.opentelemetry.javaagent.instrumentation.tomcat.v10_0;
-import static net.bytebuddy.matcher.ElementMatchers.declaresMethod;
-import static net.bytebuddy.matcher.ElementMatchers.named;
-import static net.bytebuddy.matcher.ElementMatchers.returns;
+import static io.opentelemetry.javaagent.extension.matcher.ClassLoaderMatcher.hasClassesNamed;
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
@@ -15,6 +13,7 @@ import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.instrumentation.tomcat.common.TomcatServerHandlerInstrumentation;
import java.util.Collections;
import java.util.List;
+import net.bytebuddy.matcher.ElementMatcher;
@AutoService(InstrumentationModule.class)
public class Tomcat10InstrumentationModule extends InstrumentationModule {
@@ -23,15 +22,17 @@ public class Tomcat10InstrumentationModule extends InstrumentationModule {
super("tomcat", "tomcat-10.0");
}
+ @Override
+ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
+ // only matches tomcat 10.0+
+ return hasClassesNamed("jakarta.servlet.ReadListener");
+ }
+
@Override
public List<TypeInstrumentation> typeInstrumentations() {
- // Tomcat 10+ is verified by making sure Request has a methods returning
- // jakarta.servlet.ReadListener which is returned by getReadListener method on Tomcat 10+
return Collections.singletonList(
new TomcatServerHandlerInstrumentation(
Tomcat10InstrumentationModule.class.getPackage().getName()
- + ".Tomcat10ServerHandlerAdvice",
- named("org.apache.coyote.Request")
- .and(declaresMethod(returns(named("jakarta.servlet.ReadListener"))))));
+ + ".Tomcat10ServerHandlerAdvice"));
}
}

MODIFY
instrumentation/tomcat/tomcat-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/v7_0/Tomcat7InstrumentationModule.java
instrumentation/tomcat/tomcat-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/v7_0/Tomcat7InstrumentationModule.java
package io.opentelemetry.javaagent.instrumentation.tomcat.v7_0;
-import static net.bytebuddy.matcher.ElementMatchers.declaresMethod;
-import static net.bytebuddy.matcher.ElementMatchers.named;
+import static io.opentelemetry.javaagent.extension.matcher.ClassLoaderMatcher.hasClassesNamed;
import static net.bytebuddy.matcher.ElementMatchers.not;
-import static net.bytebuddy.matcher.ElementMatchers.returns;
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
@@ -16,6 +14,7 @@ import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.instrumentation.tomcat.common.TomcatServerHandlerInstrumentation;
import java.util.Collections;
import java.util.List;
+import net.bytebuddy.matcher.ElementMatcher;
@AutoService(InstrumentationModule.class)
public class Tomcat7InstrumentationModule extends InstrumentationModule {
@@ -24,6 +23,12 @@ public class Tomcat7InstrumentationModule extends InstrumentationModule {
super("tomcat", "tomcat-7.0");
}
+ @Override
+ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
+ // does not match tomcat 10.0+
+ return not(hasClassesNamed("jakarta.servlet.ReadListener"));
+ }
+
@Override
public List<TypeInstrumentation> typeInstrumentations() {
// Tomcat 10+ is excluded by making sure Request does not have any methods returning
@@ -31,8 +36,6 @@ public class Tomcat7InstrumentationModule extends InstrumentationModule {
return Collections.singletonList(
new TomcatServerHandlerInstrumentation(
Tomcat7InstrumentationModule.class.getPackage().getName()
- + ".Tomcat7ServerHandlerAdvice",
- named("org.apache.coyote.Request")
- .and(not(declaresMethod(returns(named("jakarta.servlet.ReadListener")))))));
+ + ".Tomcat7ServerHandlerAdvice"));
}
}

MODIFY
instrumentation/tomcat/tomcat-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/common/TomcatServerHandlerInstrumentation.java
instrumentation/tomcat/tomcat-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/common/TomcatServerHandlerInstrumentation.java
@@ -18,12 +18,9 @@ import net.bytebuddy.matcher.ElementMatcher;
public class TomcatServerHandlerInstrumentation implements TypeInstrumentation {
private final String adviceClassName;
- private final ElementMatcher<TypeDescription> requestMatcher;
- public TomcatServerHandlerInstrumentation(
- String adviceClassName, ElementMatcher<TypeDescription> requestMatcher) {
+ public TomcatServerHandlerInstrumentation(String adviceClassName) {
this.adviceClassName = adviceClassName;
- this.requestMatcher = requestMatcher;
}
@Override
@@ -37,7 +34,7 @@ public class TomcatServerHandlerInstrumentation implements TypeInstrumentation {
isMethod()
.and(isPublic())
.and(named("service"))
- .and(takesArgument(0, requestMatcher))
+ .and(takesArgument(0, named("org.apache.coyote.Request")))
.and(takesArgument(1, named("org.apache.coyote.Response"))),
adviceClassName);
}

Simplify tomcat version detection (#3129)