聊聊skywalking的log4j2-activation

本文主要研究一下skywalking的log4j2-activation


聊聊skywalking的log4j2-activation


skywalking-plugin.def

skywalking-6.6.0/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/resources/skywalking-plugin.def

<code>toolkit-log4j2=org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.TraceIdConverterActivation
toolkit-log4j2=org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.async.AsyncLoggerConfigInstrumentation
toolkit-log4j2=org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.async.Log4jLogEventInstrumentation
toolkit-log4j2=org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.async.RingBufferLogEventInstrumentation/<code>
  • skywalking的log4j2-activation定義了TraceIdConverterActivation、AsyncLoggerConfigInstrumentation、Log4jLogEventInstrumentation、RingBufferLogEventInstrumentation這幾個增強

TraceIdConverterActivation

skywalking-6.6.0/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/TraceIdConverterActivation.java

<code>public class TraceIdConverterActivation extends ClassInstanceMethodsEnhancePluginDefine {

  private static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.TraceIdConverterMethodInterceptor";
  private static final String ENHANCE_CLASS = "org.apache.skywalking.apm.toolkit.log.log4j.v2.x.TraceIdConverter";
  private static final String ENHANCE_METHOD = "format";

  @Override
  public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
      return new ConstructorInterceptPoint[0];
  }

  @Override
  public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
      return new InstanceMethodsInterceptPoint[]{
          new InstanceMethodsInterceptPoint() {
              @Override
              public ElementMatcher<methoddescription> getMethodsMatcher() {

                  return named(ENHANCE_METHOD);
              }

              @Override
              public String getMethodsInterceptor() {
                  return INTERCEPT_CLASS;
              }

              @Override
              public boolean isOverrideArgs() {
                  return false;
              }
          }
      };
  }

  @Override
  protected ClassMatch enhanceClass() {
      return byName(ENHANCE_CLASS);
  }
}/<methoddescription>/<code>
  • TraceIdConverterActivation繼承了ClassInstanceMethodsEnhancePluginDefine,它使用org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.TraceIdConverterMethodInterceptor增強了org.apache.skywalking.apm.toolkit.log.log4j.v2.x.TraceIdConverter的format方法

TraceIdConverterMethodInterceptor

skywalking-6.6.0/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/TraceIdConverterMethodInterceptor.java

<code>public class TraceIdConverterMethodInterceptor implements InstanceMethodsAroundInterceptor {


  @Override
  public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class>[] argumentsTypes,
                            MethodInterceptResult result) throws Throwable {
      String tid = "";

      //Async Thread, where ContextManager is not active
      if (!ContextManager.isActive() && allArguments[0] instanceof EnhancedInstance) {
          tid = (String) ((EnhancedInstance) allArguments[0]).getSkyWalkingDynamicField();
          if (tid == null) {
              tid = "N/A";
          }
      } else {
          tid = ContextManager.getGlobalTraceId();
      }
      ((StringBuilder) allArguments[1]).append("TID: ").append(tid);
      result.defineReturnValue(null);
  }

  @Override
  public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
                            Class>[] argumentsTypes, Object ret) throws Throwable {
      return ret;
  }

  @Override
  public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
                                    Class>[] argumentsTypes, Throwable t) {
  }
}/<code>
  • TraceIdConverterMethodInterceptor實現了InstanceMethodsAroundInterceptor接口,其beforeMethod方法主要是從ContextManager.getGlobalTraceId()獲取tid,然後給allArguments[1]添加TID:前綴的tid值

AsyncLoggerConfigInstrumentation

skywalking-6.6.0/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/AsyncLoggerConfigInstrumentation.java

<code>public class AsyncLoggerConfigInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {

  private static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.async.AsyncLoggerConfigMethodInterceptor";
  private static final String ENHANCE_CLASS = "org.apache.logging.log4j.core.async.AsyncLoggerConfig";
  private static final String ENHANCE_METHOD = "populateLazilyInitializedFields";

  @Override
  public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
      return new ConstructorInterceptPoint[0];
  }

  @Override
  public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
      return new InstanceMethodsInterceptPoint[]{
          new InstanceMethodsInterceptPoint() {
              @Override
              public ElementMatcher<methoddescription> getMethodsMatcher() {
                  return named(ENHANCE_METHOD);
              }

              @Override
              public String getMethodsInterceptor() {
                  return INTERCEPT_CLASS;
              }

              @Override
              public boolean isOverrideArgs() {
                  return false;
              }
          }
      };
  }

  @Override
  protected ClassMatch enhanceClass() {
      return byName(ENHANCE_CLASS);
  }
}/<methoddescription>/<code>
  • AsyncLoggerConfigInstrumentation繼承了ClassInstanceMethodsEnhancePluginDefine,它使用org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.async.AsyncLoggerConfigMethodInterceptor增強了org.apache.logging.log4j.core.async.AsyncLoggerConfig的populateLazilyInitializedFields方法

AsyncLoggerConfigMethodInterceptor

skywalking-6.6.0/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/AsyncLoggerConfigMethodInterceptor.java

