From 316c09b01232ae4ce1709ce88a95df4e4db557d4 Mon Sep 17 00:00:00 2001 From: zhangxin Date: Wed, 22 Feb 2017 21:47:36 +0800 Subject: [PATCH] Modify the motan plugin using the new API --- .../plugin/motan/MotanBuriedPointType.java | 19 ---- .../motan/MotanClientCallInterceptor.java | 60 ------------ .../MotanClientFetchCallURLInterceptor.java | 29 ------ .../plugin/motan/MotanServerInterceptor.java | 57 ------------ .../motan/define/MotanClientDefine.java | 47 ---------- .../motan/define/MotanServerDefine.java | 49 ---------- .../ConsumerFetchRequestURLInterceptor.java | 39 ++++++++ .../motan/ConsumerInvokeInterceptor.java | 93 +++++++++++++++++++ .../plugin/motan/ProviderInterceptor.java | 88 ++++++++++++++++++ .../motan/define/ConsumerInstrumentation.java | 60 ++++++++++++ .../motan/define/ProviderInstrumentation.java | 69 ++++++++++++++ .../src/main/resources/skywalking-plugin.def | 4 +- 12 files changed, 351 insertions(+), 263 deletions(-) delete mode 100644 skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/MotanBuriedPointType.java delete mode 100644 skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/MotanClientCallInterceptor.java delete mode 100644 skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/MotanClientFetchCallURLInterceptor.java delete mode 100644 skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/MotanServerInterceptor.java delete mode 100644 skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/define/MotanClientDefine.java delete mode 100644 skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/define/MotanServerDefine.java create mode 100644 skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/ConsumerFetchRequestURLInterceptor.java create mode 100644 skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/ConsumerInvokeInterceptor.java create mode 100644 skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/ProviderInterceptor.java create mode 100644 skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/define/ConsumerInstrumentation.java create mode 100644 skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/define/ProviderInstrumentation.java diff --git a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/MotanBuriedPointType.java b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/MotanBuriedPointType.java deleted file mode 100644 index 766351815..000000000 --- a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/MotanBuriedPointType.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.a.eye.skywalking.api.plugin.motan; - -import com.a.eye.skywalking.api.IBuriedPointType; - -public enum MotanBuriedPointType implements IBuriedPointType { - - INSTANCE; - - @Override - public String getTypeName() { - return "MO"; - } - - @Override - public CallType getCallType() { - return CallType.SYNC; - } - -} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/MotanClientCallInterceptor.java b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/MotanClientCallInterceptor.java deleted file mode 100644 index c06cb9ba6..000000000 --- a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/MotanClientCallInterceptor.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.a.eye.skywalking.api.plugin.motan; - -import com.a.eye.skywalking.invoke.monitor.RPCClientInvokeMonitor; -import com.a.eye.skywalking.model.ContextData; -import com.a.eye.skywalking.model.Identification; -import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.ConstructorInvokeContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; -import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; -import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; -import com.weibo.api.motan.rpc.Request; -import com.weibo.api.motan.rpc.URL; - -/** - * Motan client interceptor - */ -public class MotanClientCallInterceptor implements InstanceMethodsAroundInterceptor{ - - private static final String REQUEST_URL = "REQUEST_URL"; - - @Override - public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, - MethodInterceptResult result) { - URL url = (URL) context.get(REQUEST_URL); - Request request = (Request) interceptorContext.allArguments()[0]; - if (url != null) { - ContextData contextData = new RPCClientInvokeMonitor() - .beforeInvoke(generateIdentify(request, url)); - String contextDataStr = contextData.toString(); - request.setAttachment("contextData", contextDataStr); - } - } - - @Override - public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, - Object ret) { - new RPCClientInvokeMonitor().afterInvoke(); - return ret; - } - - @Override - public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, - InstanceMethodInvokeContext interceptorContext) { - new RPCClientInvokeMonitor().occurException(t); - } - - - private static String generateViewPoint(URL serviceURI, Request request) { - StringBuilder viewPoint = new StringBuilder(serviceURI.getUri()); - viewPoint.append("." + request.getMethodName()); - viewPoint.append("(" + request.getParamtersDesc() + ")?group=" + serviceURI.getGroup()); - return viewPoint.toString(); - } - - - public static Identification generateIdentify(Request request, URL serviceURI) { - return Identification.newBuilder().viewPoint(generateViewPoint(serviceURI, request)) - .spanType(MotanBuriedPointType.INSTANCE).build(); - } -} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/MotanClientFetchCallURLInterceptor.java b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/MotanClientFetchCallURLInterceptor.java deleted file mode 100644 index 637a56116..000000000 --- a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/MotanClientFetchCallURLInterceptor.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.a.eye.skywalking.api.plugin.motan; - -import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; -import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; -import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; - -/** - * Created by xin on 2017/1/23. - */ -public class MotanClientFetchCallURLInterceptor implements InstanceMethodsAroundInterceptor { - - private static final String REQUEST_URL = "REQUEST_URL"; - - @Override - public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, MethodInterceptResult result) { - context.set(REQUEST_URL, interceptorContext.allArguments()[0]); - } - - @Override - public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) { - return ret; - } - - @Override - public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext) { - - } -} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/MotanServerInterceptor.java b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/MotanServerInterceptor.java deleted file mode 100644 index 9a1570aee..000000000 --- a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/MotanServerInterceptor.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.a.eye.skywalking.api.plugin.motan; - -import com.a.eye.skywalking.invoke.monitor.RPCServerInvokeMonitor; -import com.a.eye.skywalking.model.ContextData; -import com.a.eye.skywalking.model.Identification; -import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.ConstructorInvokeContext; -import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceConstructorInterceptor; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; -import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; -import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; -import com.weibo.api.motan.rpc.Request; -import com.weibo.api.motan.rpc.URL; - -public class MotanServerInterceptor implements InstanceConstructorInterceptor, InstanceMethodsAroundInterceptor { - @Override - public void onConstruct(EnhancedClassInstanceContext context, ConstructorInvokeContext interceptorContext) { - context.set("serviceURI", interceptorContext.allArguments()[0]); - } - - @Override - public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, - MethodInterceptResult result) { - com.weibo.api.motan.rpc.Request request = (com.weibo.api.motan.rpc.Request) interceptorContext.allArguments()[0]; - if (request != null) { - new RPCServerInvokeMonitor().beforeInvoke(new ContextData(request.getAttachments().get("contextData")), - generateIdentify(request, (com.weibo.api.motan.rpc.URL) context.get("serviceURI"))); - } - } - - @Override - public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, - Object ret) { - new RPCServerInvokeMonitor().afterInvoke(); - return ret; - } - - @Override - public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, - InstanceMethodInvokeContext interceptorContext) { - new RPCServerInvokeMonitor().occurException(t); - } - - - private static String generateViewPoint(URL serviceURI, Request request) { - StringBuilder viewPoint = new StringBuilder(serviceURI.getUri()); - viewPoint.append("." + request.getMethodName()); - viewPoint.append("(" + request.getParamtersDesc() + ")?group=" + serviceURI.getGroup()); - return viewPoint.toString(); - } - - - public static Identification generateIdentify(Request request, URL serviceURI) { - return Identification.newBuilder().viewPoint(generateViewPoint(serviceURI, request)) - .spanType(MotanBuriedPointType.INSTANCE).build(); - } -} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/define/MotanClientDefine.java b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/define/MotanClientDefine.java deleted file mode 100644 index e838dda1e..000000000 --- a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/define/MotanClientDefine.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.a.eye.skywalking.api.plugin.motan.define; - -import com.a.eye.skywalking.api.plugin.interceptor.InstanceMethodsInterceptPoint; -import com.a.eye.skywalking.plugin.interceptor.ConstructorInterceptPoint; -import com.a.eye.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; - -import net.bytebuddy.description.method.MethodDescription; -import net.bytebuddy.matcher.ElementMatcher; - -import static net.bytebuddy.matcher.ElementMatchers.named; - -public class MotanClientDefine extends ClassInstanceMethodsEnhancePluginDefine { - @Override - protected String enhanceClassName() { - return "com.weibo.api.motan.cluster.support.ClusterSpi"; - } - - @Override - protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { - return new ConstructorInterceptPoint[0]; - } - - @Override - protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { - return new InstanceMethodsInterceptPoint[]{new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("setUrl"); - } - - @Override - public String getMethodsInterceptor() { - return "com.a.eye.skywalking.plugin.motan.MotanClientFetchCallURLInterceptor"; - } - }, new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("call"); - } - - @Override - public String getMethodsInterceptor() { - return "com.a.eye.skywalking.plugin.motan.MotanClientCallInterceptor"; - } - }}; - } -} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/define/MotanServerDefine.java b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/define/MotanServerDefine.java deleted file mode 100644 index 4649f3a40..000000000 --- a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/api/plugin/motan/define/MotanServerDefine.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.a.eye.skywalking.api.plugin.motan.define; - -import com.a.eye.skywalking.api.plugin.interceptor.ConstructorInterceptPoint; -import com.a.eye.skywalking.api.plugin.interceptor.InstanceMethodsInterceptPoint; -import com.a.eye.skywalking.plugin.interceptor.ConstructorInterceptPoint; -import com.a.eye.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; -import net.bytebuddy.description.method.MethodDescription; -import net.bytebuddy.matcher.ElementMatcher; - -import static net.bytebuddy.matcher.ElementMatchers.any; -import static net.bytebuddy.matcher.ElementMatchers.named; - -public class MotanServerDefine extends ClassInstanceMethodsEnhancePluginDefine { - - @Override - protected String enhanceClassName() { - return "com.weibo.api.motan.rpc.AbstractProvider"; - } - - @Override - protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { - return new ConstructorInterceptPoint[] {new ConstructorInterceptPoint() { - @Override - public ElementMatcher getConstructorMatcher() { - return any(); - } - - @Override - public String getConstructorInterceptor() { - return "com.a.eye.skywalking.plugin.motan.MotanServerInterceptor"; - } - }}; - } - - @Override - protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { - return new InstanceMethodsInterceptPoint[] {new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("call"); - } - - @Override - public String getMethodsInterceptor() { - return "com.a.eye.skywalking.plugin.motan.MotanServerInterceptor"; - } - }}; - } -} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/ConsumerFetchRequestURLInterceptor.java b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/ConsumerFetchRequestURLInterceptor.java new file mode 100644 index 000000000..214f9b537 --- /dev/null +++ b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/ConsumerFetchRequestURLInterceptor.java @@ -0,0 +1,39 @@ +package com.a.eye.skywalking.plugin.motan; + +import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; + +/** + * {@link ConsumerFetchRequestURLInterceptor} record {@link com.weibo.api.motan.rpc.URL} to {@link EnhancedClassInstanceContext#context} + * for the operation name that create span need. + * + * @author zhangxin + */ +public class ConsumerFetchRequestURLInterceptor implements InstanceMethodsAroundInterceptor { + + private static final String CONTEXT_NAME_OF_REQUEST_URL = "REQUEST_URL"; + + /** + * Fetch the request url from the first param of all constructor, and put request url into {@link EnhancedClassInstanceContext#context}. + * + * @param context instance context, a class instance only has one {@link EnhancedClassInstanceContext} instance. + * @param interceptorContext method context, includes class name, method name, etc. + * @param result change this result, if you want to truncate the method. + */ + @Override + public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, MethodInterceptResult result) { + context.set(CONTEXT_NAME_OF_REQUEST_URL, interceptorContext.allArguments()[0]); + } + + @Override + public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) { + return ret; + } + + @Override + public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext) { + // do nothing + } +} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/ConsumerInvokeInterceptor.java b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/ConsumerInvokeInterceptor.java new file mode 100644 index 000000000..a0d269888 --- /dev/null +++ b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/ConsumerInvokeInterceptor.java @@ -0,0 +1,93 @@ +package com.a.eye.skywalking.plugin.motan; + +import com.a.eye.skywalking.api.context.ContextCarrier; +import com.a.eye.skywalking.api.context.ContextManager; +import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; +import com.a.eye.skywalking.trace.Span; +import com.a.eye.skywalking.trace.tag.Tags; +import com.weibo.api.motan.rpc.Request; +import com.weibo.api.motan.rpc.Response; +import com.weibo.api.motan.rpc.URL; + +/** + * {@link ConsumerInvokeInterceptor} create span by fetch request url from {@link EnhancedClassInstanceContext#context} and + * transport serialized context data to provider side through {@link Request#setAttachment(String, String)}. + * + * @author zhangxin + */ +public class ConsumerInvokeInterceptor implements InstanceMethodsAroundInterceptor { + + /** + * Context name of request url in {@link EnhancedClassInstanceContext#context} + */ + private static final String CONTEXT_NAME_OF_REQUEST_URL = "REQUEST_URL"; + + /** + * Attachment key of the serialized context data + */ + private static final String ATTACHMENT_KEY_OF_CONTEXT_DATA = "contextData"; + + /** + * Motan component + */ + private static final String MOTAN_COMPONENT = "Motan"; + + @Override + public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, + MethodInterceptResult result) { + URL url = (URL) context.get(CONTEXT_NAME_OF_REQUEST_URL); + + if (url != null) { + Request request = (Request) interceptorContext.allArguments()[0]; + + Span span = ContextManager.INSTANCE.createSpan(generateOperationName(url, request)); + Tags.PEER_HOST.set(span, url.getHost()); + Tags.PEER_PORT.set(span, url.getPort()); + Tags.COMPONENT.set(span, MOTAN_COMPONENT); + Tags.URL.set(span, url.getIdentity()); + Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_CLIENT); + Tags.SPAN_LAYER.asRPCFramework(span); + + ContextCarrier contextCarrier = new ContextCarrier(); + ContextManager.INSTANCE.inject(contextCarrier); + request.setAttachment(ATTACHMENT_KEY_OF_CONTEXT_DATA, contextCarrier.serialize()); + } + } + + @Override + public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, + Object ret) { + Response response = (Response) ret; + if (response != null && response.getException() != null) { + Span span = ContextManager.INSTANCE.activeSpan(); + span.log(response.getException()); + Tags.ERROR.set(span, true); + } + + ContextManager.INSTANCE.stopSpan(); + return ret; + } + + @Override + public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, + InstanceMethodInvokeContext interceptorContext) { + ContextManager.INSTANCE.activeSpan().log(t); + } + + + /** + * Generate operation name. + * + * @return operation name + */ + private static String generateOperationName(URL serviceURI, Request request) { + StringBuilder viewPoint = new StringBuilder(serviceURI.getPath()); + viewPoint.append("." + request.getMethodName()); + viewPoint.append("(" + request.getParamtersDesc() + ")"); + return viewPoint.toString(); + } + +} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/ProviderInterceptor.java b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/ProviderInterceptor.java new file mode 100644 index 000000000..0413cb985 --- /dev/null +++ b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/ProviderInterceptor.java @@ -0,0 +1,88 @@ +package com.a.eye.skywalking.plugin.motan; + +import com.a.eye.skywalking.api.context.ContextCarrier; +import com.a.eye.skywalking.api.context.ContextManager; +import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.ConstructorInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceConstructorInterceptor; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; +import com.a.eye.skywalking.trace.Span; +import com.a.eye.skywalking.trace.tag.Tags; +import com.weibo.api.motan.rpc.Request; +import com.weibo.api.motan.rpc.Response; +import com.weibo.api.motan.rpc.URL; + +/** + * Current trace segment will ref the trace segment from previous level if the serialized context data that fetch + * from {@link Request#getAttachments()} is not null. + * + * {@link ProviderInterceptor} intercept all constructor of {@link com.weibo.api.motan.rpc.AbstractProvider} for record + * the request url from consumer side. + * + * @author zhangxin + */ +public class ProviderInterceptor implements InstanceConstructorInterceptor, InstanceMethodsAroundInterceptor { + + /** + * The key name of request url that stored in {@link EnhancedClassInstanceContext#context} + */ + private static final String KEY_NAME_OF_REQUEST_URL = "REQUEST_URL"; + + /** + * The key name that the serialized context data stored in {@link Request#getAttachments()} + */ + private static final String ATTACHMENT_KEY_OF_CONTEXT_DATA = "contextData"; + + @Override + public void onConstruct(EnhancedClassInstanceContext context, ConstructorInvokeContext interceptorContext) { + context.set(KEY_NAME_OF_REQUEST_URL, interceptorContext.allArguments()[0]); + } + + @Override + public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, + MethodInterceptResult result) { + URL url = (URL) context.get(KEY_NAME_OF_REQUEST_URL); + if (url != null) { + com.weibo.api.motan.rpc.Request request = (com.weibo.api.motan.rpc.Request) interceptorContext.allArguments()[0]; + Span span = ContextManager.INSTANCE.createSpan(generateViewPoint(url, request)); + Tags.COMPONENT.set(span, "Motan"); + Tags.URL.set(span, url.getIdentity()); + Tags.PEER_PORT.set(span, url.getPort()); + Tags.PEER_HOST.set(span, url.getHost()); + Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_SERVER); + Tags.SPAN_LAYER.asRPCFramework(span); + + String serializedContextData = request.getAttachments().get(ATTACHMENT_KEY_OF_CONTEXT_DATA); + ContextManager.INSTANCE.extract(new ContextCarrier().deserialize(serializedContextData)); + } + } + + @Override + public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, + Object ret) { + Response response = (Response) ret; + if (response != null && response.getException() != null) { + Span span = ContextManager.INSTANCE.activeSpan(); + Tags.ERROR.set(span, true); + span.log(response.getException()); + } + return ret; + } + + @Override + public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, + InstanceMethodInvokeContext interceptorContext) { + ContextManager.INSTANCE.activeSpan().log(t); + } + + + private static String generateViewPoint(URL serviceURI, Request request) { + StringBuilder viewPoint = new StringBuilder(serviceURI.getUri()); + viewPoint.append("." + request.getMethodName()); + viewPoint.append("(" + request.getParamtersDesc() + ")"); + return viewPoint.toString(); + } + +} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/define/ConsumerInstrumentation.java b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/define/ConsumerInstrumentation.java new file mode 100644 index 000000000..cf90c0f1a --- /dev/null +++ b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/define/ConsumerInstrumentation.java @@ -0,0 +1,60 @@ +package com.a.eye.skywalking.plugin.motan.define; + +import com.a.eye.skywalking.api.plugin.interceptor.ConstructorInterceptPoint; +import com.a.eye.skywalking.api.plugin.interceptor.InstanceMethodsInterceptPoint; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import com.a.eye.skywalking.plugin.motan.ConsumerFetchRequestURLInterceptor; +import com.a.eye.skywalking.plugin.motan.ConsumerInvokeInterceptor; +import com.weibo.api.motan.rpc.Request; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; + +import static net.bytebuddy.matcher.ElementMatchers.named; + +/** + * {@link ConsumerInstrumentation} presents that skywalking use {@link ConsumerInvokeInterceptor} + * to intercept {@link com.weibo.api.motan.cluster.support.ClusterSpi#call(Request)} and use {@link ConsumerFetchRequestURLInterceptor} + * to intercept{@link ConsumerFetchRequestURLInterceptor} + */ +public class ConsumerInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.weibo.api.motan.cluster.support.ClusterSpi"; + private static final String FETCH_REQUEST_URL_INTERCEPT_CLASS = "com.a.eye.skywalking.plugin.motan.ConsumerFetchRequestURLInterceptor"; + private static final String INVOKE_INTECEPT_CLASS = "com.a.eye.skywalking.plugin.motan.ConsumerInvokeInterceptor"; + + @Override + protected String enhanceClassName() { + return ENHANCE_CLASS; + } + + @Override + protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("setUrl"); + } + + @Override + public String getMethodsInterceptor() { + return FETCH_REQUEST_URL_INTERCEPT_CLASS; + } + }, new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("call"); + } + + @Override + public String getMethodsInterceptor() { + return INVOKE_INTECEPT_CLASS; + } + }}; + } +} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/define/ProviderInstrumentation.java b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/define/ProviderInstrumentation.java new file mode 100644 index 000000000..29ee2f2b5 --- /dev/null +++ b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/java/com/a/eye/skywalking/plugin/motan/define/ProviderInstrumentation.java @@ -0,0 +1,69 @@ +package com.a.eye.skywalking.plugin.motan.define; + +import com.a.eye.skywalking.api.plugin.interceptor.ConstructorInterceptPoint; +import com.a.eye.skywalking.api.plugin.interceptor.InstanceMethodsInterceptPoint; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import com.weibo.api.motan.rpc.Request; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; + +import static net.bytebuddy.matcher.ElementMatchers.any; +import static net.bytebuddy.matcher.ElementMatchers.named; + +/** + * {@link ProviderInstrumentation} presents that skywalking will use {@link com.a.eye.skywalking.plugin.motan.ProviderInterceptor} + * to intercept all constructor of {@link com.weibo.api.motan.rpc.AbstractProvider} and {@link com.weibo.api.motan.rpc.AbstractProvider#call(Request)} + * + * @author zhangxin + */ +public class ProviderInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + /** + * Enhance class + */ + private static final String ENHANCE_CLASS = "com.weibo.api.motan.rpc.AbstractProvider"; + /** + * Class that intercept all constructor of ${@link com.weibo.api.motan.rpc.AbstractProvider} + */ + private static final String CONSTRUCTOR_INTERCEPT_CLASS = "com.a.eye.skywalking.plugin.motan.ProviderInterceptor"; + /** + * Class that intercept {@link com.weibo.api.motan.rpc.AbstractProvider#call(Request)} + */ + private static final String PROVIDER_INVOKE_INTERCEPT_CLASS = "com.a.eye.skywalking.plugin.motan.ProviderInterceptor"; + + @Override + protected String enhanceClassName() { + return ENHANCE_CLASS; + } + + @Override + protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[]{new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return CONSTRUCTOR_INTERCEPT_CLASS; + } + }}; + } + + @Override + protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("call"); + } + + @Override + public String getMethodsInterceptor() { + return PROVIDER_INVOKE_INTERCEPT_CLASS; + } + }}; + } +} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/resources/skywalking-plugin.def b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/resources/skywalking-plugin.def index e70bf5478..f223bc720 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/resources/skywalking-plugin.def +++ b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/main/resources/skywalking-plugin.def @@ -1,2 +1,2 @@ -com.a.eye.skywalking.plugin.motan.define.MotanClientDefine -com.a.eye.skywalking.plugin.motan.define.MotanServerDefine +com.a.eye.skywalking.plugin.motan.define.ConsumerInstrumentation +com.a.eye.skywalking.plugin.motan.define.ProviderInstrumentation