Search in sources :

Example 1 with StatsEngineImpl

use of com.newrelic.agent.stats.StatsEngineImpl in project newrelic-java-agent by newrelic.

the class SqlTraceServiceTest method multipleHarvests.

@Test
public void multipleHarvests() throws Exception {
    Map<String, Object> configMap = createStagingMap();
    createServiceManager(configMap);
    SqlTraceService sqlTraceService = ServiceFactory.getSqlTraceService();
    Assert.assertTrue(sqlTraceService.isEnabled());
    // run a transaction
    Tracer requestDispatcherTracer = startDispatcherTracer();
    long duration = TimeUnit.NANOSECONDS.convert(getExplainThresholdInMillis() + 1, TimeUnit.MILLISECONDS);
    startSqlTracer("select * from dude where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
    startSqlTracer("select * from dude where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
    startSqlTracer("select * from dudette where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
    requestDispatcherTracer.finish(Opcodes.RETURN, null);
    // run a harvest
    MockHarvestService mockharvestService = (MockHarvestService) ServiceFactory.getHarvestService();
    mockharvestService.runHarvest(ServiceFactory.getConfigService().getDefaultAgentConfig().getApplicationName(), new StatsEngineImpl());
    MockRPMService mockRPMService = (MockRPMService) ServiceFactory.getRPMService();
    List<SqlTrace> sqlTraces = mockRPMService.getSqlTraces();
    // verify results
    Assert.assertEquals(2, sqlTraces.size());
    // run a transaction
    requestDispatcherTracer = startDispatcherTracer();
    startSqlTracer("select * from dude where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
    startSqlTracer("select * from dude where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
    startSqlTracer("select * from dudette where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
    requestDispatcherTracer.finish(Opcodes.RETURN, null);
    // run a harvest
    mockharvestService.runHarvest(ServiceFactory.getConfigService().getDefaultAgentConfig().getApplicationName(), new StatsEngineImpl());
    sqlTraces = mockRPMService.getSqlTraces();
    // verify results
    Assert.assertEquals(2, sqlTraces.size());
    long expectedDuration = TimeUnit.MILLISECONDS.convert(duration, TimeUnit.NANOSECONDS);
    String expectedSql = "select * from dude where somevalue = ?";
    SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
    long expectedId = sqlObfuscator.obfuscateSql(expectedSql).hashCode();
    SqlTrace sqlTrace = getSqlTrace(expectedId, sqlTraces);
    Assert.assertNotNull(sqlTrace);
    Assert.assertEquals(expectedId, sqlTrace.getId());
    Assert.assertEquals(expectedSql, sqlTrace.getQuery());
    Assert.assertEquals(2, sqlTrace.getCallCount());
    Assert.assertEquals(expectedDuration * 2, sqlTrace.getTotal());
    Assert.assertEquals(expectedDuration, sqlTrace.getMin());
    Assert.assertEquals(expectedDuration, sqlTrace.getMax());
    expectedSql = "select * from dudette where somevalue = ?";
    expectedId = sqlObfuscator.obfuscateSql(expectedSql).hashCode();
    sqlTrace = getSqlTrace(expectedId, sqlTraces);
    Assert.assertNotNull(sqlTrace);
    Assert.assertEquals(expectedId, sqlTrace.getId());
    Assert.assertEquals(expectedSql, sqlTrace.getQuery());
    Assert.assertEquals(1, sqlTrace.getCallCount());
    Assert.assertEquals(expectedDuration, sqlTrace.getTotal());
    Assert.assertEquals(expectedDuration, sqlTrace.getMin());
    Assert.assertEquals(expectedDuration, sqlTrace.getMax());
}
Also used : StatsEngineImpl(com.newrelic.agent.stats.StatsEngineImpl) SqlTracer(com.newrelic.agent.tracers.SqlTracer) OtherRootSqlTracer(com.newrelic.agent.tracers.OtherRootSqlTracer) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) Tracer(com.newrelic.agent.tracers.Tracer) MockHarvestService(com.newrelic.agent.MockHarvestService) SqlObfuscator(com.newrelic.agent.database.SqlObfuscator) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Example 2 with StatsEngineImpl

use of com.newrelic.agent.stats.StatsEngineImpl in project newrelic-java-agent by newrelic.

the class SqlTraceServiceTest method insertSqlMaxLength.

@Test
public void insertSqlMaxLength() throws Exception {
    Map<String, Object> configMap = createStagingMap();
    createServiceManager(configMap);
    // run a transaction
    Tracer requestDispatcherTracer = startDispatcherTracer();
    long duration = TimeUnit.NANOSECONDS.convert(getExplainThresholdInMillis() + 1, TimeUnit.MILLISECONDS);
    char[] charData = new char[2100];
    String sql = "select * from dude where  where somevalue = 'cool'";
    for (int i = 0; i < sql.length(); i++) {
        charData[i] = sql.charAt(i);
    }
    for (int i = sql.length(); i < charData.length; i++) {
        charData[i] = 'a';
    }
    sql = String.valueOf(charData);
    startSqlTracer(sql, duration).finish(Opcodes.RETURN, null);
    requestDispatcherTracer.finish(Opcodes.RETURN, null);
    // run a harvest
    MockHarvestService mockharvestService = (MockHarvestService) ServiceFactory.getHarvestService();
    mockharvestService.runHarvest(ServiceFactory.getConfigService().getDefaultAgentConfig().getApplicationName(), new StatsEngineImpl());
    MockRPMService mockRPMService = (MockRPMService) ServiceFactory.getRPMService();
    List<SqlTrace> sqlTraces = mockRPMService.getSqlTraces();
    Assert.assertEquals(1, sqlTraces.size());
    SqlTrace sqlTrace = sqlTraces.get(0);
    int maxSqlLength = ServiceFactory.getConfigService().getDefaultAgentConfig().getTransactionTracerConfig().getInsertSqlMaxLength();
    SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
    sql = sqlObfuscator.obfuscateSql(sql);
    Assert.assertEquals(TransactionSegment.truncateSql(sql, maxSqlLength).length(), sqlTrace.getQuery().length());
}
Also used : StatsEngineImpl(com.newrelic.agent.stats.StatsEngineImpl) SqlTracer(com.newrelic.agent.tracers.SqlTracer) OtherRootSqlTracer(com.newrelic.agent.tracers.OtherRootSqlTracer) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) Tracer(com.newrelic.agent.tracers.Tracer) MockHarvestService(com.newrelic.agent.MockHarvestService) SqlObfuscator(com.newrelic.agent.database.SqlObfuscator) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Example 3 with StatsEngineImpl

use of com.newrelic.agent.stats.StatsEngineImpl in project newrelic-java-agent by newrelic.

the class SqlTraceServiceTest method serializeSqlTrace.

@Test
public void serializeSqlTrace() throws Exception {
    Map<String, Object> configMap = createStagingMap();
    createServiceManager(configMap);
    // run a transaction
    Tracer requestDispatcherTracer = startDispatcherTracer();
    long duration = TimeUnit.NANOSECONDS.convert(getExplainThresholdInMillis() + 1, TimeUnit.MILLISECONDS);
    startSqlTracer("select * from dude where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
    requestDispatcherTracer.finish(Opcodes.RETURN, null);
    // run a harvest
    MockHarvestService mockharvestService = (MockHarvestService) ServiceFactory.getHarvestService();
    mockharvestService.runHarvest(ServiceFactory.getConfigService().getDefaultAgentConfig().getApplicationName(), new StatsEngineImpl());
    MockRPMService mockRPMService = (MockRPMService) ServiceFactory.getRPMService();
    List<SqlTrace> sqlTraces = mockRPMService.getSqlTraces();
    Assert.assertEquals(1, sqlTraces.size());
    SqlTrace sqlTrace = sqlTraces.get(0);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    Writer writer = new OutputStreamWriter(os);
    JSONValue.writeJSONString(sqlTraces, writer);
    writer.close();
    String json = os.toString();
    JSONParser parser = new JSONParser();
    JSONArray deserializedSqlTraces = (JSONArray) parser.parse(json);
    Assert.assertEquals(1, deserializedSqlTraces.size());
    JSONArray deserializedSqlTrace = (JSONArray) deserializedSqlTraces.get(0);
    Assert.assertEquals(10, deserializedSqlTrace.size());
    Assert.assertEquals(sqlTrace.getBlameMetricName(), deserializedSqlTrace.get(0));
    Assert.assertEquals(sqlTrace.getUri(), deserializedSqlTrace.get(1));
    Assert.assertEquals(sqlTrace.getId(), deserializedSqlTrace.get(2));
    Assert.assertEquals(sqlTrace.getQuery(), deserializedSqlTrace.get(3));
    Assert.assertEquals(sqlTrace.getMetricName(), deserializedSqlTrace.get(4));
    Assert.assertEquals(Integer.valueOf(sqlTrace.getCallCount()).longValue(), deserializedSqlTrace.get(5));
    Assert.assertEquals(sqlTrace.getTotal(), deserializedSqlTrace.get(6));
    Assert.assertEquals(sqlTrace.getMin(), deserializedSqlTrace.get(7));
    Assert.assertEquals(sqlTrace.getMax(), deserializedSqlTrace.get(8));
    Map<String, Object> decodedParams = (Map<String, Object>) decodeParams(deserializedSqlTrace.get(9));
    Map<String, Object> sqlTraceParams = sqlTrace.getParameters();
    Double decodedPriority = (Double) decodedParams.remove("priority");
    Float sqlTracePriority = (Float) sqlTraceParams.remove("priority");
    Assert.assertEquals(decodedParams, sqlTraceParams);
    Assert.assertEquals(decodedPriority, sqlTracePriority, 0.0000001f);
}
Also used : StatsEngineImpl(com.newrelic.agent.stats.StatsEngineImpl) SqlTracer(com.newrelic.agent.tracers.SqlTracer) OtherRootSqlTracer(com.newrelic.agent.tracers.OtherRootSqlTracer) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) Tracer(com.newrelic.agent.tracers.Tracer) JSONArray(org.json.simple.JSONArray) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MockHarvestService(com.newrelic.agent.MockHarvestService) OutputStreamWriter(java.io.OutputStreamWriter) JSONParser(org.json.simple.parser.JSONParser) Map(java.util.Map) HashMap(java.util.HashMap) MockRPMService(com.newrelic.agent.MockRPMService) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) Test(org.junit.Test)

Example 4 with StatsEngineImpl

use of com.newrelic.agent.stats.StatsEngineImpl in project newrelic-java-agent by newrelic.

the class SqlTraceServiceTest method multipleTransactions.

@Test
public void multipleTransactions() throws Exception {
    Map<String, Object> configMap = createStagingMap();
    createServiceManager(configMap);
    // run a transaction
    Tracer requestDispatcherTracer = startDispatcherTracer();
    long duration = TimeUnit.NANOSECONDS.convert(getExplainThresholdInMillis() + 1, TimeUnit.MILLISECONDS);
    startSqlTracer("select * from dude where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
    startSqlTracer("select * from dude where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
    startSqlTracer("select * from dudette where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
    requestDispatcherTracer.finish(Opcodes.RETURN, null);
    // run another transaction
    requestDispatcherTracer = startDispatcherTracer();
    long duration2 = TimeUnit.NANOSECONDS.convert(getExplainThresholdInMillis() + 1, TimeUnit.MILLISECONDS);
    startSqlTracer("select * from dude where somevalue = 'cool'", duration2).finish(Opcodes.RETURN, null);
    startSqlTracer("select * from dude where somevalue = 'cool'", duration2).finish(Opcodes.RETURN, null);
    startSqlTracer("select * from dudette where somevalue = 'cool'", duration2).finish(Opcodes.RETURN, null);
    startSqlTracer("select * from duderino where somevalue = 'cool'", duration2).finish(Opcodes.RETURN, null);
    requestDispatcherTracer.finish(Opcodes.RETURN, null);
    // run a harvest
    MockHarvestService mockharvestService = (MockHarvestService) ServiceFactory.getHarvestService();
    mockharvestService.runHarvest(ServiceFactory.getConfigService().getDefaultAgentConfig().getApplicationName(), new StatsEngineImpl());
    MockRPMService mockRPMService = (MockRPMService) ServiceFactory.getRPMService();
    List<SqlTrace> sqlTraces = mockRPMService.getSqlTraces();
    // verify results
    Assert.assertEquals(3, sqlTraces.size());
    long expectedDuration = TimeUnit.MILLISECONDS.convert(duration, TimeUnit.NANOSECONDS);
    long expectedDuration2 = TimeUnit.MILLISECONDS.convert(duration2, TimeUnit.NANOSECONDS);
    String expectedSql = "select * from dude where somevalue = ?";
    SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
    long expectedId = sqlObfuscator.obfuscateSql(expectedSql).hashCode();
    SqlTrace sqlTrace = getSqlTrace(expectedId, sqlTraces);
    Assert.assertNotNull(sqlTrace);
    Assert.assertEquals(expectedId, sqlTrace.getId());
    Assert.assertEquals(expectedSql, sqlTrace.getQuery());
    Assert.assertEquals(4, sqlTrace.getCallCount());
    Assert.assertEquals((expectedDuration * 2) + (expectedDuration2 * 2), sqlTrace.getTotal());
    Assert.assertEquals(expectedDuration, sqlTrace.getMin());
    Assert.assertEquals(expectedDuration2, sqlTrace.getMax());
    expectedSql = "select * from dudette where somevalue = ?";
    expectedId = sqlObfuscator.obfuscateSql(expectedSql).hashCode();
    sqlTrace = getSqlTrace(expectedId, sqlTraces);
    Assert.assertNotNull(sqlTrace);
    Assert.assertEquals(expectedId, sqlTrace.getId());
    Assert.assertEquals(expectedSql, sqlTrace.getQuery());
    Assert.assertEquals(2, sqlTrace.getCallCount());
    Assert.assertEquals(expectedDuration + expectedDuration2, sqlTrace.getTotal());
    Assert.assertEquals(expectedDuration, sqlTrace.getMin());
    Assert.assertEquals(expectedDuration2, sqlTrace.getMax());
    expectedSql = "select * from duderino where somevalue = ?";
    expectedId = sqlObfuscator.obfuscateSql(expectedSql).hashCode();
    sqlTrace = getSqlTrace(expectedId, sqlTraces);
    Assert.assertNotNull(sqlTrace);
    Assert.assertEquals(expectedId, sqlTrace.getId());
    Assert.assertEquals(expectedSql, sqlTrace.getQuery());
    Assert.assertEquals(1, sqlTrace.getCallCount());
    Assert.assertEquals(expectedDuration2, sqlTrace.getTotal());
    Assert.assertEquals(expectedDuration2, sqlTrace.getMin());
    Assert.assertEquals(expectedDuration2, sqlTrace.getMax());
}
Also used : StatsEngineImpl(com.newrelic.agent.stats.StatsEngineImpl) SqlTracer(com.newrelic.agent.tracers.SqlTracer) OtherRootSqlTracer(com.newrelic.agent.tracers.OtherRootSqlTracer) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) Tracer(com.newrelic.agent.tracers.Tracer) MockHarvestService(com.newrelic.agent.MockHarvestService) SqlObfuscator(com.newrelic.agent.database.SqlObfuscator) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Example 5 with StatsEngineImpl

use of com.newrelic.agent.stats.StatsEngineImpl in project newrelic-java-agent by newrelic.

the class SqlTraceServiceTest method multipleApps.

@Test
public void multipleApps() throws Exception {
    Map<String, Object> configMap = createStagingMap();
    createServiceManager(configMap);
    // run a transaction in "Test" app
    Tracer requestDispatcherTracer = startDispatcherTracer("Test");
    long duration = TimeUnit.NANOSECONDS.convert(getExplainThresholdInMillis() + 1, TimeUnit.MILLISECONDS);
    startSqlTracer("select * from dude where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
    startSqlTracer("select * from dude where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
    startSqlTracer("select * from dudette where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
    Transaction.getTransaction().setApplicationName(ApplicationNamePriority.REQUEST_ATTRIBUTE, "Test");
    requestDispatcherTracer.finish(Opcodes.RETURN, null);
    // run a transaction in "Test2" app
    requestDispatcherTracer = startDispatcherTracer("Test2");
    startSqlTracer("select * from dude2 where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
    startSqlTracer("select * from dude2 where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
    startSqlTracer("select * from dudette2 where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
    Transaction.getTransaction().setApplicationName(ApplicationNamePriority.REQUEST_ATTRIBUTE, "Test2");
    requestDispatcherTracer.finish(Opcodes.RETURN, null);
    // run a harvest for "Test"
    MockHarvestService mockharvestService = (MockHarvestService) ServiceFactory.getHarvestService();
    mockharvestService.runHarvest("Test", new StatsEngineImpl());
    MockRPMService mockRPMService = (MockRPMService) ServiceFactory.getRPMServiceManager().getOrCreateRPMService("Test");
    List<SqlTrace> sqlTraces = mockRPMService.getSqlTraces();
    // verify results
    Assert.assertEquals(2, sqlTraces.size());
    // run a harvest for "Test2"
    mockharvestService.runHarvest("Test2", new StatsEngineImpl());
    mockRPMService = (MockRPMService) ServiceFactory.getRPMServiceManager().getOrCreateRPMService("Test2");
    sqlTraces = mockRPMService.getSqlTraces();
    // verify results
    Assert.assertEquals(2, sqlTraces.size());
}
Also used : StatsEngineImpl(com.newrelic.agent.stats.StatsEngineImpl) SqlTracer(com.newrelic.agent.tracers.SqlTracer) OtherRootSqlTracer(com.newrelic.agent.tracers.OtherRootSqlTracer) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) Tracer(com.newrelic.agent.tracers.Tracer) MockHarvestService(com.newrelic.agent.MockHarvestService) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Aggregations

StatsEngineImpl (com.newrelic.agent.stats.StatsEngineImpl)53 Test (org.junit.Test)46 StatsEngine (com.newrelic.agent.stats.StatsEngine)31 HashMap (java.util.HashMap)30 JmxMetric (com.newrelic.agent.jmx.metrics.JmxMetric)20 MockRPMService (com.newrelic.agent.MockRPMService)18 MockHarvestService (com.newrelic.agent.MockHarvestService)13 OtherRootSqlTracer (com.newrelic.agent.tracers.OtherRootSqlTracer)12 SqlTracer (com.newrelic.agent.tracers.SqlTracer)12 Tracer (com.newrelic.agent.tracers.Tracer)12 BasicRequestRootTracer (com.newrelic.agent.tracers.servlet.BasicRequestRootTracer)12 Map (java.util.Map)9 JSONArray (org.json.simple.JSONArray)6 SqlObfuscator (com.newrelic.agent.database.SqlObfuscator)5 TransactionStats (com.newrelic.agent.stats.TransactionStats)5 ArrayList (java.util.ArrayList)5 MBeanServer (javax.management.MBeanServer)5 TransactionData (com.newrelic.agent.TransactionData)4 TransactionService (com.newrelic.agent.TransactionService)4 Attribute (javax.management.Attribute)4