Loading a class which is part of otel extension jar from Advice class is failing with NoClassDefFoundError #9701
Unanswered
MadhusudanN
asked this question in
Q&A
Replies: 1 comment
-
See https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/contributing/writing-instrumentation-module.md#tell-the-agent-which-classes-are-a-part-of-the-instrumentation-by-overriding-the-ishelperclass-method You probably need to override |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Background ::
I have a spring boot application. I have added custom instrumentation to apply advice when filter method is called as demonstrated in this link :: https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/examples/extension/src/main/java/com/example/javaagent/instrumentation/DemoServlet3Instrumentation.java
We know that advice is a static inner class as explained in this document :: https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/contributing/writing-instrumentation-module.md
Requirement ::
1. Read a system property to get the advice class name (-DinstrumentationAdvice=com.test.incubation.opentelemetry.extension.instrumentation.CustomAdvice)
2. Load this class using the Class.forName(className)
3. Get instance of this class using java.lang.reflection API
4. Invoke the method using java.lang.reflection API, capture the extra attributes and add them to the baggage and root span
5. Perform the default operation (Get userid and username from incoming request) and add them to baggage and root span.
Issue ::
Getting the NoClassDefFoundError below ::
Failed to load class due to java.lang.NoClassDefFoundError: com/test/incubation/opentelemetry/extension/instrumentation/CustomAdvice
java.lang.NoClassDefFoundError: com/test/incubation/opentelemetry/extension/instrumentation/CustomAdvice
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:50)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.test.incubation.engineering.springboot.filter.TestFilter2.doFilter(TestFilter2.java:40)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.test.incubation.engineering.springboot.filter.TestFilter1.doFilter(TestFilter1.java:44)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.test.cloud.service.security.AgentAuthenticationFilter.doFilter(AgentAuthenticationFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
Caused by: java.lang.ClassNotFoundException: com.test.incubation.opentelemetry.extension.instrumentation.CustomAdvice
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:592)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
Approaches tried ::
Tried to load the class "com/test/incubation/opentelemetry/extension/instrumentation/CustomAdvice" using different class loaders ::
Class.forName() -- current classloader
ClassLoader.getSystemClassLoader().loadClass -- Using System class loader
Thread.currentThread().getContextClassLoader().loadClass
The above approaches did not help and ended up with ClassNotFoundException only.
Please find the classes below ::
Interface BaseAdvice class ::
public interface BaseAdvice {
}
StandardAdvice class ::
package com.test.incubation.opentelemetry.extension.instrumentation;
public class StandardAdvice implements BaseAdvice {
// System.out.println("Inline is set to true");
System.out.println("Running Advice");
Map<String, String> attributesMap = new HashMap<>();
Class<?> adviceClazz = null;
// adviceClazz = Class.forName(className);
Constructor<?> constructor = adviceClazz.getConstructor();
Object instance = constructor.newInstance();
// System.out.println(adviceClazz.getSimpleName() + ":: Running advice");
CustomAdvice class ::
package com.test.incubation.opentelemetry.extension.instrumentation;
public class CustomAdvice extends StandardAdvice {
@OverRide
public Map<String, String> getAttributesMap(ServletRequest request) {
Map<String, String> attributesMap = new HashMap<>();
HttpServletRequest httpRequest = (HttpServletRequest) request;
attributesMap.put("custom-header", httpRequest.getHeader("custom-header"));
System.out.println("Custom header added is " + attributesMap.get("custom-header"));
return attributesMap;
}
}
The arguements passed to the application are shown below ::
set "CATALINA_OPTS=%CATALINA_OPTS%-Dotel.service.name=TestSvc -javaagent:C:\Open_Telemetry\opentelemetry-javaagent.jar -Dotel.exporter.otlp.headers="Authorization=Bearer " -Dotel.exporter.otlp.endpoint=https://:443 -Dotel.traces.exporter=otlp -Dotel.logs.exporter=otlp -Xverify:none -Dotel.javaagent.extensions=C:\eclipse-workspace\opentelemetry-java-instrumentation-extension-demo-1.0-all.jar -DinstrumentationAdvice=com.test.incubation.opentelemetry.extension.instrumentation.CustomAdvice"
Note ::
The above app is deployed as war in tomcat 8.5.84 version.
OS :: Windows 11
Can someone please look into this and please suggest how can I proceed further on this.
Beta Was this translation helpful? Give feedback.
All reactions