Search in sources :

Example 16 with InstrumentException

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

the class LogbackPlugin method setup.

@Override
public void setup(ProfilerPluginSetupContext context) {
    final LogbackConfig config = new LogbackConfig(context.getConfig());
    if (logger.isInfoEnabled()) {
        logger.info("LogbackPlugin config:{}", config);
    }
    if (!config.isLogbackLoggingTransactionInfo()) {
        logger.info("Logback plugin is not executed because logback transform enable config value is false.");
        return;
    }
    transformTemplate.transform("ch.qos.logback.classic.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.slf4j.MDC", null);
            if (mdcClass == null) {
                logger.warn("Can not modify. Because org.slf4j.MDC does not exist.");
                return null;
            }
            if (!mdcClass.hasMethod("put", "java.lang.String", "java.lang.String")) {
                logger.warn("Can not modify. Because put method does not exist at org.slf4j.MDC class.");
                return null;
            }
            if (!mdcClass.hasMethod("remove", "java.lang.String")) {
                logger.warn("Can not modify. Because remove method does not exist at org.slf4j.MDC class.");
                return null;
            }
            InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
            if (!target.hasConstructor()) {
                logger.warn("Can not modify. Because constructor to modify not exist at ch.qos.logback.classic.spi.LoggingEvent class." + "\nconstructor prototype : LoggingEvent();");
                return null;
            }
            if (!target.hasConstructor("java.lang.String", "ch.qos.logback.classic.Logger", "ch.qos.logback.classic.Level", "java.lang.String", "java.lang.Throwable", "java.lang.Object[]")) {
                logger.warn("Can not modify. Because constructor to modify not exist at ch.qos.logback.classic.spi.LoggingEvent class." + "\nconstructor prototype : LoggingEvent(String fqcn, Logger logger, Level level, String message, Throwable throwable, Object[] argArray);");
                return null;
            }
            target.addInterceptor("com.navercorp.pinpoint.plugin.logback.interceptor.LoggingEventOfLogbackInterceptor");
            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)

Example 17 with InstrumentException

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

the class JavassistClassTest method testBeforeAddInterceptor.

@Test
public void testBeforeAddInterceptor() throws Exception {
    final TestClassLoader loader = getTestClassLoader();
    final String javassistClassName = "com.navercorp.pinpoint.test.javasssit.mock.TestObject";
    loader.addTransformer(javassistClassName, new TransformCallback() {

        @Override
        public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
            try {
                logger.info("modify className:{} cl:{}", className, classLoader);
                InstrumentClass aClass = instrumentor.getInstrumentClass(classLoader, javassistClassName, classfileBuffer);
                String methodName = "callA";
                InstrumentMethod callaMethod = aClass.getDeclaredMethod(methodName);
                callaMethod.addInterceptor("com.navercorp.pinpoint.test.javasssit.TestBeforeInterceptor");
                byte[] bytes = aClass.toBytecode();
                return bytes;
            } catch (Throwable e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    });
    Class<?> testObjectClazz = loader.loadClass(javassistClassName);
    final String methodName = "callA";
    logger.info("class:{}", testObjectClazz.toString());
    logger.info("class cl:{}", testObjectClazz.getClassLoader());
    final Object testObject = testObjectClazz.newInstance();
    Method callA = testObjectClazz.getMethod(methodName);
    callA.invoke(testObject);
    Interceptor interceptor = getInterceptor(loader, 0);
    assertEqualsIntField(interceptor, "call", 1);
    assertEqualsObjectField(interceptor, "className", "com.navercorp.pinpoint.test.javasssit.mock.TestObject");
    assertEqualsObjectField(interceptor, "methodName", methodName);
    assertEqualsObjectField(interceptor, "args", null);
    assertEqualsObjectField(interceptor, "target", testObject);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) Method(java.lang.reflect.Method) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) Interceptor(com.navercorp.pinpoint.bootstrap.interceptor.Interceptor) Test(org.junit.Test)

Example 18 with InstrumentException

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);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) Method(java.lang.reflect.Method) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) Interceptor(com.navercorp.pinpoint.bootstrap.interceptor.Interceptor) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 19 with InstrumentException

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

the class HttpClientPlugin method addHttpRequestClass.

private void addHttpRequestClass(final HttpClientPluginConfig config) {
    transformTemplate.transform("com.google.api.client.http.HttpRequest", 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);
            InstrumentMethod execute = target.getDeclaredMethod("execute");
            if (execute != null) {
                execute.addInterceptor("com.navercorp.pinpoint.plugin.google.httpclient.interceptor.HttpRequestExecuteMethodInterceptor");
            }
            if (config.isAsync()) {
                InstrumentMethod executeAsync = target.getDeclaredMethod("executeAsync", "java.util.concurrent.Executor");
                if (executeAsync != null) {
                    executeAsync.addInterceptor("com.navercorp.pinpoint.plugin.google.httpclient.interceptor.HttpRequestExecuteAsyncMethodInterceptor");
                }
                for (InstrumentClass nestedClass : target.getNestedClasses(ClassFilters.chain(ClassFilters.enclosingMethod("executeAsync", "java.util.concurrent.Executor"), ClassFilters.interfaze("java.util.concurrent.Callable")))) {
                    logger.debug("Find nested class {}", target.getName());
                    instrumentor.transform(loader, nestedClass.getName(), 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.addField(AsyncTraceIdAccessor.class.getName());
                            InstrumentMethod constructor = target.getConstructor("com.google.api.client.http.HttpRequest");
                            if (constructor != null) {
                                logger.debug("Add constuctor interceptor for nested class {}", target.getName());
                                constructor.addInterceptor("com.navercorp.pinpoint.plugin.google.httpclient.interceptor.HttpRequestExecuteAsyncMethodInnerClassConstructorInterceptor");
                            }
                            InstrumentMethod m = target.getDeclaredMethod("call");
                            if (m != null) {
                                logger.debug("Add method interceptor for nested class {}.{}", target.getName(), m.getName());
                                m.addInterceptor("com.navercorp.pinpoint.plugin.google.httpclient.interceptor.HttpRequestExecuteAsyncMethodInnerClassCallMethodInterceptor");
                            }
                            return target.toBytecode();
                        }
                    });
                }
            }
            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) AsyncTraceIdAccessor(com.navercorp.pinpoint.bootstrap.async.AsyncTraceIdAccessor) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass)

Example 20 with InstrumentException

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

the class ThriftPlugin method addTTransportEditor.

// Common - transports
private void addTTransportEditor(String tTransportFqcn) {
    final String targetClassName = tTransportFqcn;
    transformTemplate.transform(targetClassName, 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, className, classfileBuffer);
            target.addField(ThriftConstants.FIELD_ACCESSOR_SOCKET);
            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