StaticMethodsAroundInterceptor Failure #10444
-
I want to intercept the get method, org.slf4j.MDC#get,But it didn't work.Skywalking version v.8.14.0public class MDCTraceIdActivation extends ClassStaticMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "org.slf4j.MDC";
private static final String ENHANCE_NEW_SCOPE_METHOD = "get";
private static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.mdc.MDCTraceIdInterceptor";
public MDCTraceIdActivation() {
System.err.println("init activation");
}
@Override
protected ClassMatch enhanceClass() {
return NameMatch.byName(ENHANCE_CLASS);
}
@Override
public StaticMethodsInterceptPoint[] getStaticMethodsInterceptPoints() {
return new StaticMethodsInterceptPoint[]{
new StaticMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ENHANCE_NEW_SCOPE_METHOD);
}
@Override
public String getMethodsInterceptor() {
return INTERCEPT_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
}
};
}
}public class MDCTraceIdInterceptor implements StaticMethodsAroundInterceptor {
private static final String TRACE_ID = "traceId";
private static final String OLD_TRACE_ID = "X-B3-TraceId";
private static final String SPAN_ID = "spanId";
private static final String OLD_SPAN_ID = "X-B3-SpanId";
public MDCTraceIdInterceptor() {
System.err.println("init interceptor");
}
@Override
public void beforeMethod(Class aClass, Method method, Object[] objects, Class<?>[] classes, MethodInterceptResult methodInterceptResult) {
System.err.println("entry before");
if (MDCConfig.Plugin.Mdc.OVERWRITE_TRACE) {
System.err.println("entry if");
String key = (String) objects[0];
System.err.printf("key: %s\n", key);
if (TRACE_ID.equals(key) || OLD_TRACE_ID.equals(key)) {
String value = ContextManager.getGlobalTraceId();
System.err.printf("trace %s: %s\n", key, value);
methodInterceptResult.defineReturnValue(value);
} else if (SPAN_ID.equals(key) || OLD_SPAN_ID.equals(key)) {
String value = ContextManager.getSegmentId();
System.err.printf("span %s: %s\n", key, value);
methodInterceptResult.defineReturnValue(value);
}
}
}
@Override
public Object afterMethod(Class aClass, Method method, Object[] objects, Class<?>[] classes, Object o) {
System.err.println("after method ");
return o;
}
@Override
public void handleMethodException(Class aClass, Method method, Object[] objects, Class<?>[] classes, Throwable throwable) {
System.err.println("error");
}
}public class MDCConfig {
public static class Plugin {
@PluginConfig(root = MDCConfig.class)
public static class Mdc {
public static boolean OVERWRITE_TRACE = true;
}
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
lujiajing1126
Feb 24, 2023
Replies: 1 comment 1 reply
-
|
Pls check the code, |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
rocasx
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pls check the code,
org.slf4jis not allowed to be intercepted as it is used in the kernel of the Agent.