From 8c3d4fe20aa2252b6f700828e4d1df38d69bbfce Mon Sep 17 00:00:00 2001 From: ascrutae Date: Sun, 31 Dec 2017 09:54:08 +0800 Subject: [PATCH] [Agent] Make operation name of fegin plugin more scenes --- .../http/v9/DefaultHttpClientInterceptor.java | 21 +++++++++++-------- .../v9/DefaultHttpClientInterceptorTest.java | 8 +++---- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/feign-default-http-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/feign/http/v9/DefaultHttpClientInterceptor.java b/apm-sniffer/apm-sdk-plugin/feign-default-http-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/feign/http/v9/DefaultHttpClientInterceptor.java index b49b1fd2c..eef05b7ef 100644 --- a/apm-sniffer/apm-sdk-plugin/feign-default-http-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/feign/http/v9/DefaultHttpClientInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/feign-default-http-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/feign/http/v9/DefaultHttpClientInterceptor.java @@ -16,7 +16,6 @@ * */ - package org.apache.skywalking.apm.plugin.feign.http.v9; import feign.Request; @@ -52,9 +51,9 @@ public class DefaultHttpClientInterceptor implements InstanceMethodsAroundInterc private static final String COMPONENT_NAME = "FeignDefaultHttp"; /** - * Get the {@link feign.Request} from {@link EnhancedInstance}, then create {@link AbstractSpan} and set host, - * port, kind, component, url from {@link feign.Request}. - * Through the reflection of the way, set the http header of context data into {@link feign.Request#headers}. + * Get the {@link feign.Request} from {@link EnhancedInstance}, then create {@link AbstractSpan} and set host, port, + * kind, component, url from {@link feign.Request}. Through the reflection of the way, set the http header of + * context data into {@link feign.Request#headers}. * * @param method * @param result change this result, if you want to truncate the method. @@ -66,11 +65,16 @@ public class DefaultHttpClientInterceptor implements InstanceMethodsAroundInterc URL url = new URL(request.url()); ContextCarrier contextCarrier = new ContextCarrier(); - String remotePeer = url.getHost() + ":" + url.getPort(); - AbstractSpan span = ContextManager.createExitSpan(request.url(), contextCarrier, remotePeer); + int port = url.getPort() == -1 ? 80 : url.getPort(); + String remotePeer = url.getHost() + ":" + port; + String operationName = url.getPath(); + if (operationName == null || operationName.length() == 0) { + operationName = "/"; + } + AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, remotePeer); span.setComponent(ComponentsDefine.FEIGN); Tags.HTTP.METHOD.set(span, request.method()); - Tags.URL.set(span, url.getPath()); + Tags.URL.set(span, request.url()); SpanLayer.asHttp(span); Field headersField = Request.class.getDeclaredField("headers"); @@ -94,8 +98,7 @@ public class DefaultHttpClientInterceptor implements InstanceMethodsAroundInterc /** * Get the status code from {@link Response}, when status code greater than 400, it means there was some errors in - * the server. - * Finish the {@link AbstractSpan}. + * the server. Finish the {@link AbstractSpan}. * * @param method * @param ret the method's original return value. diff --git a/apm-sniffer/apm-sdk-plugin/feign-default-http-9.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/feign/http/v9/DefaultHttpClientInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/feign-default-http-9.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/feign/http/v9/DefaultHttpClientInterceptorTest.java index 1c5d30a1a..20f2ccfe9 100644 --- a/apm-sniffer/apm-sdk-plugin/feign-default-http-9.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/feign/http/v9/DefaultHttpClientInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/feign-default-http-9.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/feign/http/v9/DefaultHttpClientInterceptorTest.java @@ -87,7 +87,7 @@ public class DefaultHttpClientInterceptorTest { public void setUp() throws Exception { Map> headers = new LinkedHashMap>(); - request = Request.create("GET", "http://skywalking.org", headers, "Test".getBytes(), Charset.forName("UTF-8")); + request = Request.create("GET", "http://skywalking.org/", headers, "Test".getBytes(), Charset.forName("UTF-8")); Request.Options options = new Request.Options(); allArguments = new Object[] {request, options}; argumentTypes = new Class[] {request.getClass(), options.getClass()}; @@ -112,7 +112,7 @@ public class DefaultHttpClientInterceptorTest { List tags = SpanHelper.getTags(finishedSpan); assertThat(tags.size(), is(2)); assertThat(tags.get(0).getValue(), is("GET")); - assertThat(tags.get(1).getValue(), is("")); + assertThat(tags.get(1).getValue(), is("http://skywalking.org/")); Assert.assertEquals(false, SpanHelper.getErrorOccurred(finishedSpan)); } @@ -135,7 +135,7 @@ public class DefaultHttpClientInterceptorTest { List tags = SpanHelper.getTags(finishedSpan); assertThat(tags.size(), is(3)); assertThat(tags.get(0).getValue(), is("GET")); - assertThat(tags.get(1).getValue(), is("")); + assertThat(tags.get(1).getValue(), is("http://skywalking.org/")); assertThat(tags.get(2).getValue(), is("404")); Assert.assertEquals(true, SpanHelper.getErrorOccurred(finishedSpan)); @@ -166,7 +166,7 @@ public class DefaultHttpClientInterceptorTest { List tags = SpanHelper.getTags(finishedSpan); assertThat(tags.size(), is(2)); assertThat(tags.get(0).getValue(), is("GET")); - assertThat(tags.get(1).getValue(), is("")); + assertThat(tags.get(1).getValue(), is("http://skywalking.org/")); Assert.assertEquals(true, SpanHelper.getErrorOccurred(finishedSpan));