use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod in project pinpoint by naver.
the class DefaultClassEditorBuilderTest method test.
@Test
public void test() throws Exception {
InstrumentEngine instrumentEngine = mock(InstrumentEngine.class);
TraceContext traceContext = mock(TraceContext.class);
InstrumentClass aClass = mock(InstrumentClass.class);
InstrumentMethod aMethod = mock(InstrumentMethod.class);
MethodDescriptor aDescriptor = mock(MethodDescriptor.class);
ApplicationContext applicationContext = mock(ApplicationContext.class);
InstrumentContext context = mock(InstrumentContext.class);
ClassLoader classLoader = getClass().getClassLoader();
String className = "someClass";
String methodName = "someMethod";
byte[] classFileBuffer = new byte[0];
Class<?>[] parameterTypes = new Class<?>[] { String.class };
String[] parameterTypeNames = TypeUtils.toClassNames(parameterTypes);
when(applicationContext.getInstrumentEngine()).thenReturn(instrumentEngine);
when(applicationContext.getTraceContext()).thenReturn(traceContext);
when(instrumentEngine.getClass(context, classLoader, className, classFileBuffer)).thenReturn(aClass);
when(aClass.getDeclaredMethod(methodName, parameterTypeNames)).thenReturn(aMethod);
when(aMethod.getName()).thenReturn(methodName);
when(aMethod.getParameterTypes()).thenReturn(parameterTypeNames);
when(aMethod.getDescriptor()).thenReturn(aDescriptor);
when(aClass.addInterceptor(eq(methodName), va(eq(parameterTypeNames)))).thenReturn(0);
// DefaultClassFileTransformerBuilder builder = new DefaultClassFileTransformerBuilder(context, "TargetClass");
// builder.injectField("some.accessor.Type", "java.util.HashMap");
// builder.injectGetter("some.getter.Type", "someField");
//
// MethodTransformerBuilder ib = builder.editMethod(methodName, parameterTypeNames);
// ib.injectInterceptor("com.navercorp.pinpoint.profiler.plugin.TestInterceptor", "provided");
//
// ClassFileTransformer transformer = builder.build();
//
// transformer.transform(classLoader, className, null, null, classFileBuffer);
//
// verify(aMethod).addScopedInterceptor(eq("com.navercorp.pinpoint.profiler.plugin.TestInterceptor"), eq(va("provided")), (InterceptorScope)isNull(), (ExecutionPolicy)isNull());
// verify(aClass).addField("some.accessor.Type", "new java.util.HashMap();");
// verify(aClass).addGetter("some.getter.Type", "someField");
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod in project pinpoint by naver.
the class ASMClass method addInterceptor0.
private int addInterceptor0(TargetFilter annotation, String interceptorClassName, InterceptorScope scope, ExecutionPolicy executionPolicy, Object[] constructorArgs) throws InstrumentException {
final String filterTypeName = annotation.type();
Asserts.notNull(filterTypeName, "type of @TargetFilter");
final InterceptorArgumentProvider interceptorArgumentProvider = objectBinderFactory.newInterceptorArgumentProvider(this);
final AutoBindingObjectFactory filterFactory = objectBinderFactory.newAutoBindingObjectFactory(pluginContext, classLoader, interceptorArgumentProvider);
final ObjectFactory objectFactory = ObjectFactory.byConstructor(filterTypeName, (Object[]) annotation.constructorArguments());
final MethodFilter filter = (MethodFilter) filterFactory.createInstance(objectFactory);
boolean singleton = annotation.singleton();
int interceptorId = -1;
for (InstrumentMethod m : getDeclaredMethods(filter)) {
if (singleton && interceptorId != -1) {
m.addInterceptor(interceptorId);
} else {
// TODO casting fix
interceptorId = ((ASMMethod) m).addInterceptorInternal(interceptorClassName, constructorArgs, scope, executionPolicy);
}
}
if (interceptorId == -1) {
logger.warn("No methods are intercepted. target: " + this.classNode.getInternalName(), ", interceptor: " + interceptorClassName + ", methodFilter: " + filterTypeName);
}
return interceptorId;
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod in project pinpoint by naver.
the class SpringBeansPlugin method addAbstractAutowireCapableBeanFactoryTransformer.
private void addAbstractAutowireCapableBeanFactoryTransformer(final ProfilerPluginSetupContext context) {
final ProfilerConfig config = context.getConfig();
final boolean errorMark = config.readBoolean(SPRING_BEANS_MARK_ERROR, false);
transformTemplate.transform("org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory", 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);
final BeanMethodTransformer beanTransformer = new BeanMethodTransformer(errorMark);
final ObjectFactory beanFilterFactory = ObjectFactory.byStaticFactory("com.navercorp.pinpoint.plugin.spring.beans.interceptor.TargetBeanFilter", "of", config);
final InstrumentMethod createBeanInstance = target.getDeclaredMethod("createBeanInstance", "java.lang.String", "org.springframework.beans.factory.support.RootBeanDefinition", "java.lang.Object[]");
createBeanInstance.addInterceptor("com.navercorp.pinpoint.plugin.spring.beans.interceptor.CreateBeanInstanceInterceptor", va(beanTransformer, beanFilterFactory));
final InstrumentMethod postProcessor = target.getDeclaredMethod("applyBeanPostProcessorsBeforeInstantiation", "java.lang.Class", "java.lang.String");
postProcessor.addInterceptor("com.navercorp.pinpoint.plugin.spring.beans.interceptor.PostProcessorInterceptor", va(beanTransformer, beanFilterFactory));
return target.toBytecode();
}
});
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod in project pinpoint by naver.
the class SpringBeansPlugin method addClassPathDefinitionScannerTransformer.
private void addClassPathDefinitionScannerTransformer(final ProfilerPluginSetupContext context) {
final ProfilerConfig config = context.getConfig();
final boolean errorMark = config.readBoolean(SPRING_BEANS_MARK_ERROR, false);
transformTemplate.transform("org.springframework.context.annotation.ClassPathBeanDefinitionScanner", 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);
final BeanMethodTransformer beanTransformer = new BeanMethodTransformer(errorMark);
final ObjectFactory beanFilterFactory = ObjectFactory.byStaticFactory("com.navercorp.pinpoint.plugin.spring.beans.interceptor.TargetBeanFilter", "of", context.getConfig());
final InstrumentMethod method = target.getDeclaredMethod("doScan", "java.lang.String[]");
method.addInterceptor("com.navercorp.pinpoint.plugin.spring.beans.interceptor.ClassPathDefinitionScannerDoScanInterceptor", va(loader, beanTransformer, beanFilterFactory));
return target.toBytecode();
}
});
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod 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;
}
}
});
}
}
Aggregations