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();
}
}
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);
}
}
}
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;
}
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();
}
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();
}
Aggregations