Skip to content

Commit

Permalink
Extract ning async-http-client profiler as a plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
lioolli committed Sep 10, 2015
1 parent aef9c76 commit 7b8aae4
Show file tree
Hide file tree
Showing 19 changed files with 379 additions and 233 deletions.
25 changes: 10 additions & 15 deletions agent/pom.xml
Expand Up @@ -116,37 +116,32 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<scope>provided</scope>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<scope>provided</scope>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore-nio</artifactId>
<scope>provided</scope>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>commons-codec</artifactId>
<groupId>commons-codec</groupId>
</exclusion>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<scope>provided</scope>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ning</groupId>
<artifactId>async-http-client</artifactId>
<scope>test</scope>
</dependency>

<dependency>
Expand Down
@@ -0,0 +1,45 @@
package com.navercorp.pinpoint.plugin.ning.asynchttpclient;

import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.*;

import java.util.concurrent.Future;

import org.junit.Test;
import org.junit.runner.RunWith;

import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder;
import com.navercorp.pinpoint.test.plugin.Dependency;
import com.navercorp.pinpoint.test.plugin.JvmVersion;
import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite;
import com.ning.http.client.AsyncHandler;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Request;
import com.ning.http.client.Response;

/**
* @author netspider
*/
@RunWith(PinpointPluginTestSuite.class)
@Dependency({ "com.ning:async-http-client:[1.7.24],[1.8.16,1.8.999)" })
@JvmVersion(7)
public class NingAsyncHttpClientIT {

@Test
public void test() throws Exception {
AsyncHttpClient client = new AsyncHttpClient();

try {
Future<Response> f = client.preparePost("http://www.naver.com/").addParameter("param1", "value1").execute();
Response response = f.get();
} finally {
client.close();
}

PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.printCache();

verifier.verifyTrace(event("ASYNC_HTTP_CLIENT", AsyncHttpClient.class.getMethod("executeRequest", Request.class, AsyncHandler.class), null, null, "www.naver.com", annotation("http.url", "http://www.naver.com"), annotation("http.param", "param1=value1")));
verifier.verifyTraceCount(0);
}
}
Expand Up @@ -149,23 +149,6 @@ public static ProfilerConfig load(String pinpointConfigFileName) throws IOExcept
*/
private boolean apacheNIOHttpClient4Profile = true;

/**
* ning async http client
*/
private boolean ningAsyncHttpClientProfile = true;
private boolean ningAsyncHttpClientProfileCookie = false;
private DumpType ningAsyncHttpClientProfileCookieDumpType = DumpType.EXCEPTION;
private int ningAsyncHttpClientProfileCookieDumpSize = 1024;
private int ningAsyncHttpClientProfileCookieSamplingRate = 1;
private boolean ningAsyncHttpClientProfileEntity = false;
private DumpType ningAsyncHttpClientProfileEntityDumpType = DumpType.EXCEPTION;
private int ningAsyncHttpClientProfileEntityDumpSize = 1024;
private int ningAsyncHttpClientProfileEntitySamplingRate = 1;
private boolean ningAsyncHttpClientProfileParam = false;
private DumpType ningAsyncHttpClientProfileParamDumpType = DumpType.EXCEPTION;
private int ningAsyncHttpClientProfileParamDumpSize = 1024;
private int ningAsyncHttpClientProfileParamSamplingRate = 1;

// Sampling
private boolean samplingEnable = true;
private int samplingRate = 1;
Expand Down Expand Up @@ -385,60 +368,6 @@ public boolean getApacheNIOHttpClient4Profile() {
return apacheNIOHttpClient4Profile;
}

//-----------------------------------------
// com/ning/http/client/AsyncHttpClient
public boolean isNingAsyncHttpClientProfile() {
return ningAsyncHttpClientProfile;
}

public boolean isNingAsyncHttpClientProfileCookie() {
return ningAsyncHttpClientProfileCookie;
}

