Search in sources :

Example 6 with StatsEngineImpl

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

the class SqlTraceServiceTest method overSqlLimit.

@Test
public void overSqlLimit() throws Exception {
    Map<String, Object> configMap = createStagingMap();
    createServiceManager(configMap);
    int sqlLimit = SlowQueryAggregatorImpl.SLOW_QUERY_LIMIT_PER_REPORTING_PERIOD;
    // run a transaction
    Tracer requestDispatcherTracer = startDispatcherTracer();
    for (int i = 0; i < sqlLimit * 2; i++) {
        String sql = "select * from dude where column" + i + " = 'cool'";
        long duration = TimeUnit.NANOSECONDS.convert(getExplainThresholdInMillis() + 1 + i, TimeUnit.MILLISECONDS);
        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();
    // verify results
    Assert.assertEquals(sqlLimit, sqlTraces.size());
    SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
    for (int i = sqlLimit; i < sqlLimit * 2; i++) {
        String expectedSql = "select * from dude where column" + i + " = ?";
        long expectedId = sqlObfuscator.obfuscateSql(expectedSql).hashCode();
        SqlTrace sqlTrace = getSqlTrace(expectedId, sqlTraces);
        Assert.assertNotNull(sqlTrace);
        long expectedDuration = getExplainThresholdInMillis() + 1 + i;
        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 7 with StatsEngineImpl

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

the class SqlTraceServiceTest method transactionTracerNotEnabled.

@Test
public void transactionTracerNotEnabled() throws Exception {
    Map<String, Object> configMap = createStagingMap();
    Map<String, Object> ttConfigMap = new HashMap<>();
    configMap.put("transaction_tracer", ttConfigMap);
    ttConfigMap.put("enabled", false);
    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 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(0, sqlTraces.size());
}
Also used : StatsEngineImpl(com.newrelic.agent.stats.StatsEngineImpl) HashMap(java.util.HashMap) 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)

Example 8 with StatsEngineImpl

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

the class SqlTraceServiceTest method notOverExplainThreshold.

@Test
public void notOverExplainThreshold() throws Exception {
    Map<String, Object> configMap = createStagingMap();
    createServiceManager(configMap);
    // run a transaction
    Tracer requestDispatcherTracer = startDispatcherTracer();
    long duration = TimeUnit.NANOSECONDS.convert(getExplainThresholdInMillis(), 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(0, 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)

Example 9 with StatsEngineImpl

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

the class SqlTraceServiceTest method serverConfigChanges.

@Test
public void serverConfigChanges() throws Exception {
    Map<String, Object> configMap = createStagingMap();
    Map<String, Object> sqlMap = createMap();
    sqlMap.put("enabled", true);
    configMap.put(AgentConfigImpl.SLOW_SQL, sqlMap);
    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 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());
    Map<String, Object> serverData = createMap();
    Map<String, Object> agentMap = createMap();
    serverData.put(AgentConfigFactory.AGENT_CONFIG, agentMap);
    agentMap.put(AgentConfigFactory.SLOW_SQL_PREFIX + "enabled", false);
    MockRPMServiceManager rpmServiceManager = (MockRPMServiceManager) ServiceFactory.getRPMServiceManager();
    ConnectionConfigListener connectionConfigListener = rpmServiceManager.getConnectionConfigListener();
    connectionConfigListener.connected(mockRPMService, serverData);
    // 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(0, sqlTraces.size());
    serverData = createMap();
    agentMap = createMap();
    serverData.put(AgentConfigFactory.AGENT_CONFIG, agentMap);
    agentMap.put(AgentConfigFactory.SLOW_SQL_PREFIX + "enabled", true);
    connectionConfigListener.connected(mockRPMService, serverData);
    // 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());
}
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) ConnectionConfigListener(com.newrelic.agent.ConnectionConfigListener) MockHarvestService(com.newrelic.agent.MockHarvestService) MockRPMServiceManager(com.newrelic.agent.MockRPMServiceManager) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Example 10 with StatsEngineImpl

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

the class DistributedTraceServiceImplTest method testExponentialBackoff.

@Test
public void testExponentialBackoff() {
    rpmServiceManager.getOrCreateRPMService("Test");
    // Create reservoir
    ServiceFactory.getTransactionEventsService().harvestEvents("Test");
    DistributedSamplingPriorityQueue<TransactionEvent> reservoir = ServiceFactory.getTransactionEventsService().getOrCreateDistributedSamplingReservoir("Test");
    // First 10 traces
    for (int i = 0; i < 10; i++) {
        assertTrue(DistributedTraceUtil.isSampledPriority(distributedTraceService.calculatePriority(null, reservoir)));
        TransactionEvent transactionEvent = Mockito.mock(TransactionEvent.class);
        when(transactionEvent.getPriority()).thenReturn(1.0f);
        when(transactionEvent.decider()).thenReturn(true);
        reservoir.add(transactionEvent);
    }
    distributedTraceService.beforeHarvest("Test", new StatsEngineImpl());
    ServiceFactory.getTransactionEventsService().harvestEvents("Test");
    reservoir = ServiceFactory.getTransactionEventsService().getOrCreateDistributedSamplingReservoir("Test");
    assertEquals(0, reservoir.getSampled());
    // Test that we hit target
    for (int i = 0; i < reservoir.getTarget(); i++) {
        if (DistributedTraceUtil.isSampledPriority(distributedTraceService.calculatePriority(null, reservoir))) {
            TransactionEvent transactionEvent = Mockito.mock(TransactionEvent.class);
            when(transactionEvent.getPriority()).thenReturn(1.0f);
            when(transactionEvent.decider()).thenReturn(true);
            reservoir.add(transactionEvent);
        }
    }
    assertTrue("Sampled fewer than target transactions", reservoir.getSampled() >= reservoir.getTarget());
    ServiceFactory.getTransactionEventsService().harvestEvents("Test");
    distributedTraceService.beforeHarvest("Test", new StatsEngineImpl());
    reservoir = ServiceFactory.getTransactionEventsService().getOrCreateDistributedSamplingReservoir("Test");
    // Test that we do not go above 2x target
    for (int i = 0; i < 1000 * reservoir.getTarget(); i++) {
        if (DistributedTraceUtil.isSampledPriority(distributedTraceService.calculatePriority(null, reservoir))) {
            TransactionEvent transactionEvent = Mockito.mock(TransactionEvent.class);
            when(transactionEvent.getPriority()).thenReturn(1.0f);
            when(transactionEvent.decider()).thenReturn(true);
            reservoir.add(transactionEvent);
        }
    }
    assertTrue("Sampled fewer than target transactions", reservoir.getSampled() >= reservoir.getTarget());
    assertTrue("Sampled more than 2x target ", reservoir.getSampled() <= (2 * reservoir.getTarget()));
}
Also used : TransactionEvent(com.newrelic.agent.service.analytics.TransactionEvent) StatsEngineImpl(com.newrelic.agent.stats.StatsEngineImpl) 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