From 7553dff4df64b321d863cadf61ccf6ed542f3c23 Mon Sep 17 00:00:00 2001 From: wusheng Date: Sun, 11 Dec 2016 20:41:59 +0800 Subject: [PATCH] set a new interceptor point for log4j2 to output traceid. --- .../log4j/v2/x/Log4j2OutputAppender.java | 10 +++++ .../toolkit/log4j/v2/x/TraceIdConverter.java | 2 +- .../v2/x/Log4j2OutputAppenderActivation.java | 31 ++++++++++++++++ .../log4j/v2/x/PrintTraceIdInterceptor.java | 14 +++---- .../v2/x/TraceIdConverterActivation.java | 37 ------------------- .../src/main/resources/skywalking-plugin.def | 2 +- 6 files changed, 49 insertions(+), 47 deletions(-) create mode 100644 skywalking-application-toolkit/skywalking-toolkit-log4j-2.x/src/main/java/com/a/eye/skywalking/toolkit/log4j/v2/x/Log4j2OutputAppender.java create mode 100644 skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log4j/v2/x/Log4j2OutputAppenderActivation.java delete mode 100644 skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log4j/v2/x/TraceIdConverterActivation.java diff --git a/skywalking-application-toolkit/skywalking-toolkit-log4j-2.x/src/main/java/com/a/eye/skywalking/toolkit/log4j/v2/x/Log4j2OutputAppender.java b/skywalking-application-toolkit/skywalking-toolkit-log4j-2.x/src/main/java/com/a/eye/skywalking/toolkit/log4j/v2/x/Log4j2OutputAppender.java new file mode 100644 index 000000000..890df092d --- /dev/null +++ b/skywalking-application-toolkit/skywalking-toolkit-log4j-2.x/src/main/java/com/a/eye/skywalking/toolkit/log4j/v2/x/Log4j2OutputAppender.java @@ -0,0 +1,10 @@ +package com.a.eye.skywalking.toolkit.log4j.v2.x; + +/** + * Created by wusheng on 2016/12/11. + */ +public class Log4j2OutputAppender { + public static void append(StringBuilder toAppendTo){ + toAppendTo.append("TID: N/A"); + } +} diff --git a/skywalking-application-toolkit/skywalking-toolkit-log4j-2.x/src/main/java/com/a/eye/skywalking/toolkit/log4j/v2/x/TraceIdConverter.java b/skywalking-application-toolkit/skywalking-toolkit-log4j-2.x/src/main/java/com/a/eye/skywalking/toolkit/log4j/v2/x/TraceIdConverter.java index f01726bc0..046081eef 100644 --- a/skywalking-application-toolkit/skywalking-toolkit-log4j-2.x/src/main/java/com/a/eye/skywalking/toolkit/log4j/v2/x/TraceIdConverter.java +++ b/skywalking-application-toolkit/skywalking-toolkit-log4j-2.x/src/main/java/com/a/eye/skywalking/toolkit/log4j/v2/x/TraceIdConverter.java @@ -29,6 +29,6 @@ public class TraceIdConverter extends LogEventPatternConverter { @Override public void format(LogEvent event, StringBuilder toAppendTo) { - toAppendTo.append("TID: N/A"); + Log4j2OutputAppender.append(toAppendTo); } } diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log4j/v2/x/Log4j2OutputAppenderActivation.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log4j/v2/x/Log4j2OutputAppenderActivation.java new file mode 100644 index 000000000..4a99f4bcf --- /dev/null +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log4j/v2/x/Log4j2OutputAppenderActivation.java @@ -0,0 +1,31 @@ +package com.a.eye.skywalking.toolkit.activation.log4j.v2.x; + +import com.a.eye.skywalking.plugin.interceptor.MethodMatcher; +import com.a.eye.skywalking.plugin.interceptor.StaticMethodsInterceptPoint; +import com.a.eye.skywalking.plugin.interceptor.enhance.ClassStaticMethodsEnhancePluginDefine; +import com.a.eye.skywalking.plugin.interceptor.matcher.SimpleMethodMatcher; + +/** + * Created by wusheng on 2016/12/7. + */ +public class Log4j2OutputAppenderActivation extends ClassStaticMethodsEnhancePluginDefine { + @Override + protected String enhanceClassName() { + return "com.a.eye.skywalking.toolkit.log4j.v2.x.Log4j2OutputAppender"; + } + + @Override + protected StaticMethodsInterceptPoint[] getStaticMethodsInterceptPoints() { + return new StaticMethodsInterceptPoint[]{new StaticMethodsInterceptPoint() { + @Override + public MethodMatcher[] getMethodsMatchers() { + return new MethodMatcher[]{new SimpleMethodMatcher("append")}; + } + + @Override + public String getMethodsInterceptor() { + return "com.a.eye.skywalking.toolkit.activation.log4j.v2.x.PrintTraceIdInterceptor"; + } + }}; + } +} diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log4j/v2/x/PrintTraceIdInterceptor.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log4j/v2/x/PrintTraceIdInterceptor.java index 3aee17f0b..6fd22bcc5 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log4j/v2/x/PrintTraceIdInterceptor.java +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log4j/v2/x/PrintTraceIdInterceptor.java @@ -2,16 +2,14 @@ package com.a.eye.skywalking.toolkit.activation.log4j.v2.x; import com.a.eye.skywalking.api.Tracing; import com.a.eye.skywalking.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; -import com.a.eye.skywalking.plugin.interceptor.enhance.MethodInterceptResult; +import com.a.eye.skywalking.plugin.interceptor.enhance.*; /** * Created by wusheng on 2016/12/7. */ -public class PrintTraceIdInterceptor implements InstanceMethodsAroundInterceptor { +public class PrintTraceIdInterceptor implements StaticMethodsAroundInterceptor { @Override - public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, MethodInterceptResult result) { + public void beforeMethod(StaticMethodInvokeContext interceptorContext, MethodInterceptResult result) { ((StringBuilder)interceptorContext.allArguments()[1]).append("TID:" + Tracing.getTraceId()); //make sure origin method do not invoke. @@ -19,12 +17,12 @@ public class PrintTraceIdInterceptor implements InstanceMethodsAroundInterceptor } @Override - public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) { - return ret; + public Object afterMethod(StaticMethodInvokeContext interceptorContext, Object ret) { + return null; } @Override - public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext) { + public void handleMethodException(Throwable t, MethodInvokeContext interceptorContext) { } } diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log4j/v2/x/TraceIdConverterActivation.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log4j/v2/x/TraceIdConverterActivation.java deleted file mode 100644 index 233ae4f4a..000000000 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log4j/v2/x/TraceIdConverterActivation.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.a.eye.skywalking.toolkit.activation.log4j.v2.x; - -import com.a.eye.skywalking.plugin.interceptor.ConstructorInterceptPoint; -import com.a.eye.skywalking.plugin.interceptor.InstanceMethodsInterceptPoint; -import com.a.eye.skywalking.plugin.interceptor.MethodMatcher; -import com.a.eye.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; -import com.a.eye.skywalking.plugin.interceptor.matcher.SimpleMethodMatcher; - -/** - * Created by wusheng on 2016/12/7. - */ -public class TraceIdConverterActivation extends ClassInstanceMethodsEnhancePluginDefine { - @Override - protected String enhanceClassName() { - return "com.a.eye.skywalking.toolkit.log4j.v2.x.TraceIdConverter"; - } - - @Override - protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { - return null; - } - - @Override - protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { - return new InstanceMethodsInterceptPoint[]{new InstanceMethodsInterceptPoint() { - @Override - public MethodMatcher[] getMethodsMatchers() { - return new MethodMatcher[]{new SimpleMethodMatcher("format")}; - } - - @Override - public String getMethodsInterceptor() { - return "com.a.eye.skywalking.toolkit.activation.log4j.v2.x.PrintTraceIdInterceptor"; - } - }}; - } -} diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/resources/skywalking-plugin.def b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/resources/skywalking-plugin.def index bba23263e..8fc2dd750 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/resources/skywalking-plugin.def +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/resources/skywalking-plugin.def @@ -1 +1 @@ -com.a.eye.skywalking.toolkit.activation.log4j.v2.x.TraceIdConverterActivation +com.a.eye.skywalking.toolkit.activation.log4j.v2.x.Log4j2OutputAppenderActivation -- GitLab