Skip to content

Commit

Permalink
Fixed an error that took a non-protocol uri string as a java.net.URL … (
Browse files Browse the repository at this point in the history
apache#2231)

* Fixed an error that took a non-protocol uri string as a java.net.URL construct parameter.

* Fixed an error that took a non-protocol uri string as a java.net.URL construct parameter.

* Fixed checkstyle error.
  • Loading branch information
donbing007 authored and wu-sheng committed Feb 1, 2019
1 parent 7c72618 commit 4657ce7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterceptor {

@Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
if (allArguments[0] == null || allArguments[1] == null) {
// illegal args, can't trace. ignore.
return;
Expand All @@ -48,16 +48,15 @@ public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterc
HttpRequest httpRequest = (HttpRequest)allArguments[1];
final ContextCarrier contextCarrier = new ContextCarrier();

String remotePeer = httpHost.getHostName() + ":" + (httpHost.getPort() > 0 ? httpHost.getPort() :
"https".equals(httpHost.getSchemeName().toLowerCase()) ? 443 : 80);
String remotePeer = httpHost.getHostName() + ":" + port(httpHost);

String uri = httpRequest.getRequestLine().getUri();
String requestURI = getRequestURI(uri);
String operationName = uri.startsWith("http") ? requestURI : uri;
String operationName = requestURI;
AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, remotePeer);

span.setComponent(ComponentsDefine.HTTPCLIENT);
Tags.URL.set(span, uri);
Tags.URL.set(span, buildSpanValue(httpHost,uri));
Tags.HTTP.METHOD.set(span, httpRequest.getRequestLine().getMethod());
SpanLayer.asHttp(span);

Expand All @@ -69,7 +68,7 @@ public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterc
}

@Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Object ret) throws Throwable {
Class<?>[] argumentsTypes, Object ret) throws Throwable {
if (allArguments[0] == null || allArguments[1] == null) {
return ret;
}
Expand All @@ -92,14 +91,43 @@ public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterc
}

@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
Class<?>[] argumentsTypes, Throwable t) {
AbstractSpan activeSpan = ContextManager.activeSpan();
activeSpan.errorOccurred();
activeSpan.log(t);
}

private String getRequestURI(String uri) throws MalformedURLException {
String requestPath = new URL(uri).getPath();
return requestPath != null && requestPath.length() > 0 ? requestPath : "/";
if (isUrl(uri)) {
String requestPath = new URL(uri).getPath();
return requestPath != null && requestPath.length() > 0 ? requestPath : "/";
} else {
return uri;
}
}

private boolean isUrl(String uri) {
String lowerUrl = uri.toLowerCase();
return lowerUrl.startsWith("http") || lowerUrl.startsWith("https");
}

private String buildSpanValue(HttpHost httpHost, String uri) {
if (isUrl(uri)) {
return uri;
} else {
StringBuilder buff = new StringBuilder();
buff.append(httpHost.getSchemeName().toLowerCase());
buff.append("://");
buff.append(httpHost.getHostName());
buff.append(":");
buff.append(port(httpHost));
buff.append(uri);
return buff.toString();
}
}

private int port(HttpHost httpHost) {
int port = httpHost.getPort();
return port > 0 ? port : "https".equals(httpHost.getSchemeName().toLowerCase()) ? 443 : 80;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,35 @@ public void testHttpClientWithException() throws Throwable {

}

@Test
public void testUriNotProtocol() throws Throwable {
when(request.getRequestLine()).thenReturn(new RequestLine() {
@Override
public String getMethod() {
return "GET";
}

@Override
public ProtocolVersion getProtocolVersion() {
return new ProtocolVersion("http", 1, 1);
}

@Override
public String getUri() {
return "/test-web/test";
}
});
httpClientExecuteInterceptor.beforeMethod(enhancedInstance, null, allArguments, argumentsType, null);
httpClientExecuteInterceptor.afterMethod(enhancedInstance, null, allArguments, argumentsType, httpResponse);

Assert.assertThat(segmentStorage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);

List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
assertHttpSpan(spans.get(0));
verify(request, times(1)).setHeader(anyString(), anyString());
}

private void assertHttpSpanErrorLog(List<LogDataEntity> logs) {
assertThat(logs.size(), is(1));
LogDataEntity logData = logs.get(0);
Expand Down

0 comments on commit 4657ce7

Please sign in to comment.