Add optimization to apache async client instrumentation
@@ -7,6 +7,7 @@ import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.RequestLine;
import org.apache.http.StatusLine;
+import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpCoreContext;
@@ -26,41 +27,56 @@ public class ApacheHttpAsyncClientDecorator extends HttpClientDecorator<HttpRequ
@Override
protected String method(final HttpRequest request) {
+ if (request instanceof HttpUriRequest) {
+ return ((HttpUriRequest) request).getMethod();
+ } else {
final RequestLine requestLine = request.getRequestLine();
return requestLine == null ? null : requestLine.getMethod();
}
+ }
@Override
protected URI url(final HttpRequest request) throws URISyntaxException {
+ /*
+ * Note: this is essentially an optimization: HttpUriRequest allows quicker access to required information.
+ * The downside is that we need to load HttpUriRequest which essentially means we depend on httpasyncclient
+ * library depending on httpclient library. Currently this seems to be the case.
+ */
+ if (request instanceof HttpUriRequest) {
+ return ((HttpUriRequest) request).getURI();
+ } else {
final RequestLine requestLine = request.getRequestLine();
return requestLine == null ? null : new URI(requestLine.getUri());
}
+ }
@Override
protected String hostname(final HttpRequest request) {
- final RequestLine requestLine = request.getRequestLine();
- if (requestLine != null) {
try {
- return new URI(requestLine.getUri()).getHost();
- } catch (final URISyntaxException e) {
+ final URI uri = url(request);
+ if (uri != null) {
+ return uri.getHost();
+ } else {
return null;
}
- }
+ } catch (final URISyntaxException e) {
return null;
}
+ }
@Override
protected Integer port(final HttpRequest request) {
- final RequestLine requestLine = request.getRequestLine();
- if (requestLine != null) {
try {
- return new URI(requestLine.getUri()).getPort();
- } catch (final URISyntaxException e) {
+ final URI uri = url(request);
+ if (uri != null) {
+ return uri.getPort();
+ } else {
return null;
}
- }
+ } catch (final URISyntaxException e) {
return null;
}
+ }
@Override
protected Integer status(final HttpContext context) {
