Commit aa10ce79 authored by 吴晟's avatar 吴晟 Committed by GitHub
Browse files

Merge pull request #98 from ascrutae/feature/3.0

fix issue
parents 507b6b06 21696f13
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ public abstract class NoCocurrencyAceessObject implements InstanceMethodsAroundI
        if(++counter == 1){
            runnable.run();
        }
        context.set(invokeCounterKey, counter);
    }

    public void whenExist(EnhancedClassInstanceContext context, Runnable runnable) {
@@ -42,5 +43,6 @@ public abstract class NoCocurrencyAceessObject implements InstanceMethodsAroundI
        if(--counter == 0){
            runnable.run();
        }
        context.set(invokeCounterKey, counter);
    }
}
+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);
+22 −6
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@ import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * {@link HttpClientExecuteInterceptor} transport the trace context by call {@link HttpRequest#setHeader(Header)},
 * The current span tag the {@link Tags#ERROR} if {@link StatusLine#getStatusCode()} is not equals 200.
@@ -24,7 +27,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,
@@ -36,13 +39,12 @@ public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterc
        }
        HttpHost httpHost = (HttpHost) allArguments[0];
        HttpRequest httpRequest = (HttpRequest) allArguments[1];

        Span span = ContextManager.INSTANCE.createSpan(httpRequest.getRequestLine().getUri());
        Span span = createSpan(httpRequest);
        Tags.PEER_PORT.set(span, httpHost.getPort());
        Tags.PEER_HOST.set(span, httpHost.getHostName());
        Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_CLIENT);
        Tags.COMPONENT.set(span, COMPONENT_NAME);
        Tags.URL.set(span, generateURL(httpHost, httpRequest));
        Tags.URL.set(span, generateURL(httpRequest));
        Tags.SPAN_LAYER.asHttp(span);

        ContextCarrier contextCarrier = new ContextCarrier();
@@ -55,8 +57,22 @@ 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();
    private String generateURL(HttpRequest httpRequest) {
        return httpRequest.getRequestLine().getUri();
    }

    /**
     * Create span.
     */
    private Span createSpan(HttpRequest httpRequest) {
        Span span;
        try {
            URL url = new URL(httpRequest.getRequestLine().getUri());
            span = ContextManager.INSTANCE.createSpan(url.getPath());
        } catch (MalformedURLException e) {
            span = ContextManager.INSTANCE.createSpan(httpRequest.getRequestLine().getUri());
        }
        return span;
    }

    @Override
+2 −2
Original line number Diff line number Diff line
@@ -77,7 +77,7 @@ public class HttpClientExecuteInterceptorTest {

            @Override
            public String getUri() {
                return "/test-web/test";
                return "http://127.0.0.1:8080/test-web/test";
            }
        });
        when(httpHost.getPort()).thenReturn(8080);
@@ -151,7 +151,7 @@ public class HttpClientExecuteInterceptorTest {

    private void assertHttpSpan(Span span) {
        assertThat(span.getOperationName(), is("/test-web/test"));
        assertThat(Tags.COMPONENT.get(span), is("Http"));
        assertThat(Tags.COMPONENT.get(span), is("HttpClient"));
        assertThat(Tags.PEER_HOST.get(span), is("127.0.0.1"));
        assertThat(Tags.PEER_PORT.get(span), is(8080));
        assertThat(Tags.URL.get(span), is("http://127.0.0.1:8080/test-web/test"));
+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
    }
}
Loading