Search in sources :

Example 1 with DummyResultSet

use of sql.DummyResultSet in project newrelic-java-agent by newrelic.

the class DefaultSqlTracerTest method testSpanEventDatastoreTruncationAtExactLimit.

@Test
public void testSpanEventDatastoreTruncationAtExactLimit() throws SQLException {
    final Connection connection = mock(Connection.class);
    DatastoreInstanceDetection.detectConnectionAddress();
    DatastoreInstanceDetection.associateAddress(connection, new InetSocketAddress("dbserver.nerd.us", 9945));
    DatastoreInstanceDetection.stopDetectingConnectionAddress();
    String longQueryString = "SELECT price, name FROM BOOKS WHERE name = " + Strings.repeat("a", 1957);
    DefaultSqlTracer tracer = newInstanceDBTracer(longQueryString, connection, "MySQL", "mysql");
    tracer.finish(Opcodes.ARETURN, new DummyResultSet());
    SpanEventsService spanEventService = ServiceFactory.getSpanEventService();
    ((SpanEventsServiceImpl) spanEventService).dispatcherTransactionFinished(new TransactionData(tracer.getTransaction(), 1024), new TransactionStats());
    SamplingPriorityQueue<SpanEvent> eventPool = spanEventService.getOrCreateDistributedSamplingReservoir(APP_NAME);
    List<SpanEvent> spanEvents = eventPool.asList();
    assertNotNull(spanEvents);
    assertEquals(1, spanEvents.size());
    SpanEvent spanEvent = Iterables.getFirst(spanEvents, null);
    assertNotNull(spanEvent);
    assertEquals("datastore", spanEvent.getIntrinsics().get("category"));
    assertEquals("MySQL", spanEvent.getIntrinsics().get("component"));
    assertEquals("dbserver.nerd.us", spanEvent.getIntrinsics().get("peer.hostname"));
    assertEquals("dbserver.nerd.us:9945", spanEvent.getIntrinsics().get("peer.address"));
    assertEquals(2000, spanEvent.getIntrinsics().get("db.statement").toString().length());
    // Should not end with ... since it's exactly at the limit
    assertTrue(spanEvent.getIntrinsics().get("db.statement").toString().endsWith("aaa"));
    assertEquals("books", spanEvent.getIntrinsics().get("db.collection"));
    assertEquals("client", spanEvent.getIntrinsics().get("span.kind"));
    assertEquals("Datastore/statement/MySQL/books/select", spanEvent.getName());
    assertNotNull(spanEvent.getTraceId());
    assertNotNull(spanEvent.getGuid());
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) SpanEventsServiceImpl(com.newrelic.agent.service.analytics.SpanEventsServiceImpl) InetSocketAddress(java.net.InetSocketAddress) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) Connection(java.sql.Connection) DummyConnection(sql.DummyConnection) SpanEvent(com.newrelic.agent.model.SpanEvent) DummyResultSet(sql.DummyResultSet) Test(org.junit.Test)

Example 2 with DummyResultSet

use of sql.DummyResultSet in project newrelic-java-agent by newrelic.

the class DefaultSqlTracerTest method testSpanEventDatastoreTruncation.

@Test
public void testSpanEventDatastoreTruncation() throws SQLException {
    final Connection connection = mock(Connection.class);
    DatastoreInstanceDetection.detectConnectionAddress();
    DatastoreInstanceDetection.associateAddress(connection, new InetSocketAddress("dbserver.nerd.us", 9945));
    DatastoreInstanceDetection.stopDetectingConnectionAddress();
    String longQueryString = "SELECT price, name FROM BOOKS WHERE name = " + Strings.repeat("a", 4000);
    DefaultSqlTracer tracer = newInstanceDBTracer(longQueryString, connection, "MySQL", "mysql");
    tracer.finish(Opcodes.ARETURN, new DummyResultSet());
    SpanEventsService spanEventService = ServiceFactory.getSpanEventService();
    ((SpanEventsServiceImpl) spanEventService).dispatcherTransactionFinished(new TransactionData(tracer.getTransaction(), 1024), new TransactionStats());
    SamplingPriorityQueue<SpanEvent> eventPool = spanEventService.getOrCreateDistributedSamplingReservoir(APP_NAME);
    List<SpanEvent> spanEvents = eventPool.asList();
    assertNotNull(spanEvents);
    assertEquals(1, spanEvents.size());
    SpanEvent spanEvent = Iterables.getFirst(spanEvents, null);
    assertNotNull(spanEvent);
    assertEquals("datastore", spanEvent.getIntrinsics().get("category"));
    assertEquals("MySQL", spanEvent.getIntrinsics().get("component"));
    assertEquals("dbserver.nerd.us", spanEvent.getIntrinsics().get("peer.hostname"));
    assertEquals("dbserver.nerd.us:9945", spanEvent.getIntrinsics().get("peer.address"));
    assertEquals(2000, spanEvent.getIntrinsics().get("db.statement").toString().length());
    assertTrue(spanEvent.getIntrinsics().get("db.statement").toString().endsWith("a..."));
    assertEquals("books", spanEvent.getIntrinsics().get("db.collection"));
    assertEquals("client", spanEvent.getIntrinsics().get("span.kind"));
    assertEquals("Datastore/statement/MySQL/books/select", spanEvent.getName());
    assertNotNull(spanEvent.getTraceId());
    assertNotNull(spanEvent.getGuid());
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) SpanEventsServiceImpl(com.newrelic.agent.service.analytics.SpanEventsServiceImpl) InetSocketAddress(java.net.InetSocketAddress) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) Connection(java.sql.Connection) DummyConnection(sql.DummyConnection) SpanEvent(com.newrelic.agent.model.SpanEvent) DummyResultSet(sql.DummyResultSet) Test(org.junit.Test)

Example 3 with DummyResultSet

use of sql.DummyResultSet 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 4 with DummyResultSet

use of sql.DummyResultSet 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)

Example 5 with DummyResultSet

use of sql.DummyResultSet 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

Test (org.junit.Test)10 DummyResultSet (sql.DummyResultSet)10 Connection (java.sql.Connection)9 InetSocketAddress (java.net.InetSocketAddress)6 DummyConnection (sql.DummyConnection)6 Transaction (com.newrelic.agent.Transaction)3 ConnectionFactory (com.newrelic.agent.bridge.datastore.ConnectionFactory)3 SpanEvent (com.newrelic.agent.model.SpanEvent)3 SpanEventsService (com.newrelic.agent.service.analytics.SpanEventsService)3 SpanEventsServiceImpl (com.newrelic.agent.service.analytics.SpanEventsServiceImpl)3 TransactionStats (com.newrelic.agent.stats.TransactionStats)3 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)3 SimpleMetricNameFormat (com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat)3 DatabaseMetaData (java.sql.DatabaseMetaData)1 HashMap (java.util.HashMap)1