bytebuddy代理未加载

xytpbqjk  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(273)

我有一个简单的bytebuddy代理记录方法的进入/退出。我把它当作 mvn clean test -DargLine="-javaagent:$JAVA_AGENT_JAR" 它在我的几个java项目中运行良好。然而,当我在几个开源项目上尝试它时:flink、guava、dubbo,它的作用不大。我没有要调试的日志跟踪。它在不执行任何java代理代码的情况下成功地运行单元测试。我怀疑探员甚至没有上膛。如何调试/修复此问题?
jdk: 1.8 bytebuddy verion:1.10.18 下面是代码片段

@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);
  }

更新一:尝试重传策略,还是没用

.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:使用maven\u opts env变量解决了加载代理的问题。

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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题