0

Well, I wanted to trace http calls using bytebuddy and capture the time elapsed per trace. I have come across a github project and checked some code and after a long and exhausting research and learning I was able to run it like this :

LoggingAgent

return new AgentBuilder.Default().disableClassFormatChanges()
            .with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
            .with(AgentBuilder.TypeStrategy.Default.REDEFINE)
            .type(ElementMatchers.hasSuperType(named("javax.servlet.Servlet")))
            .transform(new AgentBuilder.Transformer() {

                @Override
                public DynamicType.Builder<?> transform(
                        DynamicType.Builder<?> builder,
                        TypeDescription typeDescription,
                        ClassLoader classLoader) {
                    return builder.visit(Advice.to(LoggingAdvice.class).on( 
                            named("service"))); // Methodname = pointcut method = service()
                }
            });

LoggingAdvice

@Advice.OnMethodEnter
public static void intercept(@Advice.BoxedArguments Object[] allArguments,
        @Advice.Origin Method method) {
    Logger logger = LoggerFactory.getLogger(method.getDeclaringClass());
    logger.info("Method {} of class {} called", method.getName(), method
            .getDeclaringClass().getSimpleName());

    for (Object argument : allArguments) {
        logger.info("Method {}, parameter type {}, value={}",
                method.getName(), argument.getClass().getSimpleName(),
                argument.toString());
    }
}

The code works fine without any problem when I inject this agent at runtime, using byte-buddy-loader. But, whenever I was trying to run it by passing as CommandLine arguement, it stops the whole application.

After that, I tried to run this code, using a class loader, and then pass it as command line arguement, while loading the agent using a class loader. it worked! but now, the actual problem is, Whenever I try to use a class variable/ global variable, and inject it in an application. The app throws a NoClassDefFoundError for LoggingAdvice.class

Can anyone please guide me in the right direction ?

Edit This is not a duplicate, I mentioned above that whenever I try to access global variable only that time it does not work. Without accessing the global variable, the code is working fine.

Mohd Naved
  • 448
  • 6
  • 20
  • Please check the classpath when you run on commandline. NoClassDef hits when dependencies are not met. So Make sure all dependency classes for LoggingAdvice.class is available on classpath. In that perspective this is duplicate. – Kris Jul 02 '19 at 12:21
  • @Kris I mentioned above that whenever I try to access global variable only that time it does not work. Without accessing the global variable, the code is working fine. – Mohd Naved Jul 02 '19 at 12:42
  • I would like to answer this question myself, but it has been marked as duplicate. Please remove the duplicate tag from the question so I can answer it. – Mohd Naved Aug 14 '19 at 11:32

0 Answers0