Search in sources :

Example 1 with InstrumentMethod

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");
}
Also used : InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) MethodDescriptor(com.navercorp.pinpoint.bootstrap.context.MethodDescriptor) ApplicationContext(com.navercorp.pinpoint.profiler.context.module.ApplicationContext) InstrumentContext(com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext) TraceContext(com.navercorp.pinpoint.bootstrap.context.TraceContext) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) InstrumentEngine(com.navercorp.pinpoint.profiler.instrument.InstrumentEngine) Test(org.junit.Test)

Example 2 with InstrumentMethod

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;
}
Also used : AutoBindingObjectFactory(com.navercorp.pinpoint.profiler.objectfactory.AutoBindingObjectFactory) ObjectFactory(com.navercorp.pinpoint.bootstrap.plugin.ObjectFactory) AutoBindingObjectFactory(com.navercorp.pinpoint.profiler.objectfactory.AutoBindingObjectFactory) MethodFilter(com.navercorp.pinpoint.bootstrap.instrument.MethodFilter) InterceptorArgumentProvider(com.navercorp.pinpoint.profiler.objectfactory.InterceptorArgumentProvider) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod)

Example 3 with InstrumentMethod

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();
        }
    });
}
Also used : ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) ProfilerConfig(com.navercorp.pinpoint.bootstrap.config.ProfilerConfig) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) ObjectFactory(com.navercorp.pinpoint.bootstrap.plugin.ObjectFactory)

Example 4 with InstrumentMethod

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();
        }
    });
}
Also used : ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) ProfilerConfig(com.navercorp.pinpoint.bootstrap.config.ProfilerConfig) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) ObjectFactory(com.navercorp.pinpoint.bootstrap.plugin.ObjectFactory)

Example 5 with InstrumentMethod

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;
                }
            }
        });
    }
}
Also used : ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) HystrixCommandTransformer(com.navercorp.pinpoint.plugin.hystrix.transformer.HystrixCommandTransformer)

Aggregations

InstrumentMethod (com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod)33 InstrumentClass (com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass)29 InstrumentException (com.navercorp.pinpoint.bootstrap.instrument.InstrumentException)21 Instrumentor (com.navercorp.pinpoint.bootstrap.instrument.Instrumentor)19 TransformCallback (com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback)19 ProtectionDomain (java.security.ProtectionDomain)19 Test (org.junit.Test)9 ApiMetaDataService (com.navercorp.pinpoint.profiler.metadata.ApiMetaDataService)6 AroundInterceptor3 (com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor3)4 InstrumentContext (com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext)3 MethodFilter (com.navercorp.pinpoint.bootstrap.instrument.MethodFilter)3 ObjectFactory (com.navercorp.pinpoint.bootstrap.plugin.ObjectFactory)3 InstrumentEngine (com.navercorp.pinpoint.profiler.instrument.InstrumentEngine)3 ProfilerConfig (com.navercorp.pinpoint.bootstrap.config.ProfilerConfig)2 List (java.util.List)2 AsyncTraceIdAccessor (com.navercorp.pinpoint.bootstrap.async.AsyncTraceIdAccessor)1 MethodDescriptor (com.navercorp.pinpoint.bootstrap.context.MethodDescriptor)1 TraceContext (com.navercorp.pinpoint.bootstrap.context.TraceContext)1 AroundInterceptor0 (com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor0)1 Interceptor (com.navercorp.pinpoint.bootstrap.interceptor.Interceptor)1