Search in sources :

Example 6 with ConnectionFactory

use of com.newrelic.agent.bridge.datastore.ConnectionFactory in project newrelic-java-agent by newrelic.

the class DatabaseServiceTest method createConnectionFactory.

private ConnectionFactory createConnectionFactory(final Connection connection) {
    final JdbcDatabaseVendor jdbcDatabaseVendor = new JdbcDatabaseVendor("MySQL", "database", true) {

        @Override
        public DatastoreVendor getDatastoreVendor() {
            return DatastoreVendor.MySQL;
        }
    };
    ConnectionFactory connectionFactory = new ConnectionFactory() {

        @Override
        public Connection getConnection() throws SQLException {
            return connection;
        }

        @Override
        public DatabaseVendor getDatabaseVendor() {
            return jdbcDatabaseVendor;
        }
    };
    return connectionFactory;
}
Also used : ConnectionFactory(com.newrelic.agent.bridge.datastore.ConnectionFactory) JdbcDatabaseVendor(com.newrelic.agent.bridge.datastore.JdbcDatabaseVendor)

Example 7 with ConnectionFactory

use of com.newrelic.agent.bridge.datastore.ConnectionFactory in project newrelic-java-agent by newrelic.

the class RequestUriConfigTests method startSqlTracer.

private SqlTracer startSqlTracer(final String sql, final long duration) throws SQLException {
    DummyConnection conn = new DummyConnection();
    Statement statement = conn.createStatement();
    Transaction tx = Transaction.getTransaction();
    ClassMethodSignature sig = new ClassMethodSignature("com.foo.Statement", "executeQuery", "(Ljava/lang/String;)Ljava/sql/ResultSet;");
    SqlTracer sqlTracer = new OtherRootSqlTracer(tx, sig, statement, new SimpleMetricNameFormat(null)) {

        @Override
        public long getDuration() {
            return duration;
        }

        @Override
        public Object getSql() {
            return sql;
        }
    };
    sqlTracer.setConnectionFactory(new ConnectionFactory() {

        @Override
        public Connection getConnection() {
            return null;
        }

        @Override
        public DatabaseVendor getDatabaseVendor() {
            return UnknownDatabaseVendor.INSTANCE;
        }
    });
    sqlTracer.setRawSql(sql);
    tx.getTransactionActivity().tracerStarted(sqlTracer);
    return sqlTracer;
}
Also used : ConnectionFactory(com.newrelic.agent.bridge.datastore.ConnectionFactory) DatabaseVendor(com.newrelic.agent.bridge.datastore.DatabaseVendor) UnknownDatabaseVendor(com.newrelic.agent.bridge.datastore.UnknownDatabaseVendor) Transaction(com.newrelic.agent.Transaction) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) Statement(java.sql.Statement) DummyConnection(sql.DummyConnection) Connection(java.sql.Connection) DummyConnection(sql.DummyConnection) SqlTracer(com.newrelic.agent.tracers.SqlTracer) OtherRootSqlTracer(com.newrelic.agent.tracers.OtherRootSqlTracer) OtherRootSqlTracer(com.newrelic.agent.tracers.OtherRootSqlTracer) SimpleMetricNameFormat(com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat)

Example 8 with ConnectionFactory

use of com.newrelic.agent.bridge.datastore.ConnectionFactory in project newrelic-java-agent by newrelic.

the class DatabaseService method runExplainPlan.

public void runExplainPlan(SqlTracer sqlTracer) {
    ExplainPlanExecutor explainExecutor = sqlTracer.getExplainPlanExecutor();
    ConnectionFactory connectionFactory = sqlTracer.getConnectionFactory();
    if (explainExecutor == null || connectionFactory == null) {
        Agent.LOG.finest("Unable to execute query for explain plan");
        return;
    }
    if (sqlTracer.hasExplainPlan()) {
        return;
    }
    runExplainPlan(explainExecutor, connectionFactory);
}
Also used : ConnectionFactory(com.newrelic.agent.bridge.datastore.ConnectionFactory)

Example 9 with ConnectionFactory

use of com.newrelic.agent.bridge.datastore.ConnectionFactory in project newrelic-java-agent by newrelic.

the class DatabaseServiceTest method instanceSlowSqlAttribute.