<code>public class AsyncLoggerConfigMethodInterceptor implements InstanceMethodsAroundInterceptor {


  @Override
  public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class>[] argumentsTypes,
                            MethodInterceptResult result) throws Throwable {
      if (allArguments[0] instanceof EnhancedInstance) {
          EnhancedInstance instances = (EnhancedInstance) allArguments[0];
          instances.setSkyWalkingDynamicField(ContextManager.getGlobalTraceId());
      }
  }

  @Override
  public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
                            Class>[] argumentsTypes, Object ret) throws Throwable {
      return ret;
  }

  @Override
  public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
                                    Class>[] argumentsTypes, Throwable t) {
  }
}/<code>
  • AsyncLoggerConfigMethodInterceptor實現了InstanceMethodsAroundInterceptor接口,其beforeMethod方法會將ContextManager.getGlobalTraceId()設置給allArguments[0]的skyWalkingDynamicField

Log4jLogEventInstrumentation

skywalking-6.6.0/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/Log4jLogEventInstrumentation.java

<code>public class Log4jLogEventInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
  @Override
  public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
      return new ConstructorInterceptPoint[]{
          new ConstructorInterceptPoint() {
              @Override
              public ElementMatcher<methoddescription> getConstructorMatcher() {
                  return any();
              }

              @Override
              public String getConstructorInterceptor() {
                  return "org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.async.Log4jLogEventConstructorInterceptor";
              }
          }
      };
  }

  @Override
  public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
      return new InstanceMethodsInterceptPoint[0];
  }

  @Override
  protected ClassMatch enhanceClass() {
      return byName("org.apache.logging.log4j.core.impl.Log4jLogEvent");
  }
}/<methoddescription>/<code>
  • Log4jLogEventInstrumentation繼承了ClassInstanceMethodsEnhancePluginDefine,它使用org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.async.Log4jLogEventConstructorInterceptor增強了org.apache.logging.log4j.core.impl.Log4jLogEvent的構造器

Log4jLogEventConstructorInterceptor

skywalking-6.6.0/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/Log4jLogEventConstructorInterceptor.java

<code>public class Log4jLogEventConstructorInterceptor implements InstanceConstructorInterceptor {

  @Override
  public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
  }
}/<code>
  • Log4jLogEventConstructorInterceptor實現了InstanceConstructorInterceptor接口,其onConstruct方法目前沒有操作

RingBufferLogEventInstrumentation

skywalking-6.6.0/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/RingBufferLogEventInstrumentation.java

<code>public class RingBufferLogEventInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
  private static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.async.RingBufferLogEventMethodInterceptor";
  private static final String ENHANCE_CLASS = "org.apache.logging.log4j.core.async.RingBufferLogEvent";
  private static final String ENHANCE_METHOD = "setMessage";
  @Override
  public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
      return new ConstructorInterceptPoint[0];
  }

  @Override
  public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
      return new InstanceMethodsInterceptPoint[]{
          new InstanceMethodsInterceptPoint() {
              @Override
              public ElementMatcher<methoddescription> getMethodsMatcher() {
                  return named(ENHANCE_METHOD);
              }

              @Override
              public String getMethodsInterceptor() {
                  return INTERCEPT_CLASS;
              }

              @Override
              public boolean isOverrideArgs() {
                  return false;
              }
          }
      };
  }

  @Override
  protected ClassMatch enhanceClass() {
      return byName(ENHANCE_CLASS);
  }
}/<methoddescription>/<code>
  • RingBufferLogEventInstrumentation繼承了ClassInstanceMethodsEnhancePluginDefine,它使用org.apache.skywalking.apm.toolkit.activation.log.log4j.v2.x.async.RingBufferLogEventMethodInterceptor增強了org.apache.logging.log4j.core.async.RingBufferLogEvent的setMessage方法

RingBufferLogEventMethodInterceptor

skywalking-6.6.0/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/log/log4j/v2/x/async/RingBufferLogEventMethodInterceptor.java

<code>public class RingBufferLogEventMethodInterceptor implements InstanceMethodsAroundInterceptor {


  @Override
  public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class>[] argumentsTypes,
                            MethodInterceptResult result) throws Throwable {
      objInst.setSkyWalkingDynamicField(ContextManager.getGlobalTraceId());
  }

  @Override
  public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
                            Class>[] argumentsTypes, Object ret) throws Throwable {
      return ret;
  }

  @Override
  public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
                                    Class>[] argumentsTypes, Throwable t) {
  }
}/<code>
  • RingBufferLogEventMethodInterceptor實現了InstanceMethodsAroundInterceptor接口,其beforeMethod方法將ContextManager.getGlobalTraceId()設置給objInst的skyWalkingDynamicField

小結

skywalking的log4j2-activation定義了TraceIdConverterActivation、AsyncLoggerConfigInstrumentation、Log4jLogEventInstrumentation、RingBufferLogEventInstrumentation這幾個增強

doc

  • TraceIdConverterActivation
  • AsyncLoggerConfigInstrumentation
  • Log4jLogEventInstrumentation
  • RingBufferLogEventInstrumentation


分享到:


相關文章: