use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.
the class JavassistClassTest method testAddAfterInterceptor.
@Ignore
@Test
public void testAddAfterInterceptor() throws Exception {
final TestClassLoader loader = getTestClassLoader();
final String testClassObject = "com.navercorp.pinpoint.test.javasssit.mock.TestObject2";
loader.addTransformer(testClassObject, new TransformCallback() {
@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
try {
logger.info("modify cl:{}", classLoader);
InstrumentClass aClass = instrumentor.getInstrumentClass(classLoader, testClassObject, classfileBuffer);
String methodName = "callA";
aClass.getDeclaredMethod(methodName).addInterceptor("com.navercorp.pinpoint.profiler.interceptor.TestAfterInterceptor");
String methodName2 = "callB";
aClass.getDeclaredMethod(methodName2).addInterceptor("com.navercorp.pinpoint.profiler.interceptor.TestAfterInterceptor");
return aClass.toBytecode();
} catch (InstrumentException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
});
Class<?> testObjectClazz = loader.loadClass(testClassObject);
final String methodName = "callA";
logger.info("class:{}", testObjectClazz.toString());
final Object testObject = testObjectClazz.newInstance();
Method callA = testObjectClazz.getMethod(methodName);
Object result = callA.invoke(testObject);
Interceptor interceptor = getInterceptor(loader, 0);
assertEqualsIntField(interceptor, "call", 1);
assertEqualsObjectField(interceptor, "className", testClassObject);
assertEqualsObjectField(interceptor, "methodName", methodName);
assertEqualsObjectField(interceptor, "args", null);
assertEqualsObjectField(interceptor, "target", testObject);
assertEqualsObjectField(interceptor, "result", result);
final String methodName2 = "callB";
Method callBMethod = testObject.getClass().getMethod(methodName2);
callBMethod.invoke(testObject);
Interceptor interceptor2 = getInterceptor(loader, 1);
assertEqualsIntField(interceptor2, "call", 1);
assertEqualsObjectField(interceptor2, "className", testClassObject);
assertEqualsObjectField(interceptor2, "methodName", methodName2);
assertEqualsObjectField(interceptor2, "args", null);
assertEqualsObjectField(interceptor2, "target", testObject);
assertEqualsObjectField(interceptor2, "result", null);
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.
the class HystrixPlugin method addTransformers.
private void addTransformers(int numHystrixCommandAnonymousLocalClass) {
transformTemplate.transform("com.netflix.hystrix.HystrixCommand", new HystrixCommandTransformer());
/*
* After com.netflix.hystrix:hystrix-core:1.4.0 the api changed.
* The run() and getFallback() methods of the subclass will be called by HystrixCommand's
* anonymous inner classes.
*
* Safest way (although ugly) is to predefine the anonymous inner class names and check each of them to inject
* their appropriate interceptors as they are loaded.
*
* The anonymous inner classes that should be modified may differ according to hystrix-core version. This is
* simply something that we'll have to keep updating if any changes occur.
* (Any breaking changes can be detected through integration tests.)
*/
// number of anonymous inner classes to look for.
// We start with 3 only because the most recent version requires this many. May be better to make this
// configurable but for now let's just hard-code it.
final int numAnonymousInnerClassesToTest = numHystrixCommandAnonymousLocalClass;
for (int i = 0; i < numAnonymousInnerClassesToTest; ++i) {
String anonymousInnerClassName = "com.netflix.hystrix.HystrixCommand$" + (i + 1);
logger.debug("Registering transformer for {}", anonymousInnerClassName);
transformTemplate.transform(anonymousInnerClassName, new TransformCallback() {
@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
if (target.hasEnclosingMethod("getExecutionObservable")) {
// 1.4.0 ~ 1.5.2 - void call(Subscriber<? super R> s)
InstrumentMethod method = target.getDeclaredMethod("call", "rx.Subscriber");
// 1.5.3+ - Observable<R> call()
if (method == null) {
method = target.getDeclaredMethod("call");
// 1.5.4+ - May be another anonymous class inside getExecutionObservable()
if (!method.getReturnType().equals("rx.Observable")) {
return null;
}
}
if (method != null) {
// Add getter for the enclosing instance
target.addGetter("com.navercorp.pinpoint.plugin.hystrix.field.EnclosingInstanceFieldGetter", "this$0");
method.addInterceptor("com.navercorp.pinpoint.plugin.hystrix.interceptor.ExecutionObservableCallInterceptor");
return target.toBytecode();
} else {
logger.warn("Unknown version of HystrixCommand.getExecutionObservable() detected");
return null;
}
} else if (target.hasEnclosingMethod("getFallbackObservable")) {
// 1.4.0 ~ 1.5.2 - void call(Subscriber<? super R> s)
InstrumentMethod method = target.getDeclaredMethod("call", "rx.Subscriber");
// 1.5.3+ - Observable<R> call()
if (method == null) {
method = target.getDeclaredMethod("call");
}
if (method != null) {
// Add getter for the enclosing instance
target.addGetter("com.navercorp.pinpoint.plugin.hystrix.field.EnclosingInstanceFieldGetter", "this$0");
method.addInterceptor("com.navercorp.pinpoint.plugin.hystrix.interceptor.FallbackObservableCallInterceptor");
return target.toBytecode();
} else {
logger.warn("Unknown version of HystrixCommand.getFallbackObservable detected");
return null;
}
} else {
return null;
}
}
});
}
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.
the class DubboPlugin method addTransformers.
private void addTransformers() {
transformTemplate.transform("com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker", new TransformCallback() {
@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
target.getDeclaredMethod("invoke", "com.alibaba.dubbo.rpc.Invocation").addInterceptor("com.navercorp.pinpoint.plugin.dubbo.interceptor.DubboConsumerInterceptor");
return target.toBytecode();
}
});
transformTemplate.transform("com.alibaba.dubbo.rpc.proxy.AbstractProxyInvoker", new TransformCallback() {
@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
target.getDeclaredMethod("invoke", "com.alibaba.dubbo.rpc.Invocation").addInterceptor("com.navercorp.pinpoint.plugin.dubbo.interceptor.DubboProviderInterceptor");
return target.toBytecode();
}
});
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.
the class ActiveMQClientPlugin method addProducerEditor.
private void addProducerEditor(final Filter<String> excludeDestinationFilter) {
final MethodFilter methodFilter = MethodFilters.chain(MethodFilters.name("send"), MethodFilters.argAt(0, "javax.jms.Destination"), MethodFilters.argAt(1, "javax.jms.Message"));
transformTemplate.transform(ActiveMQClientConstants.ACTIVEMQ_MESSAGE_PRODUCER_FQCN, new TransformCallback() {
@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
target.addGetter(ActiveMQClientConstants.FIELD_GETTER_ACTIVEMQ_SESSION, ActiveMQClientConstants.FIELD_ACTIVEMQ_MESSAGE_PRODUCER_SESSION);
for (InstrumentMethod method : target.getDeclaredMethods(methodFilter)) {
try {
method.addInterceptor(ActiveMQClientConstants.ACTIVEMQ_MESSAGE_PRODUCER_SEND_INTERCEPTOR_FQCN, va(excludeDestinationFilter));
} catch (Exception e) {
if (logger.isWarnEnabled()) {
logger.warn("Unsupported method " + method, e);
}
}
}
return target.toBytecode();
}
});
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.
the class GsonPlugin method setup.
@Override
public void setup(ProfilerPluginSetupContext context) {
GsonConfig config = new GsonConfig(context.getConfig());
logger.debug("[Gson] Initialized config={}", config);
if (config.isProfile()) {
transformTemplate.transform("com.google.gson.Gson", new TransformCallback() {
@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
for (InstrumentMethod m : target.getDeclaredMethods(MethodFilters.name("fromJson"))) {
m.addScopedInterceptor("com.navercorp.pinpoint.plugin.gson.interceptor.FromJsonInterceptor", GSON_SCOPE);
}
for (InstrumentMethod m : target.getDeclaredMethods(MethodFilters.name("toJson"))) {
m.addScopedInterceptor("com.navercorp.pinpoint.plugin.gson.interceptor.ToJsonInterceptor", GSON_SCOPE);
}
return target.toBytecode();
}
});
}
}
Aggregations