Commit 8c5b1e8c authored by ascrutae's avatar ascrutae
Browse files

fix issue

parent d1f320a0
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -54,11 +54,11 @@ public class DubboInterceptor implements InstanceMethodsAroundInterceptor {
        Span span = ContextManager.INSTANCE.createSpan(generateOperationName(requestURL, invocation));
        Tags.URL.set(span, generateRequestURL(requestURL, invocation));
        Tags.COMPONENT.set(span, DUBBO_COMPONENT);
        Tags.PEER_HOST.set(span, requestURL.getHost());
        Tags.PEER_PORT.set(span, requestURL.getPort());
        Tags.SPAN_LAYER.asRPCFramework(span);

        if (isConsumer) {
            Tags.PEER_HOST.set(span, requestURL.getHost());
            Tags.PEER_PORT.set(span, requestURL.getPort());
            Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_CLIENT);
            ContextCarrier contextCarrier = new ContextCarrier();
            ContextManager.INSTANCE.inject(contextCarrier);
+2 −2
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ import org.apache.http.StatusLine;
 */
public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterceptor {
    public static final String HEADER_NAME_OF_CONTEXT_DATA = "SWTraceContext";
    private static final String COMPONENT_NAME = "Http";
    private static final String COMPONENT_NAME = "HttpClient";

    @Override
    public void beforeMethod(EnhancedClassInstanceContext context,
@@ -56,7 +56,7 @@ public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterc
     * @return request URL
     */
    private String generateURL(HttpHost httpHost, HttpRequest httpRequest) {
        return httpHost.getSchemeName() + "://" + httpHost.getHostName() + ":" + httpHost.getPort() + httpRequest.getRequestLine().getUri();
        return httpRequest.getRequestLine().getUri();
    }

    @Override
+0 −40
Original line number Diff line number Diff line
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 MotanConsumerFetchRequestURLInterceptor} record {@link com.weibo.api.motan.rpc.URL} to {@link EnhancedClassInstanceContext#context}
 * for the operation name that create span need.
 *
 * @author zhangxin
 */
public class MotanConsumerFetchRequestURLInterceptor 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
    }
}
+21 −14
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@ 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;
@@ -13,37 +15,41 @@ import com.weibo.api.motan.rpc.Response;
import com.weibo.api.motan.rpc.URL;

/**
 * {@link MotanConsumerInvokeInterceptor} create span by fetch request url from
 * {@link EnhancedClassInstanceContext#context} and transport serialized context
 * data to provider side through {@link Request#setAttachment(String, String)}.
 * 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 MotanConsumerInterceptor} intercept all constructor of {@link com.weibo.api.motan.rpc.AbstractProvider} for record
 * the request url from consumer side.
 *
 * @author zhangxin
 */
public class MotanConsumerInvokeInterceptor implements InstanceMethodsAroundInterceptor {
public class MotanConsumerInterceptor implements InstanceConstructorInterceptor, InstanceMethodsAroundInterceptor {

    /**
     * Context name of request url in {@link EnhancedClassInstanceContext#context}.
     * The
     */
    private static final String CONTEXT_NAME_OF_REQUEST_URL = "REQUEST_URL";
    private static final String KEY_NAME_OF_REQUEST_URL = "REQUEST_URL";

    /**
     * Attachment key of the serialized context data.
     * The {@link Request#getAttachments()} key. It maps to the serialized {@link ContextCarrier}.
     */
    private static final String ATTACHMENT_KEY_OF_CONTEXT_DATA = "SWTraceContext";

    /**
     * Motan component
     */
    private static final String MOTAN_COMPONENT = "Motan";

    @Override
    public void onConstruct(EnhancedClassInstanceContext context, ConstructorInvokeContext interceptorContext) {
        context.set(KEY_NAME_OF_REQUEST_URL, interceptorContext.allArguments()[1]);
    }

    @Override
    public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext,
                             MethodInterceptResult result) {
        URL url = (URL) context.get(CONTEXT_NAME_OF_REQUEST_URL);

        URL url = (URL) context.get(KEY_NAME_OF_REQUEST_URL);
        com.weibo.api.motan.rpc.Request request = (com.weibo.api.motan.rpc.Request) interceptorContext.allArguments()[0];
        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());
@@ -64,10 +70,9 @@ public class MotanConsumerInvokeInterceptor implements InstanceMethodsAroundInte
        Response response = (Response) ret;
        if (response != null && response.getException() != null) {
            Span span = ContextManager.INSTANCE.activeSpan();
            span.log(response.getException());
            Tags.ERROR.set(span, true);
            span.log(response.getException());
        }

        ContextManager.INSTANCE.stopSpan();
        return ret;
    }
@@ -79,6 +84,8 @@ public class MotanConsumerInvokeInterceptor implements InstanceMethodsAroundInte
    }




    /**
     * Generate operation name.
     *
+18 −33
Original line number Diff line number Diff line
@@ -3,8 +3,6 @@ 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;
@@ -13,49 +11,37 @@ 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 MotanProviderInterceptor} intercept all constructor of {@link com.weibo.api.motan.rpc.AbstractProvider} for record
 * the request url from consumer side.
 * {@link MotanProviderInterceptor} 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 MotanProviderInterceptor implements InstanceConstructorInterceptor, InstanceMethodsAroundInterceptor {
public class MotanProviderInterceptor implements InstanceMethodsAroundInterceptor {

    /**
     * The
     * Context name of request url in {@link EnhancedClassInstanceContext#context}.
     */
    private static final String KEY_NAME_OF_REQUEST_URL = "REQUEST_URL";
    private static final String CONTEXT_NAME_OF_REQUEST_URL = "REQUEST_URL";

    /**
     * The {@link Request#getAttachments()} key. It maps to the serialized {@link ContextCarrier}.
     * Attachment key of the serialized context data.
     */
    private static final String ATTACHMENT_KEY_OF_CONTEXT_DATA = "SWTraceContext";

    /**
     * Motan component
     */
    private static final String MOTAN_COMPONENT = "Motan";

    @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];
        Request request = (Request) interceptorContext.allArguments()[0];
        Span span = ContextManager.INSTANCE.createSpan(generateViewPoint(request));
        Tags.COMPONENT.set(span, MOTAN_COMPONENT);
            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);

@@ -64,7 +50,6 @@ public class MotanProviderInterceptor implements InstanceConstructorInterceptor,
            ContextManager.INSTANCE.extract(new ContextCarrier().deserialize(serializedContextData));
        }
    }
    }

    @Override
    public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext,
@@ -72,9 +57,10 @@ public class MotanProviderInterceptor implements InstanceConstructorInterceptor,
        Response response = (Response) ret;
        if (response != null && response.getException() != null) {
            Span span = ContextManager.INSTANCE.activeSpan();
            Tags.ERROR.set(span, true);
            span.log(response.getException());
            Tags.ERROR.set(span, true);
        }

        ContextManager.INSTANCE.stopSpan();
        return ret;
    }
@@ -92,5 +78,4 @@ public class MotanProviderInterceptor implements InstanceConstructorInterceptor,
        viewPoint.append("(" + request.getParamtersDesc() + ")");
        return viewPoint.toString();
    }

}
Loading