Search in sources :

Example 1 with DatabaseInfo

use of com.navercorp.pinpoint.bootstrap.context.DatabaseInfo in project pinpoint by naver.

the class DefaultJdbcUrlParsingService method parseJdbcUrl.

@Override
public DatabaseInfo parseJdbcUrl(ServiceType serviceType, String jdbcUrl) {
    if (jdbcUrl == null) {
        return UnKnownDatabaseInfo.INSTANCE;
    }
    CacheKey cacheKey = new CacheKey(serviceType, jdbcUrl);
    DatabaseInfo cacheValue = eachServiceTypeCache.get(cacheKey);
    if (cacheValue != null) {
        return cacheValue;
    }
    for (JdbcUrlParserV2 parser : jdbcUrlParserList) {
        if (serviceType == parser.getServiceType()) {
            DatabaseInfo databaseInfo = parser.parse(jdbcUrl);
            return putCacheIfAbsent(cacheKey, databaseInfo);
        }
    }
    return putCacheIfAbsent(cacheKey, UnKnownDatabaseInfo.createUnknownDataBase(jdbcUrl));
}
Also used : UnKnownDatabaseInfo(com.navercorp.pinpoint.bootstrap.plugin.jdbc.UnKnownDatabaseInfo) DatabaseInfo(com.navercorp.pinpoint.bootstrap.context.DatabaseInfo) JdbcUrlParserV2(com.navercorp.pinpoint.bootstrap.plugin.jdbc.JdbcUrlParserV2)

Example 2 with DatabaseInfo

use of com.navercorp.pinpoint.bootstrap.context.DatabaseInfo in project pinpoint by naver.

the class AccessorInjectionTest method addTraceValue.

@Test
public void addTraceValue() 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 loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
            try {
                logger.info("modify cl:{}", loader);
                InstrumentClass aClass = instrumentor.getInstrumentClass(loader, javassistClassName, classfileBuffer);
                aClass.addField(ObjectTraceValue.class.getName());
                aClass.addField(IntTraceValue.class.getName());
                aClass.addField(IntArrayTraceValue.class.getName());
                aClass.addField(IntegerArrayTraceValue.class.getName());
                aClass.addField(DatabaseInfoTraceValue.class.getName());
                aClass.addField(BindValueTraceValue.class.getName());
                String methodName = "callA";
                aClass.getDeclaredMethod(methodName).addInterceptor("com.navercorp.pinpoint.test.javasssit.TestBeforeInterceptor");
                return aClass.toBytecode();
            } catch (InstrumentException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    });
    Class<?> testObjectClazz = loader.loadClass(javassistClassName);
    final String methodName = "callA";
    logger.info("class:{}", testObjectClazz.toString());
    final Object testObject = testObjectClazz.newInstance();
    Method callA = testObjectClazz.getMethod(methodName);
    callA.invoke(testObject);
    Class<?> objectTraceValue = loader.loadClass(ObjectTraceValue.class.getName());
    Assert.assertTrue("ObjectTraceValue implements fail", objectTraceValue.isInstance(testObject));
    objectTraceValue.getMethod("_$PINPOINT$_setTraceObject", Object.class).invoke(testObject, "a");
    Object get = objectTraceValue.getMethod("_$PINPOINT$_getTraceObject").invoke(testObject);
    Assert.assertEquals("a", get);
    Class<?> intTraceValue = loader.loadClass(IntTraceValue.class.getName());
    Assert.assertTrue("IntTraceValue implements fail", intTraceValue.isInstance(testObject));
    intTraceValue.getMethod("_$PINPOINT$_setTraceInt", int.class).invoke(testObject, 1);
    int a = (Integer) intTraceValue.getMethod("_$PINPOINT$_getTraceInt").invoke(testObject);
    Assert.assertEquals(1, a);
    Class<?> intArrayTraceValue = loader.loadClass(IntArrayTraceValue.class.getName());
    Assert.assertTrue("IntArrayTraceValue implements fail", intArrayTraceValue.isInstance(testObject));
    int[] expectedInts = { 1, 2, 3 };
    intArrayTraceValue.getMethod("_$PINPOINT$_setTraceIntArray", int[].class).invoke(testObject, expectedInts);
    int[] ints = (int[]) intArrayTraceValue.getMethod("_$PINPOINT$_getTraceIntArray").invoke(testObject);
    Assert.assertEquals(expectedInts, ints);
    Class<?> integerArrayTraceValue = loader.loadClass(IntegerArrayTraceValue.class.getName());
    Assert.assertTrue("IntegerArrayTraceValue implements fail", integerArrayTraceValue.isInstance(testObject));
    Integer[] expectedIntegers = { 1, 2 };
    // wrap due to vararg expansion
    Object[] wrappedExpectedIntegers = new Object[] { expectedIntegers };
    integerArrayTraceValue.getMethod("_$PINPOINT$_setTraceIntegerArray", Integer[].class).invoke(testObject, wrappedExpectedIntegers);
    Integer[] integers = (Integer[]) integerArrayTraceValue.getMethod("_$PINPOINT$_getTraceIntegerArray").invoke(testObject);
    Assert.assertArrayEquals(expectedIntegers, integers);
    Class<?> databaseTraceValue = loader.loadClass(DatabaseInfoTraceValue.class.getName());
    Assert.assertTrue("DatabaseInfoTraceValue implements fail", databaseTraceValue.isInstance(testObject));
    databaseTraceValue.getMethod("_$PINPOINT$_setTraceDatabaseInfo", DatabaseInfo.class).invoke(testObject, UnKnownDatabaseInfo.INSTANCE);
    Object databaseInfo = databaseTraceValue.getMethod("_$PINPOINT$_getTraceDatabaseInfo").invoke(testObject);
    Assert.assertSame(UnKnownDatabaseInfo.INSTANCE, databaseInfo);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) UnKnownDatabaseInfo(com.navercorp.pinpoint.bootstrap.plugin.jdbc.UnKnownDatabaseInfo) DatabaseInfo(com.navercorp.pinpoint.bootstrap.context.DatabaseInfo) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) 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) Test(org.junit.Test) JavassistClassTest(com.navercorp.pinpoint.test.javasssit.JavassistClassTest)

