Search in sources :

Example 1 with ConnectionFactory

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

the class TransactionTrace method runExplainPlans.

private void runExplainPlans() {
    if (!sqlTracers.isEmpty()) {
        DatabaseService dbService = ServiceFactory.getDatabaseService();
        for (Entry<ConnectionFactory, List<ExplainPlanExecutor>> entry : sqlTracers.entrySet()) {
            Agent.LOG.finer(MessageFormat.format("Running {0} explain plan(s)", entry.getValue().size()));
            Connection connection = null;
            try {
                connection = entry.getKey().getConnection();
                DatabaseVendor vendor = entry.getKey().getDatabaseVendor();
                for (ExplainPlanExecutor explainExecutor : entry.getValue()) {
                    if (explainExecutor != null) {
                        explainExecutor.runExplainPlan(dbService, connection, vendor);
                    }
                }
            } catch (Throwable t) {
                String msg = MessageFormat.format("An error occurred executing an explain plan: {0}", t.toString());
                if (Agent.LOG.isLoggable(Level.FINER)) {
                    Agent.LOG.log(Level.FINER, msg, t);
                } else {
                    Agent.LOG.fine(msg);
                }
            } finally {
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (Exception e) {
                        Agent.LOG.log(Level.FINER, "Unable to close connection", e);
                    }
                }
            }
        }
        sqlTracers.clear();
    }
}
Also used : ConnectionFactory(com.newrelic.agent.bridge.datastore.ConnectionFactory) DatabaseVendor(com.newrelic.agent.bridge.datastore.DatabaseVendor) Connection(java.sql.Connection) ExplainPlanExecutor(com.newrelic.agent.database.ExplainPlanExecutor) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) DatabaseService(com.newrelic.agent.database.DatabaseService) IOException(java.io.IOException)

Example 2 with ConnectionFactory

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

the class TransactionTrace method processSqlTracer.

private void processSqlTracer(Tracer tracer) {
    if (tracer instanceof SqlTracer) {
        SqlTracer sqlTracer = (SqlTracer) tracer;
        ExplainPlanExecutor explainExecutor = sqlTracer.getExplainPlanExecutor();
        ConnectionFactory connectionFactory = sqlTracer.getConnectionFactory();
        if (!sqlTracer.hasExplainPlan() && explainExecutor != null && connectionFactory != null) {
            List<ExplainPlanExecutor> tracers = sqlTracers.get(connectionFactory);
            if (tracers == null) {
                tracers = new LinkedList<>();
                sqlTracers.put(connectionFactory, tracers);
            }
            tracers.add(explainExecutor);
        }
    }
}
Also used : ConnectionFactory(com.newrelic.agent.bridge.datastore.ConnectionFactory) ExplainPlanExecutor(com.newrelic.agent.database.ExplainPlanExecutor) SqlTracer(com.newrelic.agent.tracers.SqlTracer)

Example 3 with ConnectionFactory

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

the class SqlTraceServiceTest 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() throws SQLException {
            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) SQLException(java.sql.SQLException) 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 4 with ConnectionFactory

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

the class DatabaseServiceTest method instanceLocalhostReplace.

@Test
public void instanceLocalhostReplace() throws Exception {
    Map<String, Object> configMap = createStagingMap();
    Map<String, Object> dsConfigMap = createMap();
    dsConfigMap.put(DatastoreConfigImpl.INSTANCE_REPORTING, true);
    configMap.put(AgentConfigImpl.DATASTORE_TRACER, dsConfigMap);
    createServiceManager(configMap);
    final Connection connection = Mockito.mock(Connection.class);
    DatastoreInstanceDetection.detectConnectionAddress();
    DatastoreInstanceDetection.associateAddress(connection, new InetSocketAddress("localhost", 8080));
    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");
    AgentHelper.setLastTracer(tracer);
    tracer.provideConnection(connection);
    ConnectionFactory connectionFactory = createConnectionFactory(connection);
    tracer.setConnectionFactory(connectionFactory);
    tracer.finish(Opcodes.ARETURN, new DummyResultSet());
    String notExpectedRollupMetricName = getScopedInstanceDBMetric(DatastoreVendor.MySQL.name(), "localhost", "8080");
    Assert.assertFalse(tracer.getRolledUpMetricNamesForTesting().contains(notExpectedRollupMetricName));
    String expectedRollupMetricName = getScopedInstanceDBMetric(DatastoreVendor.MySQL.name(), DatastoreMetrics.HOSTNAME, "8080");
    Assert.assertTrue(String.format("Expected instance metric %s found", expectedRollupMetricName), tracer.getRolledUpMetricNamesForTesting().contains(expectedRollupMetricName));
    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)

Example 5 with ConnectionFactory

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

the class DatabaseServiceTest method instanceDisabled.

@Test
public void instanceDisabled() throws Exception {
    Map<String, Object> configMap = createStagingMap();
    Map<String, Object> dbConfigMap = createMap();
    Map<String, Object> nestedMap = new HashMap<>();
    nestedMap.put(DatastoreConfigImpl.ENABLED, !DatastoreConfigImpl.INSTANCE_REPORTING_DEFAULT_ENABLED);
    dbConfigMap.put(DatastoreConfigImpl.INSTANCE_REPORTING, nestedMap);
    configMap.put(AgentConfigImpl.DATASTORE_TRACER, dbConfigMap);
    createServiceManager(configMap);
    final Connection connection = Mockito.mock(Connection.class);
    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);
    ConnectionFactory connectionFactory = createConnectionFactory(connection);
    tracer.setConnectionFactory(connectionFactory);
    tracer.setRawSql("select * from metrics");
    tracer.provideConnection(connection);
    AgentHelper.setLastTracer(tracer);
    tracer.finish(Opcodes.ARETURN, new DummyResultSet());
    String expectedRollupMetricName = getScopedInstanceDBMetric(DatastoreVendor.MySQL.name(), "unknown", "unknown");
    Assert.assertFalse(tracer.getRolledUpMetricNamesForTesting().contains(expectedRollupMetricName));
    Transaction.clearTransaction();
}
Also used : ConnectionFactory(com.newrelic.agent.bridge.datastore.ConnectionFactory) Transaction(com.newrelic.agent.Transaction) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) HashMap(java.util.HashMap) 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