Search in sources :

Example 11 with InstrumentException

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();
            }
        });
    }
}
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)

Example 12 with InstrumentException

use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.

the class DedicatedMethodTransformer method edit.

@Override
public void edit(ClassLoader classLoader, InstrumentClass target) throws Throwable {
    InstrumentMethod targetMethod = target.getDeclaredMethod(targetMethodName, targetMethodParameterTypes);
    if (targetMethod == null) {
        if (ignoreIfNotExist) {
            return;
        } else {
            Exception e = new NoSuchMethodException("No such method: " + targetMethodName + "(" + Arrays.deepToString(targetMethodParameterTypes) + ")");
            if (exceptionHandler != null) {
                exceptionHandler.handle(target.getName(), targetMethodName, targetMethodParameterTypes, e);
                logger.info("Cannot find target method" + targetMethodName + "(" + Arrays.deepToString(targetMethodParameterTypes) + ") but MethodTransformerExceptionHandler handled it.");
                return;
            } else {
                throw new InstrumentException("Fail to edit method", e);
            }
        }
    }
    for (MethodRecipe recipe : recipes) {
        try {
            recipe.edit(classLoader, target, targetMethod);
        } catch (Throwable t) {
            if (exceptionHandler != null) {
                exceptionHandler.handle(target.getName(), targetMethodName, targetMethodParameterTypes, t);
                logger.info("Exception thrown while editing" + targetMethod.getDescriptor().getApiDescriptor() + " but MethodTransformerExceptionHandler handled it.", t);
            } else {
                throw new InstrumentException("Fail to edit method " + targetMethod.getDescriptor().getApiDescriptor(), t);
            }
        }
    }
}
Also used : InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException)

Example 13 with InstrumentException

use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.

the class ASMMethodNodeTest method addInterceptor.

