sequence

This paper focuses on log4J2-activation of Skywalking

skywalking-plugin.def

Skywalking – 6.6.0 / apm – sniffers/apm – toolkit – the activation/apm – toolkit – log4j – 2. X-ray activation/SRC/main/resources/skywalking – plugi n.def

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
Copy the code
  • Defines TraceIdConverterActivation skywalking log4j2 – activation, AsyncLoggerConfigInstrumentation, Log4jLogEventInstrumentation, RingBufferLogEventInstrumentation these enhancements

TraceIdConverterActivation

Skywalking – 6.6.0 / apm – sniffers/apm – toolkit – the activation/apm – toolkit – log4j – 2. X-ray activation/SRC/main/Java/org/apache/skywalking /apm/toolkit/activation/log/log4j/v2/x/TraceIdConverterActivation.java

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 ClassMatchenhanceClass() {
        returnbyName(ENHANCE_CLASS); }}Copy the code
  • TraceIdConverterActivation inherited ClassInstanceMethodsEnhancePluginDefine, It USES org. Apache. Skywalking. Apm. Toolkit. Activation. The log4j. V2. X.T raceIdConverterMethodInterceptor enhances the org. Apache. Skywalking . Apm. Toolkit. The log. Log4j. V2, x.T raceIdConverter format method

TraceIdConverterMethodInterceptor

Skywalking – 6.6.0 / apm – sniffers/apm – toolkit – the activation/apm – toolkit – log4j – 2. X-ray activation/SRC/main/Java/org/apache/skywalking /apm/toolkit/activation/log/log4j/v2/x/TraceIdConverterMethodInterceptor.java

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 {returnret; } @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<? >[] argumentsTypes, Throwable t) { } }Copy the code
  • TraceIdConverterMethodInterceptor InstanceMethodsAroundInterceptor interface is achieved, The beforeMethod methods mainly from ContextManager. GetGlobalTraceId get tid (), and then give allArguments [1]TID:Tid value of prefix

AsyncLoggerConfigInstrumentation

Skywalking – 6.6.0 / apm – sniffers/apm – toolkit – the activation/apm – toolkit – log4j – 2. X-ray activation/SRC/main/Java/org/apache/skywalking /apm/toolkit/activation/log/log4j/v2/x/async/AsyncLoggerConfigInstrumentation.java

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 ClassMatchenhanceClass() {
        returnbyName(ENHANCE_CLASS); }}Copy the code
  • AsyncLoggerConfigInstrumentation inherited ClassInstanceMethodsEnhancePluginDefine, It USES the org. Apache. Skywalking. Apm. Toolkit. Activation. The log4j. V2. X.a sync. AsyncLoggerConfigMethodInterceptor enhanced org. Apache. Log Ging. Log4j. Core. Async. AsyncLoggerConfig populateLazilyInitializedFields method

AsyncLoggerConfigMethodInterceptor

Skywalking – 6.6.0 / apm – sniffers/apm – toolkit – the activation/apm – toolkit – log4j – 2. X-ray activation/SRC/main/Java/org/apache/skywalking /apm/toolkit/activation/log/log4j/v2/x/async/AsyncLoggerConfigMethodInterceptor.java

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 {returnret; } @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<? >[] argumentsTypes, Throwable t) { } }Copy the code
  • AsyncLoggerConfigMethodInterceptor InstanceMethodsAroundInterceptor interface is achieved, The beforeMethod methods will ContextManager. GetGlobalTraceId () set to allArguments skyWalkingDynamicField [0]

Log4jLogEventInstrumentation

Skywalking – 6.6.0 / apm – sniffers/apm – toolkit – the activation/apm – toolkit – log4j – 2. X-ray activation/SRC/main/Java/org/apache/skywalking /apm/toolkit/activation/log/log4j/v2/x/async/Log4jLogEventInstrumentation.java

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"); }}Copy the code
  • Log4jLogEventInstrumentation inherited ClassInstanceMethodsEnhancePluginDefine, It USES the org. Apache. Skywalking. Apm. Toolkit. Activation. The log4j. V2. X.a sync. Log4jLogEventConstructorInterceptor enhanced org. Apache. Lo Gging. Log4j. Core. Impl. Log4jLogEvent constructor

Log4jLogEventConstructorInterceptor

Skywalking – 6.6.0 / apm – sniffers/apm – toolkit – the activation/apm – toolkit – log4j – 2. X-ray activation/SRC/main/Java/org/apache/skywalking /apm/toolkit/activation/log/log4j/v2/x/async/Log4jLogEventConstructorInterceptor.java

public class Log4jLogEventConstructorInterceptor implements InstanceConstructorInterceptor {

    @Override
    public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
    }
}
Copy the code
  • Log4jLogEventConstructorInterceptor InstanceConstructorInterceptor interface is achieved, the onConstruct methods there is no operation

RingBufferLogEventInstrumentation

Skywalking – 6.6.0 / apm – sniffers/apm – toolkit – the activation/apm – toolkit – log4j – 2. X-ray activation/SRC/main/Java/org/apache/skywalking /apm/toolkit/activation/log/log4j/v2/x/async/RingBufferLogEventInstrumentation.java

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 ClassMatchenhanceClass() {
        returnbyName(ENHANCE_CLASS); }}Copy the code
  • RingBufferLogEventInstrumentation inherited ClassInstanceMethodsEnhancePluginDefine, It USES the org. Apache. Skywalking. Apm. Toolkit. Activation. The log4j. V2. X.a sync. RingBufferLogEventMethodInterceptor enhanced org. Apache. Lo Gging. Log4j. Core. Async. RingBufferLogEvent setMessage method

RingBufferLogEventMethodInterceptor

Skywalking – 6.6.0 / apm – sniffers/apm – toolkit – the activation/apm – toolkit – log4j – 2. X-ray activation/SRC/main/Java/org/apache/skywalking /apm/toolkit/activation/log/log4j/v2/x/async/RingBufferLogEventMethodInterceptor.java

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 {returnret; } @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<? >[] argumentsTypes, Throwable t) { } }Copy the code
  • RingBufferLogEventMethodInterceptor InstanceMethodsAroundInterceptor interface is achieved, The beforeMethod method will ContextManager. GetGlobalTraceId () set to objInst skyWalkingDynamicField

summary

Defines TraceIdConverterActivation skywalking log4j2 – activation, AsyncLoggerConfigInstrumentation, Log4jLogEventInstrumentation, RingBufferLogEventInstrumentation these enhancements

doc

  • TraceIdConverterActivation
  • AsyncLoggerConfigInstrumentation
  • Log4jLogEventInstrumentation
  • RingBufferLogEventInstrumentation