Bytebuddy agent not getting loaded

Viewed 499

I have a simple bytebuddy agent which records method entry/exit. I run it as mvn clean test -DargLine="-javaagent:$JAVA_AGENT_JAR"

It works well for couple of my Java projects. However, when I tried it on few open source projects: flink, guava, dubbo it dint work. I don't get any log trace to debug. It successfully runs the unit tests without executing any java-agent code. I am suspecting the agent is not even getting loaded. How do I debug/fix this issue? jdk: 1.8 bytebuddy verion:1.10.18

Here is code snippet

@Override
  public void instrument(Instrumentation instrumentation) {
    System.out.println("bytebuddy Instrmentation called");

    final Advice methodAdvice = Advice.to(MethodTracer.class);
    final Advice constructorAdvice = Advice.to(ConstructorTracer.class);


    ResettableClassFileTransformer agent = new AgentBuilder.Default()
        .with(new TracerLogger())  
        .type(ElementMatchers.nameStartsWith("com.examples.")) 
        .transform(new AgentBuilder.Transformer() {
          @Override
          public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule module) {
            builder = builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().method(ElementMatchers.isMethod(), methodAdvice));
            builder = builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().constructor(ElementMatchers.isConstructor(), constructorAdvice));
            return builder;
          }
        })
        .installOn(instrumentation);
  }

Update-1: trying retransformation strategy, it still didn't work

            .with(AgentBuilder.Listener.StreamWriting.toSystemOut())
                .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
                .with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
                .with(AgentBuilder.TypeStrategy.Default.REDEFINE)
                .type(matcher)
            .transform(new AgentBuilder.Transformer() {
              @Override
              public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription,
                  ClassLoader classLoader, JavaModule module) {
                builder = builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().method(
                    ElementMatchers.isMethod().and(ElementMatchers.isAnnotatedWith(Test.class)), testMethodAdvice));
                builder = builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().method(
                    ElementMatchers.isMethod().and(ElementMatchers.not(ElementMatchers.isAnnotatedWith(Test.class))),
                    methodAdvice));
                builder = builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().constructor(
                    ElementMatchers.isConstructor().and(ElementMatchers.isAnnotatedWith(Test.class)),
                    testConstructorAdvice));
                builder = builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().constructor(
                    ElementMatchers.isConstructor().and(
                        ElementMatchers.not(ElementMatchers.isAnnotatedWith(Test.class))),
                    constructorAdvice));

                return builder;
              }
            })
            .installOn(instrumentation);

Update-2: Using MAVEN_OPTS env variable solved the problem of loading the agent.

export MAVEN_OPTS="-javaagent:$JAVA_AGENT=$CONFIG_FILE" 
mvn clean test -DargLine="-javaagent:$JAVA_AGENT=$CONFIG_FILE"
0 Answers
Related