Skip to content

Commit

Permalink
[#1417] add support for vertx httpclient 3.4.0, 3.4.1, 3.4.2 versions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaehong-kim committed Jun 29, 2017
1 parent 203fd54 commit 48aca5c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 29 deletions.
Expand Up @@ -31,4 +31,5 @@ public class VertxConstants {
public static final ServiceType VERTX_HTTP_CLIENT_INTERNAL = ServiceTypeFactory.of(9131, "VERTX_HTTP_CLIENT_INTERNAL", "VERTX_HTTP_CLIENT");

public static final String HTTP_CLIENT_REQUEST_SCOPE = "HttpClientRequestScope";
public static final String HTTP_CLIENT_CREATE_REQUEST_SCOPE = "HttpClientCreateRequestScope";
}
Expand Up @@ -53,7 +53,7 @@ public void setup(ProfilerPluginSetupContext context) {
if (!config.isEnable() || (!config.isEnableHttpServer() && !config.isEnableHttpClient())) {
return;
}
// for vertx.io 3.x
// for vertx.io 3.3.x, 3.4.x
final VertxDetector vertxDetector = new VertxDetector(config.getBootstrapMains());
context.addApplicationTypeDetector(vertxDetector);

Expand All @@ -69,7 +69,7 @@ public void setup(ProfilerPluginSetupContext context) {
addContextImpl("io.vertx.core.impl.ContextImpl");
addContextImpl("io.vertx.core.impl.EventLoopContext");
addContextImpl("io.vertx.core.impl.MultiThreadedWorkerContext");
addContextImpl("io.vertx.core.impl.EventLoopContext");
addContextImpl("io.vertx.core.impl.WorkerContext");
}

if (config.isEnableHttpServer()) {
Expand Down Expand Up @@ -261,15 +261,25 @@ public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader,
}
}

// 3.3.x
final InstrumentMethod doRequestMethod = target.getDeclaredMethod("doRequest", "io.vertx.core.http.HttpMethod", "java.lang.String", "int", "java.lang.String", "io.vertx.core.MultiMap");
if (doRequestMethod != null) {
doRequestMethod.addInterceptor("com.navercorp.pinpoint.plugin.vertx.interceptor.HttpClientImplDoRequestInterceptor");
}

// connect
final InstrumentMethod getConnectionForRequestMethod = target.getDeclaredMethod("getConnectionForRequest", "int", "java.lang.String", "io.vertx.core.http.impl.Waiter");
if (getConnectionForRequestMethod != null) {
getConnectionForRequestMethod.addInterceptor("com.navercorp.pinpoint.plugin.vertx.interceptor.HttpClientImplGetConnectionForRequest");
// 3.4.1, 3.4.2
for (InstrumentMethod method : target.getDeclaredMethods(MethodFilters.name("createRequest"))) {
if (method != null) {
// scoped for 3.4.2
method.addScopedInterceptor("com.navercorp.pinpoint.plugin.vertx.interceptor.HttpClientImplDoRequestInterceptor", VertxConstants.HTTP_CLIENT_CREATE_REQUEST_SCOPE);
}
}

// connection for 3.3.x, 3.4.0, 3.4.1, 3.4.2
for (InstrumentMethod method : target.getDeclaredMethods(MethodFilters.name("getConnectionForRequest"))) {
if (method != null) {
method.addInterceptor("com.navercorp.pinpoint.plugin.vertx.interceptor.HttpClientImplGetConnectionForRequest");
}
}

return target.toBytecode();
Expand All @@ -286,9 +296,10 @@ public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader,
target.addField(AsyncTraceIdAccessor.class.getName());

// for HttpClientResponseImpl.
final InstrumentMethod doHandleResponseMethod = target.getDeclaredMethod("doHandleResponse", "io.vertx.core.http.impl.HttpClientResponseImpl");
if (doHandleResponseMethod != null) {
doHandleResponseMethod.addInterceptor("com.navercorp.pinpoint.plugin.vertx.interceptor.HttpClientRequestImplDoHandleResponseInterceptor");
for (InstrumentMethod method : target.getDeclaredMethods(MethodFilters.name("doHandleResponse"))) {
if (method != null) {
method.addInterceptor("com.navercorp.pinpoint.plugin.vertx.interceptor.HttpClientRequestImplDoHandleResponseInterceptor");
}
}

// for completionHandler, writeHead(), connect().
Expand Down
Expand Up @@ -19,6 +19,7 @@
import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.interceptor.SpanEventSimpleAroundInterceptorForPlugin;
import com.navercorp.pinpoint.common.plugin.util.HostAndPort;
import com.navercorp.pinpoint.common.trace.AnnotationKey;
import com.navercorp.pinpoint.plugin.vertx.VertxConstants;

Expand All @@ -36,47 +37,70 @@ public void doInBeforeTrace(SpanEventRecorder recorder, Object target, Object[]
return;
}

final int port = (Integer) args[0];
final String host = (String) args[1];
if (host != null) {
final HttpUrlData httpUrlData = getHostAndPort(args);
if (httpUrlData.host != null) {
// connection address(host:port)
if (port > 0) {
recorder.recordAttribute(AnnotationKey.HTTP_INTERNAL_DISPLAY, host + ":" + port);
} else {
recorder.recordAttribute(AnnotationKey.HTTP_INTERNAL_DISPLAY, host);
}
recorder.recordAttribute(AnnotationKey.HTTP_INTERNAL_DISPLAY, HostAndPort.toHostAndPortString(httpUrlData.host, httpUrlData.port));
}
}

private boolean validate(final Object[] args) {
if (args == null || args.length < 2) {
if (args == null || args.length < 3) {
if (isDebug) {
logger.debug("Invalid args object. args={}.", args);
}
return false;
}
return true;
}

if (!(args[0] instanceof Integer)) {
if (isDebug) {
logger.debug("Invalid args[0] object. {}.", args[0]);
private HttpUrlData getHostAndPort(final Object[] args) {
HttpUrlData httpUrlData = new HttpUrlData();
if (args.length == 3) {
// 3.3.x
// int port, String host, Waiter waiter
if (args[0] instanceof Integer) {
httpUrlData.port = (Integer) args[0];
}
return false;
}

if (!(args[1] instanceof String)) {
if (isDebug) {
logger.debug("Invalid args[1] object. {}.", args[1]);
if (args[1] instanceof String) {
httpUrlData.host = (String) args[1];
}
} else if(args.length == 4) {
// 3.4.0, 3.4.1
// boolean ssl, int port, String host, Waiter waiter
if (args[1] instanceof Integer) {
httpUrlData.port = (Integer) args[1];
}
return false;
}

return true;
if (args[2] instanceof String) {
httpUrlData.host = (String) args[2];
}
} else if (args.length == 5) {
// 3.4.2
// String peerHost, boolean ssl, int port, String host, Waiter waiter
if (args[2] instanceof Integer) {
httpUrlData.port = (Integer) args[2];
}

if (args[3] instanceof String) {
httpUrlData.host = (String) args[3];
}
}
return httpUrlData;
}


@Override
public void doInAfterTrace(SpanEventRecorder recorder, Object target, Object[] args, Object result, Throwable throwable) {
recorder.recordApi(methodDescriptor);
recorder.recordServiceType(VertxConstants.VERTX_HTTP_CLIENT_INTERNAL);
recorder.recordException(throwable);
}

private class HttpUrlData {
private String host;
private int port = -1;
}

}

0 comments on commit 48aca5c

Please sign in to comment.