public DumpType getNingAsyncHttpClientProfileCookieDumpType() {
return ningAsyncHttpClientProfileCookieDumpType;
}

public int getNingAsyncHttpClientProfileCookieDumpSize() {
return ningAsyncHttpClientProfileCookieDumpSize;
}

public int getNingAsyncHttpClientProfileCookieSamplingRate() {
return ningAsyncHttpClientProfileCookieSamplingRate;
}

public boolean isNingAsyncHttpClientProfileEntity() {
return ningAsyncHttpClientProfileEntity;
}

public DumpType getNingAsyncHttpClientProfileEntityDumpType() {
return ningAsyncHttpClientProfileEntityDumpType;
}

public int getNingAsyncHttpClientProfileEntityDumpSize() {
return ningAsyncHttpClientProfileEntityDumpSize;
}

public int getNingAsyncHttpClientProfileEntitySamplingRate() {
return ningAsyncHttpClientProfileEntitySamplingRate;
}

public boolean isNingAsyncHttpClientProfileParam() {
return ningAsyncHttpClientProfileParam;
}

public DumpType getNingAsyncHttpClientProfileParamDumpType() {
return ningAsyncHttpClientProfileParamDumpType;
}

public int getNingAsyncHttpClientProfileParamDumpSize() {
return ningAsyncHttpClientProfileParamDumpSize;
}

public int getNingAsyncHttpClientProfileParamSamplingRate() {
return ningAsyncHttpClientProfileParamSamplingRate;
}

