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.