Example 3 with DatabaseInfo

use of com.navercorp.pinpoint.bootstrap.context.DatabaseInfo in project pinpoint by naver.

the class DefaultDataSourceMetric method collectDataSource.

private TDataSource collectDataSource(DataSourceMonitorWrapper dataSourceMonitor) {
    TDataSource dataSource = new TDataSource();
    dataSource.setId(dataSourceMonitor.getId());
    dataSource.setServiceTypeCode(dataSourceMonitor.getServiceType().getCode());
    String jdbcUrl = dataSourceMonitor.getUrl();
    if (jdbcUrl != null) {
        dataSource.setUrl(jdbcUrl);
        DatabaseInfo databaseInfo = jdbcUrlParsingService.getDatabaseInfo(jdbcUrl);
        if (databaseInfo != null) {
            dataSource.setDatabaseName(databaseInfo.getDatabaseId());
        }
    }
    int activeConnectionSize = dataSourceMonitor.getActiveConnectionSize();
    // this field is optional (default value is 0)
    if (activeConnectionSize != 0) {
        dataSource.setActiveConnectionSize(activeConnectionSize);
    }
    dataSource.setMaxConnectionSize(dataSourceMonitor.getMaxConnectionSize());
    return dataSource;
}
Also used : DatabaseInfo(com.navercorp.pinpoint.bootstrap.context.DatabaseInfo) TDataSource(com.navercorp.pinpoint.thrift.dto.TDataSource)

Example 4 with DatabaseInfo

use of com.navercorp.pinpoint.bootstrap.context.DatabaseInfo in project pinpoint by naver.

the class PreparedStatementCreateInterceptor method prepareAfterTrace.