public boolean isIBatisEnabled() {
return ibatis;
}
Expand Down Expand Up @@ -577,25 +506,6 @@ void readPropertyValues() {
*/
this.apacheNIOHttpClient4Profile = readBoolean("profiler.apache.nio.httpclient4", true);

/**
* ning.async http client
*/
this.ningAsyncHttpClientProfile = readBoolean("profiler.ning.asynchttpclient", true);
this.ningAsyncHttpClientProfileCookie = readBoolean("profiler.ning.asynchttpclient.cookie", false);
this.ningAsyncHttpClientProfileCookieDumpType = readDumpType("profiler.ning.asynchttpclient.cookie.dumptype", DumpType.EXCEPTION);
this.ningAsyncHttpClientProfileCookieDumpSize = readInt("profiler.ning.asynchttpclient.cookie.dumpsize", 1024);
this.ningAsyncHttpClientProfileCookieSamplingRate = readInt("profiler.ning.asynchttpclient.cookie.sampling.rate", 1);

this.ningAsyncHttpClientProfileEntity = readBoolean("profiler.ning.asynchttpclient.entity", false);
this.ningAsyncHttpClientProfileEntityDumpType = readDumpType("profiler.ning.asynchttpclient.entity.dumptype", DumpType.EXCEPTION);
this.ningAsyncHttpClientProfileEntityDumpSize = readInt("profiler.ning.asynchttpclient.entity.dumpsize", 1024);
this.ningAsyncHttpClientProfileEntitySamplingRate = readInt("profiler.asynchttpclient.entity.sampling.rate", 1);

this.ningAsyncHttpClientProfileParam = readBoolean("profiler.ning.asynchttpclient.param", false);
this.ningAsyncHttpClientProfileParamDumpType = readDumpType("profiler.ning.asynchttpclient.param.dumptype", DumpType.EXCEPTION);
this.ningAsyncHttpClientProfileParamDumpSize = readInt("profiler.ning.asynchttpclient.param.dumpsize", 1024);
this.ningAsyncHttpClientProfileParamSamplingRate = readInt("profiler.asynchttpclient.param.sampling.rate", 1);

/**
* log4j
*/
Expand Down Expand Up @@ -814,32 +724,6 @@ public String toString() {
builder.append(apacheHttpClient4ProfileIo);
builder.append(", apacheNIOHttpClient4Profile=");
builder.append(apacheNIOHttpClient4Profile);
builder.append(", ningAsyncHttpClientProfile=");
builder.append(ningAsyncHttpClientProfile);
builder.append(", ningAsyncHttpClientProfileCookie=");
builder.append(ningAsyncHttpClientProfileCookie);
builder.append(", ningAsyncHttpClientProfileCookieDumpType=");
builder.append(ningAsyncHttpClientProfileCookieDumpType);
builder.append(", ningAsyncHttpClientProfileCookieDumpSize=");
builder.append(ningAsyncHttpClientProfileCookieDumpSize);
builder.append(", ningAsyncHttpClientProfileCookieSamplingRate=");
builder.append(ningAsyncHttpClientProfileCookieSamplingRate);
builder.append(", ningAsyncHttpClientProfileEntity=");
builder.append(ningAsyncHttpClientProfileEntity);
builder.append(", ningAsyncHttpClientProfileEntityDumpType=");
builder.append(ningAsyncHttpClientProfileEntityDumpType);
builder.append(", ningAsyncHttpClientProfileEntityDumpSize=");
builder.append(ningAsyncHttpClientProfileEntityDumpSize);
builder.append(", ningAsyncHttpClientProfileEntitySamplingRate=");
builder.append(ningAsyncHttpClientProfileEntitySamplingRate);
builder.append(", ningAsyncHttpClientProfileParam=");
builder.append(ningAsyncHttpClientProfileParam);
builder.append(", ningAsyncHttpClientProfileParamDumpType=");
builder.append(ningAsyncHttpClientProfileParamDumpType);
builder.append(", ningAsyncHttpClientProfileParamDumpSize=");
builder.append(ningAsyncHttpClientProfileParamDumpSize);
builder.append(", ningAsyncHttpClientProfileParamSamplingRate=");
builder.append(ningAsyncHttpClientProfileParamSamplingRate);
builder.append(", samplingEnable=");
builder.append(samplingEnable);
builder.append(", samplingRate=");
Expand Down
Expand Up @@ -302,8 +302,8 @@ public boolean equals(Object obj) {
// 9053 HTTP_CLIENT_4_INTERNAL
// 9054 GOOGLE_HTTP_CLIENT_INTERNAL
// public static final ServiceType JDK_HTTPURLCONNECTOR = of(9055, "JDK_HTTPURLCONNECTOR", "JDK_HTTPCONNECTOR", NORMAL_SCHEMA);
public static final ServiceType ASYNC_HTTP_CLIENT = of(9056, "ASYNC_HTTP_CLIENT", NORMAL_SCHEMA, RECORD_STATISTICS);
public static final ServiceType ASYNC_HTTP_CLIENT_INTERNAL = of(9057, "ASYNC_HTTP_CLIENT_INTERNAL", "ASYNC_HTTP_CLIENT", NORMAL_SCHEMA);
// public static final ServiceType ASYNC_HTTP_CLIENT = of(9056, "ASYNC_HTTP_CLIENT", NORMAL_SCHEMA, RECORD_STATISTICS);
// public static final ServiceType ASYNC_HTTP_CLIENT_INTERNAL = of(9057, "ASYNC_HTTP_CLIENT_INTERNAL", "ASYNC_HTTP_CLIENT", NORMAL_SCHEMA);
// public static final ServiceType NPC_CLIENT = of(9060, "NPC_CLIENT", NORMAL_SCHEMA, RECORD_STATISTICS);
public static final ServiceType NIMM_CLIENT = of(9070, "NIMM_CLIENT", NORMAL_SCHEMA, RECORD_STATISTICS);

Expand Down
5 changes: 5 additions & 0 deletions plugins/ning-asynchttpclient/.gitignore
@@ -0,0 +1,5 @@
/target/
/.settings/
/.classpath
/.project
/*.iml
5 changes: 5 additions & 0 deletions plugins/ning-asynchttpclient/clover.license
@@ -0,0 +1,5 @@
RMRqrdbgbKFhbaVnDxHUdDQvrOQXxIBklnvcmahheubVC
mh2KM35CLkwUHS4DH7QVhxy52J5hnWbyEm6Cyd3KkF<mV
RmmnSVOqOMnOnMMrmMqwXomoroNrqPNRrPSsWwtUxXuUU
sRONqpnmqmUUnqonmstsmmmmmUUnqonmstsmmmmmUUGfk
mlfkqUUnmmmm
27 changes: 27 additions & 0 deletions plugins/ning-asynchttpclient/pom.xml
@@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pom</artifactId>
<relativePath>../..</relativePath>
<version>1.5.0-SNAPSHOT</version>
</parent>

<artifactId>pinpoint-ning-asynchttpclient-plugin</artifactId>
<name>pinpoint-ning-asynchttpclient-plugin</name>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-bootstrap-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.ning</groupId>
<artifactId>async-http-client</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,36 @@
/*
* Copyright 2014 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.plugin.ning.asynchttpclient;

import com.navercorp.pinpoint.common.trace.AnnotationKey;
import com.navercorp.pinpoint.common.trace.AnnotationKeyMatchers;
import com.navercorp.pinpoint.common.trace.TraceMetadataProvider;
import com.navercorp.pinpoint.common.trace.TraceMetadataSetupContext;

/**
*
* @author jaehong.kim
*
*/
public class NingAsyncHttpClientMetadataProvider implements TraceMetadataProvider {

@Override
public void setup(TraceMetadataSetupContext context) {
context.addServiceType(NingAsyncHttpClientPlugin.ASYNC_HTTP_CLIENT, AnnotationKeyMatchers.exact(AnnotationKey.HTTP_URL));
context.addServiceType(NingAsyncHttpClientPlugin.ASYNC_HTTP_CLIENT_INTERNAL, AnnotationKeyMatchers.exact(AnnotationKey.HTTP_INTERNAL_DISPLAY));
}

}
@@ -0,0 +1,55 @@
/*
* Copyright 2014 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.plugin.ning.asynchttpclient;

import static com.navercorp.pinpoint.common.trace.HistogramSchema.*;
import static com.navercorp.pinpoint.common.trace.ServiceTypeProperty.*;

import java.security.ProtectionDomain;

import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentException;
import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPlugin;
import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPluginInstrumentContext;
import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPluginSetupContext;
import com.navercorp.pinpoint.bootstrap.plugin.transformer.PinpointClassFileTransformer;
import com.navercorp.pinpoint.common.trace.ServiceType;

/**
* @author netspider
* @author emeroad
* @author minwoo.jung
* @author jaehong.kim
*
*/
public class NingAsyncHttpClientPlugin implements ProfilerPlugin {
public static final ServiceType ASYNC_HTTP_CLIENT = ServiceType.of(9056, "ASYNC_HTTP_CLIENT", NORMAL_SCHEMA, RECORD_STATISTICS);
public static final ServiceType ASYNC_HTTP_CLIENT_INTERNAL = ServiceType.of(9057, "ASYNC_HTTP_CLIENT_INTERNAL", "ASYNC_HTTP_CLIENT", NORMAL_SCHEMA);

@Override
public void setup(ProfilerPluginSetupContext context) {
context.addClassFileTransformer("com.ning.http.client.AsyncHttpClient", new PinpointClassFileTransformer() {

@Override
public byte[] transform(ProfilerPluginInstrumentContext instrumentContext, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
InstrumentClass target = instrumentContext.getInstrumentClass(loader, className, classfileBuffer);
target.addInterceptor("com.navercorp.pinpoint.plugin.ning.asynchttpclient.interceptor.ExecuteRequestInterceptor");

return target.toBytecode();
}
});
}
}

0 comments on commit 7b8aae4

Please sign in to comment.