专栏首页码匠的流水账聊聊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

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
  • 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

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

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) {
    }
}
  • 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

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

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) {
    }
}
  • 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

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");
    }
}
  • 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

public class Log4jLogEventConstructorInterceptor implements InstanceConstructorInterceptor {

    @Override
    public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
    }
}
  • 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

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

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) {
    }
}
  • RingBufferLogEventMethodInterceptor实现了InstanceMethodsAroundInterceptor接口,其beforeMethod方法将ContextManager.getGlobalTraceId()设置给objInst的skyWalkingDynamicField

小结

skywalking的log4j2-activation定义了TraceIdConverterActivation、AsyncLoggerConfigInstrumentation、Log4jLogEventInstrumentation、RingBufferLogEventInstrumentation这几个增强

doc

  • TraceIdConverterActivation
  • AsyncLoggerConfigInstrumentation
  • Log4jLogEventInstrumentation
  • RingBufferLogEventInstrumentation

本文分享自微信公众号 - 码匠的流水账(geek_luandun),作者:码匠乱炖

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2020-03-20

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊skywalking的log4j2-activation

    skywalking-6.6.0/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activa...

    codecraft
  • 聊聊maxwell的MysqlPositionStore

    maxwell-1.25.1/src/main/java/com/zendesk/maxwell/schema/MysqlPositionStore.java

    codecraft
  • 聊聊RocketMQTransactionAnnotationProcessor

    本文主要研究一下RocketMQTransactionAnnotationProcessor

    codecraft
  • 聊聊skywalking的log4j2-activation

    skywalking-6.6.0/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activa...

    codecraft
  • linux系统下Vi编辑器或者Vim编辑器设置显示行号、自动缩进、调整tab键宽度的技巧?

    工作中嫌vim 中一个tab键的宽度太大,linux系统默认,没改之前是一个tab键宽度是8个字符,想改成4个字符,

    黑泽君
  • Jtro的技术分享:Unity读取excel表格文件生成asset配置表文件

    项目开源地址:https://github.com/641273917/unity_Excel-asset- 许多时候,我们需要通过excel表格来生成我们要...

    LittleU
  • 设计模式之访问者模式(行为型)

    访问者模式:表示一个作用于某对象结构中的各元素的操作,它使我们可以在不改变各元素的类的前提下定义作用于这些元素的新操作。所以访问者模式是一种对象行为型模式。

    SmileNicky
  • python基础:集合-set()

    py3study
  • 写一个Gradle插件

    我们在Android Studio中创建的app项目中,build.gradle常有如下这行代码:

    Clayman Twinkle
  • Android编程设计模式之命令模式详解

    本文实例讲述了Android编程设计模式之命令模式。分享给大家供大家参考,具体如下:

    砸漏

扫码关注云+社区

领取腾讯云代金券