From 21696f132fc0b6f55ab2749153bbaa6fb82d4ee2 Mon Sep 17 00:00:00 2001 From: ascrutae Date: Wed, 8 Mar 2017 21:31:29 +0800 Subject: [PATCH] fix http client plugin test failure issue --- .../v4/HttpClientExecuteInterceptor.java | 24 +++++++++++++++---- .../v4/HttpClientExecuteInterceptorTest.java | 4 ++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/skywalking-sniffer/skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/com/a/eye/skywalking/plugin/httpClient/v4/HttpClientExecuteInterceptor.java b/skywalking-sniffer/skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/com/a/eye/skywalking/plugin/httpClient/v4/HttpClientExecuteInterceptor.java index c801f66c7..e2bf4650e 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/com/a/eye/skywalking/plugin/httpClient/v4/HttpClientExecuteInterceptor.java +++ b/skywalking-sniffer/skywalking-sdk-plugin/httpClient-4.x-plugin/src/main/java/com/a/eye/skywalking/plugin/httpClient/v4/HttpClientExecuteInterceptor.java @@ -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. @@ -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,10 +57,24 @@ public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterc * * @return request URL */ - private String generateURL(HttpHost httpHost, HttpRequest httpRequest) { + 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 public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) { diff --git a/skywalking-sniffer/skywalking-sdk-plugin/httpClient-4.x-plugin/src/test/java/com/a/eye/skywalking/plugin/httpClient/v4/HttpClientExecuteInterceptorTest.java b/skywalking-sniffer/skywalking-sdk-plugin/httpClient-4.x-plugin/src/test/java/com/a/eye/skywalking/plugin/httpClient/v4/HttpClientExecuteInterceptorTest.java index 82e2a1d42..d8c7f3faa 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/httpClient-4.x-plugin/src/test/java/com/a/eye/skywalking/plugin/httpClient/v4/HttpClientExecuteInterceptorTest.java +++ b/skywalking-sniffer/skywalking-sdk-plugin/httpClient-4.x-plugin/src/test/java/com/a/eye/skywalking/plugin/httpClient/v4/HttpClientExecuteInterceptorTest.java @@ -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"));