Search in sources :

Example 1 with DatabaseVendor

use of com.newrelic.agent.bridge.datastore.DatabaseVendor 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 DatabaseVendor

use of com.newrelic.agent.bridge.datastore.DatabaseVendor 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 3 with DatabaseVendor

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

the class Driver_Weaved method connect.

public Connection connect(String url, Properties props) {
    boolean firstInConnectPath = !DatastoreInstanceDetection.shouldDetectConnectionAddress();
    try {
        DatastoreInstanceDetection.detectConnectionAddress();
        Connection connection = Weaver.callOriginal();
        DatastoreInstanceDetection.associateAddress(connection);
        if (!JdbcHelper.connectionFactoryExists(connection)) {
            String detectedUrl = JdbcHelper.getConnectionURL(connection);
            if (detectedUrl == null) {
                return connection;
            }
            // Detect correct vendor type and then store new connection factory based on URL
            DatabaseVendor vendor = JdbcHelper.getVendor(getClass(), detectedUrl);
            JdbcHelper.putConnectionFactory(detectedUrl, new JdbcDriverConnectionFactory(vendor, (Driver) this, url, props));
        }
        return connection;
    } finally {
        if (firstInConnectPath) {
            DatastoreInstanceDetection.stopDetectingConnectionAddress();
        }
    }
}
Also used : JdbcDriverConnectionFactory(com.newrelic.agent.bridge.datastore.JdbcDriverConnectionFactory) DatabaseVendor(com.newrelic.agent.bridge.datastore.DatabaseVendor)

Example 4 with DatabaseVendor

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

the class DataSource_Weaved method getConnection.

// This is a leaf tracer because it's common for these methods to delegate to each other and we don't want double
// counts
@Trace(leaf = true)
public Connection getConnection() throws Exception {
    boolean firstInConnectPath = !DatastoreInstanceDetection.shouldDetectConnectionAddress();
    try {
        DatastoreInstanceDetection.detectConnectionAddress();
        Connection connection = Weaver.callOriginal();
        AgentBridge.getAgent().getTracedMethod().addRollupMetricName(DatastoreMetrics.DATABASE_GET_CONNECTION);
        DatastoreInstanceDetection.associateAddress(connection);
        if (!JdbcHelper.connectionFactoryExists(connection)) {
            String url = JdbcHelper.getConnectionURL(connection);
            if (url == null) {
                return connection;
            }
            // Detect correct vendor type and then store new connection factory based on URL
            DatabaseVendor vendor = JdbcHelper.getVendor(getClass(), url);
            JdbcHelper.putConnectionFactory(url, new JdbcDataSourceConnectionFactory(vendor, (DataSource) this));
        }
        return connection;
    } catch (Exception e) {
        AgentBridge.getAgent().getMetricAggregator().incrementCounter(DatastoreMetrics.DATABASE_ERRORS_ALL);
        throw e;
    } finally {
        if (firstInConnectPath) {
            DatastoreInstanceDetection.stopDetectingConnectionAddress();
        }
    }
}
Also used : DatabaseVendor(com.newrelic.agent.bridge.datastore.DatabaseVendor) JdbcDataSourceConnectionFactory(com.newrelic.agent.bridge.datastore.JdbcDataSourceConnectionFactory) Connection(java.sql.Connection) Trace(com.newrelic.api.agent.Trace)

Example 5 with DatabaseVendor

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

the class Driver_Instrumentation method connect.

public Connection connect(String url, Properties props) throws SQLException {
    boolean firstInConnectPath = !DatastoreInstanceDetection.shouldDetectConnectionAddress();
    try {
        DatastoreInstanceDetection.detectConnectionAddress();
        Connection connection = Weaver.callOriginal();
        DatastoreInstanceDetection.associateAddress(connection);
        if (!JdbcHelper.connectionFactoryExists(connection)) {
            String detectedUrl = JdbcHelper.getConnectionURL(connection);
            if (detectedUrl == null) {
                return connection;
            }
            // Detect correct vendor type and then store new connection factory based on URL
            DatabaseVendor vendor = JdbcHelper.getVendor(getClass(), detectedUrl);
            JdbcHelper.putConnectionFactory(detectedUrl, new JdbcDriverConnectionFactory(vendor, (java.sql.Driver) this, url, props));
        }
        return connection;
    } finally {
        if (firstInConnectPath) {
            DatastoreInstanceDetection.stopDetectingConnectionAddress();
        }
    }
}
Also used : JdbcDriverConnectionFactory(com.newrelic.agent.bridge.datastore.JdbcDriverConnectionFactory) DatabaseVendor(com.newrelic.agent.bridge.datastore.DatabaseVendor) Connection(java.sql.Connection)

Aggregations

DatabaseVendor (com.newrelic.agent.bridge.datastore.DatabaseVendor)11 Connection (java.sql.Connection)9 JdbcDataSourceConnectionFactory (com.newrelic.agent.bridge.datastore.JdbcDataSourceConnectionFactory)4 Trace (com.newrelic.api.agent.Trace)4 ConnectionFactory (com.newrelic.agent.bridge.datastore.ConnectionFactory)3 Transaction (com.newrelic.agent.Transaction)2 JdbcDriverConnectionFactory (com.newrelic.agent.bridge.datastore.JdbcDriverConnectionFactory)2 UnknownDatabaseVendor (com.newrelic.agent.bridge.datastore.UnknownDatabaseVendor)2 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)2 OtherRootSqlTracer (com.newrelic.agent.tracers.OtherRootSqlTracer)2 SqlTracer (com.newrelic.agent.tracers.SqlTracer)2 SimpleMetricNameFormat (com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 DataSource (javax.sql.DataSource)2 DummyConnection (sql.DummyConnection)2 DatastoreVendor (com.newrelic.agent.bridge.datastore.DatastoreVendor)1 RecordSql (com.newrelic.agent.bridge.datastore.RecordSql)1 DatabaseService (com.newrelic.agent.database.DatabaseService)1 ExplainPlanExecutor (com.newrelic.agent.database.ExplainPlanExecutor)1