set a new interceptor point for log4j2 to output traceid.

This commit is contained in:
wusheng 2016-12-11 20:41:59 +08:00
parent 7545ebcc77
commit 7553dff4df
6 changed files with 49 additions and 47 deletions

View File

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

View File

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

View File

@ -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";
}
}};
}
}

View File

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

View File

@ -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";
}
}};
}
}

View File

@ -1 +1 @@
com.a.eye.skywalking.toolkit.activation.log4j.v2.x.TraceIdConverterActivation
com.a.eye.skywalking.toolkit.activation.log4j.v2.x.Log4j2OutputAppenderActivation