From 922a5001fa7aaecc9cfd5f297889f40fd5ca7f3b Mon Sep 17 00:00:00 2001 From: xiaqi1210 Date: Wed, 13 Sep 2023 08:17:20 +0800 Subject: [PATCH] Fix the issue of createSpan failure caused by invalid request URL in HttpClient 4.x/5.x plugin (#603) --- CHANGES.md | 1 + .../v4/HttpClientExecuteInterceptor.java | 14 +++++++++++--- .../v5/HttpClientDoExecuteInterceptor.java | 14 +++++++++++--- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 8cb79d44f..cb5ac457a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,7 @@ Release Notes. ------------------ * Fix hbase onConstruct NPE in the file configuration scenario +* Fix the issue of createSpan failure caused by invalid request URL in HttpClient 4.x/5.x plugin * Optimize ElasticSearch 6.x 7.x plugin compatibility #### Documentation diff --git a/apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/httpClient/v4/HttpClientExecuteInterceptor.java b/apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/httpClient/v4/HttpClientExecuteInterceptor.java index b10768510..b02de707a 100644 --- a/apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/httpClient/v4/HttpClientExecuteInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/httpClient/v4/HttpClientExecuteInterceptor.java @@ -42,6 +42,7 @@ import org.apache.skywalking.apm.plugin.httpclient.HttpClientPluginConfig; import org.apache.skywalking.apm.util.StringUtil; public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterceptor { + private static final String ERROR_URI = "/_blank"; @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, @@ -60,7 +61,9 @@ public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterc String requestURI = getRequestURI(uri); String operationName = requestURI; AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, remotePeer); - + if (ERROR_URI.equals(requestURI)) { + span.errorOccurred(); + } span.setComponent(ComponentsDefine.HTTPCLIENT); Tags.URL.set(span, buildSpanValue(httpHost, uri)); Tags.HTTP.METHOD.set(span, httpRequest.getRequestLine().getMethod()); @@ -112,9 +115,14 @@ public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterc activeSpan.log(t); } - private String getRequestURI(String uri) throws MalformedURLException { + private String getRequestURI(String uri) { if (isUrl(uri)) { - String requestPath = new URL(uri).getPath(); + String requestPath; + try { + requestPath = new URL(uri).getPath(); + } catch (MalformedURLException e) { + return ERROR_URI; + } return requestPath != null && requestPath.length() > 0 ? requestPath : "/"; } else { return uri; diff --git a/apm-sniffer/apm-sdk-plugin/httpclient-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/httpclient/v5/HttpClientDoExecuteInterceptor.java b/apm-sniffer/apm-sdk-plugin/httpclient-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/httpclient/v5/HttpClientDoExecuteInterceptor.java index 0d037afe2..e31b5cae1 100644 --- a/apm-sniffer/apm-sdk-plugin/httpclient-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/httpclient/v5/HttpClientDoExecuteInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/httpclient-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/httpclient/v5/HttpClientDoExecuteInterceptor.java @@ -39,6 +39,7 @@ import java.net.MalformedURLException; import java.net.URL; public class HttpClientDoExecuteInterceptor implements InstanceMethodsAroundInterceptor { + private static final String ERROR_URI = "/_blank"; private static final ILog LOGGER = LogManager.getLogger(HttpClientDoExecuteInterceptor.class); @@ -59,7 +60,9 @@ public class HttpClientDoExecuteInterceptor implements InstanceMethodsAroundInte String requestURI = getRequestURI(uri); String operationName = requestURI; AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, remotePeer); - + if (ERROR_URI.equals(requestURI)) { + span.errorOccurred(); + } span.setComponent(ComponentsDefine.HTTPCLIENT); Tags.URL.set(span, buildURL(httpHost, uri)); Tags.HTTP.METHOD.set(span, httpRequest.getMethod()); @@ -101,9 +104,14 @@ public class HttpClientDoExecuteInterceptor implements InstanceMethodsAroundInte activeSpan.log(t); } - private String getRequestURI(String uri) throws MalformedURLException { + private String getRequestURI(String uri) { if (isUrl(uri)) { - String requestPath = new URL(uri).getPath(); + String requestPath; + try { + requestPath = new URL(uri).getPath(); + } catch (MalformedURLException e) { + return ERROR_URI; + } return requestPath != null && requestPath.length() > 0 ? requestPath : "/"; } else { return uri;