Unable to instrument apache httpclient using javaagent for spring boot uber jar application

旧城冷巷雨未停 提交于 2019-12-04 17:54:08
segeon

I solved this problem through two steps:

  1. Use Spring Boot's dedicated ClassLoader to deligate to an unloaded type:

    public static void premain(String arguments, Instrumentation instrumentation) {
      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      ClassFileLocator.Compound compound = new ClassFileLocator.Compound(ClassFileLocator.ForClassLoader.of(classLoader), ClassFileLocator.ForClassLoader.ofClassPath());
      TypeDescription delegator = TypePool.Default.of(compound).describe(delegatorClass).resolve();
      new AgentBuilder.Default()
        //.withBinaryLocator(binaryLocatorFor(instrumentation))
        .withListener(DebugListener.getListener())
        .type(is(abstractHttpClientDescription()))
        .transform(new AgentBuilder.Transformer() {
          @Override
          public DynamicType.Builder transform(DynamicType.Builder builder,
              TypeDescription typeDescription) {
            return builder.method(named("execute")
              .and(takesArguments(httpHostDescription(), httpRequestDescription(), httpContextDescription()))
              .and(returns(named("org.apache.http.HttpResponse"))))
              .intercept(MethodDelegation.to(delegator));
          }
      }).installOn(instrumentation);
    } 
    
  2. Package the javaagent jar into Spring Boot's uber-jar such that the delegator class can reference classes related to the intecepted classes.

This issue is discussed in greater detail in Byte Buddy's issue tracker.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!