Fix the issue of createSpan failure caused by invalid request URL in HttpClient 4.x/5.x plugin (#603)

This commit is contained in:
xiaqi1210 2023-09-13 08:17:20 +08:00 committed by GitHub
parent 44b8f669a9
commit 922a5001fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 6 deletions

View File

@ -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

View File

@ -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;

View File

@ -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;