@Override
protected void prepareAfterTrace(Object target, Object[] args, Object result, Throwable throwable) {
    final boolean success = InterceptorUtils.isSuccess(throwable);
    if (success) {
        if (target instanceof DatabaseInfoAccessor) {
            // set databaseInfo to PreparedStatement only when preparedStatement is generated successfully.
            DatabaseInfo databaseInfo = ((DatabaseInfoAccessor) target)._$PINPOINT$_getDatabaseInfo();
            if (databaseInfo != null) {
                if (result instanceof DatabaseInfoAccessor) {
                    ((DatabaseInfoAccessor) result)._$PINPOINT$_setDatabaseInfo(databaseInfo);
                }
            }
        }
        if (result instanceof ParsingResultAccessor) {
            // 1. Don't check traceContext. preparedStatement can be created in other thread.
            // 2. While sampling is active, the thread which creates preparedStatement could not be a sampling target. So record sql anyway. 
            String sql = (String) args[0];
            ParsingResult parsingResult = traceContext.parseSql(sql);
            if (parsingResult != null) {
                ((ParsingResultAccessor) result)._$PINPOINT$_setParsingResult(parsingResult);
            } else {
                if (logger.isErrorEnabled()) {
                    logger.error("sqlParsing fail. parsingResult is null sql:{}", sql);
                }
            }
        }
    }
}
Also used : ParsingResult(com.navercorp.pinpoint.bootstrap.context.ParsingResult) UnKnownDatabaseInfo(com.navercorp.pinpoint.bootstrap.plugin.jdbc.UnKnownDatabaseInfo) DatabaseInfo(com.navercorp.pinpoint.bootstrap.context.DatabaseInfo) DatabaseInfoAccessor(com.navercorp.pinpoint.bootstrap.plugin.jdbc.DatabaseInfoAccessor) ParsingResultAccessor(com.navercorp.pinpoint.bootstrap.plugin.jdbc.ParsingResultAccessor)

Example 5 with DatabaseInfo

use of com.navercorp.pinpoint.bootstrap.context.DatabaseInfo in project pinpoint by naver.

the class StatementExecuteQueryInterceptor method doInBeforeTrace.

@Override
public void doInBeforeTrace(SpanEventRecorder recorder, final Object target, Object[] args) {
    /*
         * If method was not called by request handler, we skip tagging.
         */
    DatabaseInfo databaseInfo = (target instanceof DatabaseInfoAccessor) ? ((DatabaseInfoAccessor) target)._$PINPOINT$_getDatabaseInfo() : null;
    if (databaseInfo == null) {
        databaseInfo = UnKnownDatabaseInfo.INSTANCE;
    }
    recorder.recordServiceType(databaseInfo.getExecuteQueryType());
    recorder.recordEndPoint(databaseInfo.getMultipleHost());
    recorder.recordDestinationId(databaseInfo.getDatabaseId());
}
Also used : UnKnownDatabaseInfo(com.navercorp.pinpoint.bootstrap.plugin.jdbc.UnKnownDatabaseInfo) DatabaseInfo(com.navercorp.pinpoint.bootstrap.context.DatabaseInfo) DatabaseInfoAccessor(com.navercorp.pinpoint.bootstrap.plugin.jdbc.DatabaseInfoAccessor)

Aggregations

DatabaseInfo (com.navercorp.pinpoint.bootstrap.context.DatabaseInfo)74 Test (org.junit.Test)48 UnKnownDatabaseInfo (com.navercorp.pinpoint.bootstrap.plugin.jdbc.UnKnownDatabaseInfo)25 DatabaseInfoAccessor (com.navercorp.pinpoint.bootstrap.plugin.jdbc.DatabaseInfoAccessor)20 DefaultDatabaseInfo (com.navercorp.pinpoint.bootstrap.plugin.jdbc.DefaultDatabaseInfo)8 ParsingResult (com.navercorp.pinpoint.bootstrap.context.ParsingResult)4 SpanEventRecorder (com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder)4 Trace (com.navercorp.pinpoint.bootstrap.context.Trace)4 ParsingResultAccessor (com.navercorp.pinpoint.bootstrap.plugin.jdbc.ParsingResultAccessor)4 RegularStatement (com.datastax.driver.core.RegularStatement)2 BindValueAccessor (com.navercorp.pinpoint.bootstrap.plugin.jdbc.BindValueAccessor)2 BoundStatement (com.datastax.driver.core.BoundStatement)1 Cluster (com.datastax.driver.core.Cluster)1 Host (com.datastax.driver.core.Host)1 InstrumentClass (com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass)1 InstrumentException (com.navercorp.pinpoint.bootstrap.instrument.InstrumentException)1 Instrumentor (com.navercorp.pinpoint.bootstrap.instrument.Instrumentor)1 TransformCallback (com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback)1 JdbcUrlParserV2 (com.navercorp.pinpoint.bootstrap.plugin.jdbc.JdbcUrlParserV2)1 ServiceType (com.navercorp.pinpoint.common.trace.ServiceType)1