Skip to content

Commit 922a500

Browse files
authored
Fix the issue of createSpan failure caused by invalid request URL in HttpClient 4.x/5.x plugin (#603)
1 parent 44b8f66 commit 922a500

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Release Notes.
66
------------------
77

88
* Fix hbase onConstruct NPE in the file configuration scenario
9+
* Fix the issue of createSpan failure caused by invalid request URL in HttpClient 4.x/5.x plugin
910
* Optimize ElasticSearch 6.x 7.x plugin compatibility
1011

1112
#### Documentation

apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/httpClient/v4/HttpClientExecuteInterceptor.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.apache.skywalking.apm.util.StringUtil;
4343

4444
public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterceptor {
45+
private static final String ERROR_URI = "/_blank";
4546

4647
@Override
4748
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
@@ -60,7 +61,9 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
6061
String requestURI = getRequestURI(uri);
6162
String operationName = requestURI;
6263
AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, remotePeer);
63-
64+
if (ERROR_URI.equals(requestURI)) {
65+
span.errorOccurred();
66+
}
6467
span.setComponent(ComponentsDefine.HTTPCLIENT);
6568
Tags.URL.set(span, buildSpanValue(httpHost, uri));
6669
Tags.HTTP.METHOD.set(span, httpRequest.getRequestLine().getMethod());
@@ -112,9 +115,14 @@ public void handleMethodException(EnhancedInstance objInst, Method method, Objec
112115
activeSpan.log(t);
113116
}
114117

115-
private String getRequestURI(String uri) throws MalformedURLException {
118+
private String getRequestURI(String uri) {
116119
if (isUrl(uri)) {
117-
String requestPath = new URL(uri).getPath();
120+
String requestPath;
121+
try {
122+
requestPath = new URL(uri).getPath();
123+
} catch (MalformedURLException e) {
124+
return ERROR_URI;
125+
}
118126
return requestPath != null && requestPath.length() > 0 ? requestPath : "/";
119127
} else {
120128
return uri;

apm-sniffer/apm-sdk-plugin/httpclient-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/httpclient/v5/HttpClientDoExecuteInterceptor.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.net.URL;
4040

4141
public class HttpClientDoExecuteInterceptor implements InstanceMethodsAroundInterceptor {
42+
private static final String ERROR_URI = "/_blank";
4243

4344
private static final ILog LOGGER = LogManager.getLogger(HttpClientDoExecuteInterceptor.class);
4445

@@ -59,7 +60,9 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
5960
String requestURI = getRequestURI(uri);
6061
String operationName = requestURI;
6162
AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, remotePeer);
62-
63+
if (ERROR_URI.equals(requestURI)) {
64+
span.errorOccurred();
65+
}
6366
span.setComponent(ComponentsDefine.HTTPCLIENT);
6467
Tags.URL.set(span, buildURL(httpHost, uri));
6568
Tags.HTTP.METHOD.set(span, httpRequest.getMethod());
@@ -101,9 +104,14 @@ public void handleMethodException(EnhancedInstance objInst, Method method, Objec
101104
activeSpan.log(t);
102105
}
103106

104-
private String getRequestURI(String uri) throws MalformedURLException {
107+
private String getRequestURI(String uri) {
105108
if (isUrl(uri)) {
106-
String requestPath = new URL(uri).getPath();
109+
String requestPath;
110+
try {
111+
requestPath = new URL(uri).getPath();
112+
} catch (MalformedURLException e) {
113+
return ERROR_URI;
114+
}
107115
return requestPath != null && requestPath.length() > 0 ? requestPath : "/";
108116
} else {
109117
return uri;

0 commit comments

Comments
 (0)