Search in sources :

Example 86 with MockRPMService

use of com.newrelic.agent.MockRPMService in project newrelic-java-agent by newrelic.

the class TransactionTraceServiceTest method transactionSamplersWithSynthetics.

@Test
public void transactionSamplersWithSynthetics() throws Exception {
    Map<String, Object> configMap = createConfigMap();
    Map<String, Object> ttConfigMap = new HashMap<>();
    configMap.put("transaction_tracer", ttConfigMap);
    ttConfigMap.put(TransactionTracerConfigImpl.ENABLED, true);
    ttConfigMap.put(TransactionTracerConfigImpl.COLLECT_TRACES, true);
    ttConfigMap.put(TransactionTracerConfigImpl.TRANSACTION_THRESHOLD, 1000f);
    createServiceManager(configMap);
    TransactionTraceService ttService = ServiceFactory.getTransactionTraceService();
    // This sampler only saves traces with "baseball" in the tx name
    ITransactionSampler transactionSampler = new ITransactionSampler() {

        private final List<TransactionData> tds = new ArrayList<>();

        @Override
        public boolean noticeTransaction(TransactionData td) {
            if (td.getPriorityTransactionName().getName().contains("baseball")) {
                tds.add(td);
                return true;
            }
            return false;
        }

        @Override
        public List<TransactionTrace> harvest(String appName) {
            List<TransactionTrace> traces = new ArrayList<>();
            for (TransactionData td : tds) {
                TransactionTrace trace = TransactionTrace.getTransactionTrace(td);
                traces.add(trace);
            }
            return traces;
        }

        @Override
        public void stop() {
            tds.clear();
        }
    };
    // This sampler only saves traces with "football" in the tx name
    ITransactionSampler transactionSampler2 = new ITransactionSampler() {

        private final List<TransactionData> tds = new ArrayList<>();

        @Override
        public boolean noticeTransaction(TransactionData td) {
            // football tx's only
            if (td.getPriorityTransactionName().getName().contains("football")) {
                tds.add(td);
                return true;
            }
            return false;
        }

        @Override
        public List<TransactionTrace> harvest(String appName) {
            List<TransactionTrace> traces = new ArrayList<>();
            for (TransactionData td : tds) {
                TransactionTrace trace = TransactionTrace.getTransactionTrace(td);
                traces.add(trace);
            }
            return traces;
        }

        @Override
        public void stop() {
            tds.clear();
        }
    };
    // This sampler saves the longest trace it sees
    ITransactionSampler transactionSampler3 = new ITransactionSampler() {

        private final List<TransactionData> tds = new ArrayList<>();

        @Override
        public boolean noticeTransaction(TransactionData td) {
            if (tds.size() == 0) {
                tds.add(td);
                return true;
            } else if (td.getLegacyDuration() > tds.get(0).getLegacyDuration()) {
                tds.clear();
                tds.add(td);
            }
            return false;
        }

        @Override
        public List<TransactionTrace> harvest(String appName) {
            List<TransactionTrace> traces = new ArrayList<>();
            for (TransactionData td : tds) {
                TransactionTrace trace = TransactionTrace.getTransactionTrace(td);
                traces.add(trace);
            }
            return traces;
        }

        @Override
        public void stop() {
            tds.clear();
        }
    };
    ttService.addTransactionTraceSampler(transactionSampler);
    ttService.addTransactionTraceSampler(transactionSampler2);
    ttService.addTransactionTraceSampler(transactionSampler3);
    addTx("haggis-eating", 100L, false);
    addTx("baseball", 100L, false);
    addTx("baseball", 101L, true);
    addTx("football", 100L, false);
    addTx("football", 101L, true);
    addTx("cricket", 100L, false);
    addTx("cricket", 101L, true);
    addTx("curling", 1500L, false);
    addTx("curling", 2501L, true);
    ttService.afterHarvest(APP_NAME);
    MockRPMService rpmService = (MockRPMService) ServiceFactory.getRPMServiceManager().getOrCreateRPMService(APP_NAME);
    List<TransactionTrace> traces = rpmService.getTraces();
    System.out.println(traces);
    Assert.assertEquals(8, traces.size());
    // The synthetics sampler should have picked up all the traces with lengths ending in a 1
    int nSynthetic = 0;
    for (TransactionTrace tt : traces) {
        if (tt.getDuration() % 10 == 1) {
            nSynthetic++;
        }
    }
    Assert.assertEquals(4, nSynthetic);
    // And other samplers should have captured everything but (cricket, 100)
    for (TransactionTrace tt : traces) {
        if (tt.getRequestUri().contains("cricket") && tt.getDuration() == 100L) {
            Assert.fail("samplers not behaving as expected.");
        }
    }
    traces.clear();
    traces = rpmService.getTraces();
    Assert.assertEquals(0, traces.size());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TransactionData(com.newrelic.agent.TransactionData) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Example 87 with MockRPMService

use of com.newrelic.agent.MockRPMService in project newrelic-java-agent by newrelic.

the class TransactionTraceServiceTest method testKeyTransactionSampler.

@Test
public void testKeyTransactionSampler() throws Exception {
    final String keyTransactionName = "/a/key/transaction";
    final String normalTransactionName = "/a/normal/transaction";
    Map<String, Object> configMap = createConfigMap();
    Map<String, Object> ttConfigMap = new HashMap<>();
    configMap.put(AgentConfigImpl.ENABLE_AUTO_APP_NAMING, false);
    configMap.put("transaction_tracer", ttConfigMap);
    ttConfigMap.put(TransactionTracerConfigImpl.ENABLED, true);
    ttConfigMap.put(TransactionTracerConfigImpl.COLLECT_TRACES, true);
    ttConfigMap.put(TransactionTracerConfigImpl.TRANSACTION_THRESHOLD, 100L);
    Map<String, Object> ktConfig = new HashMap<>();
    ktConfig.put(keyTransactionName, 0.04);
    configMap.put(AgentConfigImpl.KEY_TRANSACTIONS, ktConfig);
    createServiceManager(configMap);
    removeRandomTTSamplers();
    TransactionTraceService ttService = ServiceFactory.getTransactionTraceService();
    TransactionData td = createTransactionData(keyTransactionName, "App Name", 50);
    rpmServiceManager.getOrCreateRPMService("App Name");
    Assert.assertTrue(td.getAgentConfig().isApdexTSet(keyTransactionName));
    Assert.assertEquals(40, td.getAgentConfig().getApdexTInMillis(keyTransactionName));
    ttService.dispatcherTransactionFinished(td, null);
    TransactionData td2 = createTransactionData(normalTransactionName, "App Name", 50);
    Assert.assertFalse(td2.getAgentConfig().isApdexTSet(normalTransactionName));
    ttService.dispatcherTransactionFinished(td2, null);
    ttService.afterHarvest("App Name");
    MockRPMService rpmService = (MockRPMService) ServiceFactory.getRPMServiceManager().getRPMService("App Name");
    Assert.assertEquals(1, rpmService.getTraces().size());
    Assert.assertEquals(keyTransactionName, rpmService.getTraces().get(0).getRequestUri());
    Assert.assertEquals(50L, rpmService.getTraces().get(0).getDuration());
}
Also used : HashMap(java.util.HashMap) TransactionData(com.newrelic.agent.TransactionData) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Example 88 with MockRPMService

use of com.newrelic.agent.MockRPMService in project newrelic-java-agent by newrelic.

the class BasicRequestDispatcherTracerTest method requestXStartHeaderRecordApdexMetrics.

@Test
public void requestXStartHeaderRecordApdexMetrics() throws Exception {
    MockRPMServiceManager rpmServiceManager = (MockRPMServiceManager) ServiceFactory.getRPMServiceManager();
    MockRPMService rpmService = (MockRPMService) rpmServiceManager.getRPMService();
    rpmService.setEverConnected(true);
    Map<String, Object> data = new HashMap<>();
    data.put(AgentConfigImpl.APDEX_T, 6.0d);
    ConnectionConfigListener connectionConfigListener = rpmServiceManager.getConnectionConfigListener();
    connectionConfigListener.connected(rpmService, data);
    MockHttpRequest httpRequest = new MockHttpRequest();
    httpRequest.setHeader(QueueTimeTracker.REQUEST_X_START_HEADER, "server1 t=" + (Transaction.getTransaction().getWallClockStartTimeMs() - 24005));
    WebRequestDispatcher dispatcher = createDispatcher(httpRequest);
    dispatcher.getTransaction().getRootTracer().finish(0, null);
    StatsEngine statsEngine = ServiceFactory.getStatsService().getStatsEngineForHarvest(APP_NAME);
    ApdexStats apdexStats = statsEngine.getApdexStats(MetricName.create(MetricNames.APDEX));
    Assert.assertEquals(1, apdexStats.getApdexFrustrating());
    apdexStats = statsEngine.getApdexStats(MetricName.create("Apdex/Uri/Unknown"));
    Assert.assertEquals(1, apdexStats.getApdexFrustrating());
}
Also used : HashMap(java.util.HashMap) ConnectionConfigListener(com.newrelic.agent.ConnectionConfigListener) ApdexStats(com.newrelic.agent.stats.ApdexStats) MockRPMServiceManager(com.newrelic.agent.MockRPMServiceManager) WebRequestDispatcher(com.newrelic.agent.dispatchers.WebRequestDispatcher) StatsEngine(com.newrelic.agent.stats.StatsEngine) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Example 89 with MockRPMService

use of com.newrelic.agent.MockRPMService in project newrelic-java-agent by newrelic.

the class BoundedConcurrentCacheTest method createServiceManager.

private MockServiceManager createServiceManager(Map<String, Object> configMap) throws Exception {
    AgentConfig config = AgentConfigFactory.createAgentConfig(configMap, null, null);
    MockServiceManager serviceManager = new MockServiceManager();
    ServiceFactory.setServiceManager(serviceManager);
    ConfigService configService = ConfigServiceFactory.createConfigService(config, configMap);
    serviceManager.setConfigService(configService);
    ThreadService threadService = new ThreadService();
    serviceManager.setThreadService(threadService);
    HarvestService harvestService = new MockHarvestService();
    serviceManager.setHarvestService(harvestService);
    TransactionService transactionService = new TransactionService();
    serviceManager.setTransactionService(transactionService);
    StatsService statsService = new StatsServiceImpl();
    serviceManager.setStatsService(statsService);
    DatabaseService dbService = new DatabaseService();
    serviceManager.setDatabaseService(dbService);
    SqlTraceService sqlTraceService = new SqlTraceServiceImpl();
    serviceManager.setSqlTraceService(sqlTraceService);
    MockCoreService agent = new MockCoreService();
    serviceManager.setCoreService(agent);
    TransactionTraceService transactionTraceService = new TransactionTraceService();
    serviceManager.setTransactionTraceService(transactionTraceService);
    MockRPMServiceManager rpmServiceManager = new MockRPMServiceManager();
    serviceManager.setRPMServiceManager(rpmServiceManager);
    MockRPMService rpmService = new MockRPMService();
    rpmService.setApplicationName(APP_NAME);
    rpmService.setEverConnected(true);
    rpmService.setErrorService(new ErrorServiceImpl(APP_NAME));
    rpmServiceManager.setRPMService(rpmService);
    configService.start();
    serviceManager.start();
    sqlTraceService.start();
    return serviceManager;
}
Also used : MockHarvestService(com.newrelic.agent.MockHarvestService) HarvestService(com.newrelic.agent.HarvestService) TransactionService(com.newrelic.agent.TransactionService) ErrorServiceImpl(com.newrelic.agent.errors.ErrorServiceImpl) StatsService(com.newrelic.agent.stats.StatsService) MockRPMServiceManager(com.newrelic.agent.MockRPMServiceManager) DatabaseService(com.newrelic.agent.database.DatabaseService) TransactionTraceService(com.newrelic.agent.trace.TransactionTraceService) AgentConfig(com.newrelic.agent.config.AgentConfig) ThreadService(com.newrelic.agent.ThreadService) ConfigService(com.newrelic.agent.config.ConfigService) StatsServiceImpl(com.newrelic.agent.stats.StatsServiceImpl) MockServiceManager(com.newrelic.agent.MockServiceManager) MockHarvestService(com.newrelic.agent.MockHarvestService) MockCoreService(com.newrelic.agent.MockCoreService) MockRPMService(com.newrelic.agent.MockRPMService)

Example 90 with MockRPMService

use of com.newrelic.agent.MockRPMService in project newrelic-java-agent by newrelic.

the class SlowQueryAggregatorImplTest method createServiceManager.

private MockServiceManager createServiceManager(Map<String, Object> configMap) throws Exception {
    AgentConfig config = AgentConfigFactory.createAgentConfig(configMap, null, null);
    MockServiceManager serviceManager = new MockServiceManager();
    ServiceFactory.setServiceManager(serviceManager);
    ConfigService configService = ConfigServiceFactory.createConfigService(config, configMap);
    serviceManager.setConfigService(configService);
    ThreadService threadService = new ThreadService();
    serviceManager.setThreadService(threadService);
    HarvestService harvestService = new MockHarvestService();
    serviceManager.setHarvestService(harvestService);
    TransactionService transactionService = new TransactionService();
    serviceManager.setTransactionService(transactionService);
    StatsService statsService = new StatsServiceImpl();
    serviceManager.setStatsService(statsService);
    DatabaseService dbService = new DatabaseService();
    serviceManager.setDatabaseService(dbService);
    SqlTraceService sqlTraceService = new SqlTraceServiceImpl();
    serviceManager.setSqlTraceService(sqlTraceService);
    MockCoreService agent = new MockCoreService();
    serviceManager.setCoreService(agent);
    TransactionTraceService transactionTraceService = new TransactionTraceService();
    serviceManager.setTransactionTraceService(transactionTraceService);
    serviceManager.setDistributedTraceService(new DistributedTraceServiceImpl());
    MockRPMServiceManager rpmServiceManager = new MockRPMServiceManager();
    serviceManager.setRPMServiceManager(rpmServiceManager);
    MockRPMService rpmService = new MockRPMService();
    rpmService.setApplicationName(APP_NAME);
    rpmService.setEverConnected(true);
    rpmService.setErrorService(new ErrorServiceImpl(APP_NAME));
    rpmServiceManager.setRPMService(rpmService);
    configService.start();
    serviceManager.start();
    sqlTraceService.start();
    sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
    serviceManager.setAttributesService(new AttributesService());
    return serviceManager;
}
Also used : MockHarvestService(com.newrelic.agent.MockHarvestService) HarvestService(com.newrelic.agent.HarvestService) TransactionService(com.newrelic.agent.TransactionService) ErrorServiceImpl(com.newrelic.agent.errors.ErrorServiceImpl) StatsService(com.newrelic.agent.stats.StatsService) DistributedTraceServiceImpl(com.newrelic.agent.tracing.DistributedTraceServiceImpl) AttributesService(com.newrelic.agent.attributes.AttributesService) MockRPMServiceManager(com.newrelic.agent.MockRPMServiceManager) DatabaseService(com.newrelic.agent.database.DatabaseService) TransactionTraceService(com.newrelic.agent.trace.TransactionTraceService) AgentConfig(com.newrelic.agent.config.AgentConfig) ThreadService(com.newrelic.agent.ThreadService) ConfigService(com.newrelic.agent.config.ConfigService) StatsServiceImpl(com.newrelic.agent.stats.StatsServiceImpl) MockServiceManager(com.newrelic.agent.MockServiceManager) MockHarvestService(com.newrelic.agent.MockHarvestService) MockCoreService(com.newrelic.agent.MockCoreService) MockRPMService(com.newrelic.agent.MockRPMService)

Aggregations

MockRPMService (com.newrelic.agent.MockRPMService)97 Test (org.junit.Test)59 MockRPMServiceManager (com.newrelic.agent.MockRPMServiceManager)44 MockServiceManager (com.newrelic.agent.MockServiceManager)39 MockHarvestService (com.newrelic.agent.MockHarvestService)30 HashMap (java.util.HashMap)28 TransactionService (com.newrelic.agent.TransactionService)26 ConfigService (com.newrelic.agent.config.ConfigService)21 MockCoreService (com.newrelic.agent.MockCoreService)20 StatsEngineImpl (com.newrelic.agent.stats.StatsEngineImpl)18 HarvestService (com.newrelic.agent.HarvestService)17 ErrorServiceImpl (com.newrelic.agent.errors.ErrorServiceImpl)17 ThreadService (com.newrelic.agent.ThreadService)15 StatsService (com.newrelic.agent.stats.StatsService)15 TransactionTraceService (com.newrelic.agent.trace.TransactionTraceService)15 OtherRootSqlTracer (com.newrelic.agent.tracers.OtherRootSqlTracer)15 SqlTracer (com.newrelic.agent.tracers.SqlTracer)15 Tracer (com.newrelic.agent.tracers.Tracer)15 BasicRequestRootTracer (com.newrelic.agent.tracers.servlet.BasicRequestRootTracer)15 TransactionData (com.newrelic.agent.TransactionData)14