@Test
public void instanceSlowSqlAttribute() throws Exception {
    Map<String, Object> configMap = createStagingMap();
    Map<String, Object> ttConfigMap = createMap();
    ttConfigMap.put(TransactionTracerConfigImpl.ENABLED, true);
    ttConfigMap.put(TransactionTracerConfigImpl.COLLECT_TRACES, true);
    ttConfigMap.put(TransactionTracerConfigImpl.EXPLAIN_THRESHOLD, 0);
    configMap.put(AgentConfigImpl.TRANSACTION_TRACER, ttConfigMap);
    Map<String, Object> dsConfigMap = createMap();
    dsConfigMap.put(DatastoreConfigImpl.INSTANCE_REPORTING, true);
    configMap.put(AgentConfigImpl.DATASTORE_TRACER, dsConfigMap);
    Map<String, Object> stConfigMap = createMap();
    stConfigMap.put(SqlTraceConfigImpl.ENABLED, true);
    configMap.put(AgentConfigImpl.SLOW_SQL, stConfigMap);
    createServiceManager(configMap);
    final Connection connection = Mockito.mock(Connection.class);
    final String databaseName = "myDatabase";
    Mockito.when(connection.getCatalog()).thenReturn(databaseName);
    DatastoreInstanceDetection.detectConnectionAddress();
    InetSocketAddress address = new InetSocketAddress("address", 8080);
    DatastoreInstanceDetection.associateAddress(connection, address);
    DatastoreInstanceDetection.stopDetectingConnectionAddress();
    Transaction transaction = Transaction.getTransaction(true);
    ClassMethodSignature sig = new ClassMethodSignature("com.foo.Statement", "executeQuery", "(Ljava/lang/String;)Ljava/sql/ResultSet;");
    TestDefaultSqlTracer tracer = new TestDefaultSqlTracer(transaction, sig, null, new SimpleMetricNameFormat(null), DefaultTracer.DEFAULT_TRACER_FLAGS);
    tracer.setRawSql("select * from metrics");
    tracer.provideConnection(connection);
    ConnectionFactory connectionFactory = createConnectionFactory(connection);
    tracer.setConnectionFactory(connectionFactory);
    AgentHelper.setLastTracer(tracer);
    tracer.finish(Opcodes.ARETURN, new DummyResultSet());
    Assert.assertEquals(address.getHostName(), tracer.getAgentAttribute(DatastoreMetrics.DATASTORE_HOST));
    Assert.assertEquals(String.valueOf(address.getPort()), tracer.getAgentAttribute(DatastoreMetrics.DATASTORE_PORT_PATH_OR_ID));
    Assert.assertEquals(databaseName, tracer.getAgentAttribute(DatastoreMetrics.DB_INSTANCE));
    Transaction.clearTransaction();
}
Also used : ConnectionFactory(com.newrelic.agent.bridge.datastore.ConnectionFactory) Transaction(com.newrelic.agent.Transaction) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) InetSocketAddress(java.net.InetSocketAddress) Connection(java.sql.Connection) SimpleMetricNameFormat(com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat) DummyResultSet(sql.DummyResultSet) Test(org.junit.Test)

Aggregations

ConnectionFactory (com.newrelic.agent.bridge.datastore.ConnectionFactory)9 Connection (java.sql.Connection)6 Transaction (com.newrelic.agent.Transaction)5 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)5 SimpleMetricNameFormat (com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat)5 DatabaseVendor (com.newrelic.agent.bridge.datastore.DatabaseVendor)3 SqlTracer (com.newrelic.agent.tracers.SqlTracer)3 Test (org.junit.Test)3 DummyResultSet (sql.DummyResultSet)3 UnknownDatabaseVendor (com.newrelic.agent.bridge.datastore.UnknownDatabaseVendor)2 ExplainPlanExecutor (com.newrelic.agent.database.ExplainPlanExecutor)2 OtherRootSqlTracer (com.newrelic.agent.tracers.OtherRootSqlTracer)2 InetSocketAddress (java.net.InetSocketAddress)2 Statement (java.sql.Statement)2 DummyConnection (sql.DummyConnection)2 JdbcDatabaseVendor (com.newrelic.agent.bridge.datastore.JdbcDatabaseVendor)1 DatabaseService (com.newrelic.agent.database.DatabaseService)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1