@Test
public void addInterceptor() throws Exception {
    final int interceptorId = interceptorRegistryBinder.getInterceptorRegistryAdaptor().addInterceptor(new ArgsArrayInterceptor());
    final String targetClassName = "com.navercorp.pinpoint.profiler.instrument.mock.NormalClass";
    final ASMClass declaringClass = mock(ASMClass.class);
    when(declaringClass.getName()).thenReturn(targetClassName);
    final ObjectBinderFactory objectBinderFactory = mock(ObjectBinderFactory.class);
    ASMClassNodeLoader.TestClassLoader classLoader = ASMClassNodeLoader.getClassLoader();
    classLoader.setTrace(false);
    classLoader.setVerify(false);
    classLoader.setTargetClassName(targetClassName);
    classLoader.setCallbackHandler(new ASMClassNodeLoader.CallbackHandler() {

        @Override
        public void handle(ClassNode classNode) {
            List<MethodNode> methodNodes = classNode.methods;
            for (MethodNode methodNode : methodNodes) {
                ASMMethod method = new ASMMethod(objectBinderFactory, pluginContext, apiMetaDataService, interceptorRegistryBinder, declaringClass, methodNode);
                try {
                    method.addInterceptor(interceptorId);
                } catch (InstrumentException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    Class<?> clazz = classLoader.loadClass(targetClassName);
    Method method = clazz.getDeclaredMethod("sum", int.class);
    assertEquals(55, method.invoke(clazz.newInstance(), 10));
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) ArgsArrayInterceptor(com.navercorp.pinpoint.profiler.instrument.mock.ArgsArrayInterceptor) ObjectBinderFactory(com.navercorp.pinpoint.profiler.objectfactory.ObjectBinderFactory) Method(java.lang.reflect.Method) MethodNode(org.objectweb.asm.tree.MethodNode) List(java.util.List) Test(org.junit.Test)

Example 14 with InstrumentException

use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.

the class MyBatisPlugin method addInterceptorsForSqlSession.

// SqlSession implementations
private void addInterceptorsForSqlSession() {
    final MethodFilter methodFilter = MethodFilters.name("selectOne", "selectList", "selectMap", "select", "insert", "update", "delete");
    final String[] sqlSessionImpls = { "org.apache.ibatis.session.defaults.DefaultSqlSession", "org.mybatis.spring.SqlSessionTemplate" };
    for (final String sqlSession : sqlSessionImpls) {
        transformTemplate.transform(sqlSession, new TransformCallback() {

            @Override
            public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
                final InstrumentClass target = instrumentor.getInstrumentClass(loader, sqlSession, classfileBuffer);
                final List<InstrumentMethod> methodsToTrace = target.getDeclaredMethods(methodFilter);
                for (InstrumentMethod methodToTrace : methodsToTrace) {
                    String sqlSessionOperationInterceptor = "com.navercorp.pinpoint.plugin.mybatis.interceptor.SqlSessionOperationInterceptor";
                    methodToTrace.addScopedInterceptor(sqlSessionOperationInterceptor, MYBATIS_SCOPE, ExecutionPolicy.BOUNDARY);
                }
                return target.toBytecode();
            }
        });
    }
}
Also used : ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) MethodFilter(com.navercorp.pinpoint.bootstrap.instrument.MethodFilter) 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) List(java.util.List)

Example 15 with InstrumentException

use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.

the class Log4jPlugin method setup.

@Override
public void setup(ProfilerPluginSetupContext context) {
    final Log4jConfig config = new Log4jConfig(context.getConfig());
    if (logger.isInfoEnabled()) {
        logger.info("Log4jPlugin config:{}", config);
    }
    if (!config.isLog4jLoggingTransactionInfo()) {
        logger.info("Log4j plugin is not executed because log4j transform enable config value is false.");
        return;
    }
    transformTemplate.transform("org.apache.log4j.spi.LoggingEvent", new TransformCallback() {

        @Override
        public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
            InstrumentClass mdcClass = instrumentor.getInstrumentClass(loader, "org.apache.log4j.MDC", null);
            if (mdcClass == null) {
                logger.warn("Can not modify. Because org.apache.log4j.MDC does not exist.");
                return null;
            }
            if (!mdcClass.hasMethod("put", "java.lang.String", "java.lang.Object")) {
                logger.warn("Can not modify. Because put method does not exist at org.apache.log4j.MDC class.");
                return null;
            }
            if (!mdcClass.hasMethod("remove", "java.lang.String")) {
                logger.warn("Can not modify. Because remove method does not exist at org.apache.log4j.MDC class.");
                return null;
            }
            InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
            if (!target.hasConstructor("java.lang.String", "org.apache.log4j.Category", "org.apache.log4j.Priority", "java.lang.Object", "java.lang.Throwable")) {
                logger.warn("Can not modify. Because constructor to modify not exist at org.apache.log4j.MDC class." + "\nThere is a strong presumption that your application use under version 1.2.14 log4j." + "\nconstructor prototype : LoggingEvent(String fqnOfCategoryClass, Category logger, Priority level, Object message, Throwable throwable);");
                return null;
            }
            if (!target.hasConstructor("java.lang.String", "org.apache.log4j.Category", "long", "org.apache.log4j.Priority", "java.lang.Object", "java.lang.Throwable")) {
                logger.warn("Can not modify. Because constructor to modify not exist at org.apache.log4j.MDC class." + "\nThere is a strong presumption that your application use under version 1.2.14 log4j." + "\nconstructor prototype : LoggingEvent(String fqnOfCategoryClass, Category logger, long timeStamp, Priority level, Object message, Throwable throwable);");
                return null;
            }
            if (!target.hasConstructor("java.lang.String", "org.apache.log4j.Category", "long", "org.apache.log4j.Level", "java.lang.Object", "java.lang.String", "org.apache.log4j.spi.ThrowableInformation", "java.lang.String", "org.apache.log4j.spi.LocationInfo", "java.util.Map")) {
                logger.warn("Can not modify. Because constructor to modify not exist at org.apache.log4j.MDC class. " + "\nThere is a strong presumption that your application use under version 1.2.14 log4j." + "\nconstructor prototype : LoggingEvent(final String fqnOfCategoryClass, final Category logger, final long timeStamp, final Level level, final Object message, final String threadName, final ThrowableInformation throwable, final String ndc, final LocationInfo info, final java.util.Map properties);");
                return null;
            }
            target.addInterceptor("com.navercorp.pinpoint.plugin.log4j.interceptor.LoggingEventOfLog4jInterceptor");
            return target.toBytecode();
        }
    });
}
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)

Aggregations

InstrumentException (com.navercorp.pinpoint.bootstrap.instrument.InstrumentException)35 InstrumentClass (com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass)29 Instrumentor (com.navercorp.pinpoint.bootstrap.instrument.Instrumentor)28 TransformCallback (com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback)28 ProtectionDomain (java.security.ProtectionDomain)28 InstrumentMethod (com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod)22 Method (java.lang.reflect.Method)6 Test (org.junit.Test)6 TestClassLoader (com.navercorp.pinpoint.test.classloader.TestClassLoader)5 NotFoundInstrumentException (com.navercorp.pinpoint.bootstrap.instrument.NotFoundInstrumentException)3 PinpointException (com.navercorp.pinpoint.exception.PinpointException)3 JavassistClassTest (com.navercorp.pinpoint.test.javasssit.JavassistClassTest)3 List (java.util.List)3 ProfilerConfig (com.navercorp.pinpoint.bootstrap.config.ProfilerConfig)2 MethodFilter (com.navercorp.pinpoint.bootstrap.instrument.MethodFilter)2 Interceptor (com.navercorp.pinpoint.bootstrap.interceptor.Interceptor)2 ObjectFactory (com.navercorp.pinpoint.bootstrap.plugin.ObjectFactory)2 AsyncTraceIdAccessor (com.navercorp.pinpoint.bootstrap.async.AsyncTraceIdAccessor)1 DatabaseInfo (com.navercorp.pinpoint.bootstrap.context.DatabaseInfo)1 PreparedStatementBindingMethodFilter (com.navercorp.pinpoint.bootstrap.plugin.jdbc.PreparedStatementBindingMethodFilter)1