Search in sources :

Example 46 with MockRPMService

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

the class ErrorServiceTest method harvest.

@Test
public void harvest() throws Exception {
    Map<String, Object> config = createConfig("java.lang.Exception");
    EventTestHelper.createServiceManager(config);
    MockRPMService rpmService = (MockRPMService) ServiceFactory.getRPMService();
    rpmService.setIsConnected(false);
    ErrorServiceImpl errorService = (ErrorServiceImpl) ServiceFactory.getRPMService().getErrorService();
    ErrorCollectorConfig errorCollectorConfig = ServiceFactory.getConfigService().getDefaultAgentConfig().getErrorCollectorConfig();
    TracedError error = HttpTracedError.builder(errorCollectorConfig, APP_NAME, "dude", System.currentTimeMillis()).statusCodeAndMessage(403, null).requestUri("/dude").build();
    errorService.reportError(error);
    StatsService spy = spy(new StatsServiceImpl());
    StatsEngine statsEngine = spy.getStatsEngineForHarvest(APP_NAME);
    ((MockServiceManager) ServiceFactory.getServiceManager()).setStatsService(spy);
    List<TracedError> actualErrors = errorService.getAndClearTracedErrors(APP_NAME, statsEngine);
    Assert.assertEquals(0, actualErrors.size());
    Assert.assertEquals(1, statsEngine.getStats(MetricNames.ERRORS_ALL).getCallCount());
    rpmService.setIsConnected(true);
    actualErrors = errorService.getAndClearTracedErrors(APP_NAME, statsEngine);
    Assert.assertEquals(1, actualErrors.size());
    Assert.assertEquals(1, statsEngine.getStats(MetricNames.ERRORS_ALL).getCallCount());
}
Also used : StatsService(com.newrelic.agent.stats.StatsService) ErrorCollectorConfig(com.newrelic.agent.config.ErrorCollectorConfig) StatsServiceImpl(com.newrelic.agent.stats.StatsServiceImpl) MockServiceManager(com.newrelic.agent.MockServiceManager) StatsEngine(com.newrelic.agent.stats.StatsEngine) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Example 47 with MockRPMService

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

the class ErrorServiceTest method setupAndVerifyStripExceptionMessage.

private void setupAndVerifyStripExceptionMessage(ConfigEnhancer enhancer, Boolean highSecurity, Boolean stripException, String allowedExceptionClasses, boolean expectedToBeStripped, Throwable exception) throws Exception {
    Map<String, Object> config = createConfig(null, highSecurity, stripException, allowedExceptionClasses);
    enhancer.enhance(config, exception);
    EventTestHelper.createServiceManager(config);
    ErrorServiceImpl errorService = (ErrorServiceImpl) ServiceFactory.getRPMService().getErrorService();
    MockRPMService rpmService = (MockRPMService) ServiceFactory.getRPMService();
    rpmService.setIsConnected(true);
    ErrorCollectorConfig errorCollectorConfig = ServiceFactory.getConfigService().getErrorCollectorConfig(APP_NAME);
    TracedError error = ThrowableError.builder(errorCollectorConfig, APP_NAME, "dude", exception, System.currentTimeMillis()).errorMessageReplacer(new ErrorMessageReplacer(ServiceFactory.getConfigService().getStripExceptionConfig(APP_NAME))).build();
    errorService.reportError(error);
    // Checking ...
    StatsEngineImpl statsEngine = new StatsEngineImpl();
    List<TracedError> actualErrors = errorService.getAndClearTracedErrors(APP_NAME, statsEngine);
    if (enhancer.shouldMatch()) {
        // If we supplied a configuration that ignored the error,
        // check that it worked, and we're done here. This verifies
        // the fix for JAVA-2975.
        Assert.assertEquals(0, actualErrors.size());
        return;
    }
    Assert.assertEquals(1, actualErrors.size());
    TracedError tracedError = actualErrors.get(0);
    String expectedMessage = exception.getMessage();
    // - Strip Exceptions On
    if (expectedToBeStripped && (highSecurity != null && highSecurity)) {
        if (stripException == null || stripException) {
            expectedMessage = ErrorMessageReplacer.STRIPPED_EXCEPTION_REPLACEMENT;
        }
    } else if (expectedToBeStripped && (stripException != null && stripException)) {
        expectedMessage = ErrorMessageReplacer.STRIPPED_EXCEPTION_REPLACEMENT;
    }
    Assert.assertEquals("High Security = " + (highSecurity != null ? highSecurity.toString() : "Unset") + ", Strip Exceptions = " + (stripException != null ? stripException.toString() : "Unset") + ", Exceptions to be allowed unstripped = " + (allowedExceptionClasses != null ? allowedExceptionClasses : "Unset"), expectedMessage, tracedError.getMessage());
}
Also used : StatsEngineImpl(com.newrelic.agent.stats.StatsEngineImpl) ErrorCollectorConfig(com.newrelic.agent.config.ErrorCollectorConfig) MockRPMService(com.newrelic.agent.MockRPMService)

Example 48 with MockRPMService

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

the class ErrorServiceTest method errorCountMetrics.

@Test
public void errorCountMetrics() throws Exception {
    Map<String, Object> config = createConfig("java.lang.Exception");
    EventTestHelper.createServiceManager(config);
    MockRPMService rpmService = (MockRPMService) ServiceFactory.getRPMService();
    rpmService.setIsConnected(true);
    TransactionService txService = ServiceFactory.getTransactionService();
    Throwable error = new ArrayIndexOutOfBoundsException();
    TransactionData data = createTransactionData(true, 0, error, false);
    TransactionStats transactionStats = new TransactionStats();
    txService.transactionFinished(data, transactionStats);
    Assert.assertEquals(1, transactionStats.getUnscopedStats().getStats("Errors/WebTransaction/Uri/dude").getCallCount());
    Assert.assertEquals(1, transactionStats.getUnscopedStats().getStats(MetricNames.WEB_TRANSACTION_ERRORS_ALL).getCallCount());
    data = createTransactionData(true, 0, null, false);
    transactionStats = new TransactionStats();
    txService.transactionFinished(data, transactionStats);
    Assert.assertEquals(0, transactionStats.getUnscopedStats().getStats("Errors/WebTransaction/Uri/dude").getCallCount());
    Assert.assertEquals(0, transactionStats.getUnscopedStats().getStats(MetricNames.WEB_TRANSACTION_ERRORS_ALL).getCallCount());
    data = createTransactionData(false, 0, error, false);
    transactionStats = new TransactionStats();
    txService.transactionFinished(data, transactionStats);
    Assert.assertEquals(1, transactionStats.getUnscopedStats().getStats("Errors/OtherTransaction/Custom/dude").getCallCount());
    Assert.assertEquals(1, transactionStats.getUnscopedStats().getStats(MetricNames.OTHER_TRANSACTION_ERRORS_ALL).getCallCount());
    data = createTransactionData(false, 0, null, false);
    transactionStats = new TransactionStats();
    txService.transactionFinished(data, transactionStats);
    Assert.assertEquals(0, transactionStats.getUnscopedStats().getStats("Errors/OtherTransaction/Custom/dude").getCallCount());
    Assert.assertEquals(0, transactionStats.getUnscopedStats().getStats(MetricNames.WEB_TRANSACTION_ERRORS_ALL).getCallCount());
    StatsService spy = spy(new StatsServiceImpl());
    ((MockServiceManager) ServiceFactory.getServiceManager()).setStatsService(spy);
    ErrorServiceImpl errorService = (ErrorServiceImpl) ServiceFactory.getRPMService().getErrorService();
    StatsEngine statsEngine = spy.getStatsEngineForHarvest(APP_NAME);
    List<TracedError> actualErrors = errorService.getAndClearTracedErrors(APP_NAME, statsEngine);
    Assert.assertEquals(2, actualErrors.size());
    Assert.assertEquals(2, statsEngine.getStats(MetricNames.ERRORS_ALL).getCallCount());
    spy = spy(new StatsServiceImpl());
    ((MockServiceManager) ServiceFactory.getServiceManager()).setStatsService(spy);
    statsEngine = spy.getStatsEngineForHarvest(APP_NAME);
    actualErrors = errorService.getAndClearTracedErrors(APP_NAME, statsEngine);
    Assert.assertEquals(0, actualErrors.size());
    Assert.assertEquals(0, statsEngine.getStats(MetricNames.ERRORS_ALL).getCallCount());
    data = createTransactionData(true, 0, error, false);
    txService.transactionFinished(data, new TransactionStats());
    spy = spy(new StatsServiceImpl());
    ((MockServiceManager) ServiceFactory.getServiceManager()).setStatsService(spy);
    statsEngine = spy.getStatsEngineForHarvest(APP_NAME);
    actualErrors = errorService.getAndClearTracedErrors(APP_NAME, statsEngine);
    Assert.assertEquals(1, actualErrors.size());
    Assert.assertEquals(1, statsEngine.getStats(MetricNames.ERRORS_ALL).getCallCount());
}
Also used : TransactionService(com.newrelic.agent.TransactionService) StatsService(com.newrelic.agent.stats.StatsService) StatsEngine(com.newrelic.agent.stats.StatsEngine) TransactionStats(com.newrelic.agent.stats.TransactionStats) StatsServiceImpl(com.newrelic.agent.stats.StatsServiceImpl) MockServiceManager(com.newrelic.agent.MockServiceManager) TransactionData(com.newrelic.agent.TransactionData) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Example 49 with MockRPMService

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

the class ErrorServiceTest method testErrorEventFasterHarvest.

@Test
public void testErrorEventFasterHarvest() throws Exception {
    String appName = ServiceFactory.getRPMService().getApplicationName();
    ServiceManager serviceManager = spy(ServiceFactory.getServiceManager());
    ServiceFactory.setServiceManager(serviceManager);
    HarvestServiceImpl harvestService = spy(new HarvestServiceImpl());
    doReturn(harvestService).when(serviceManager).getHarvestService();
    doReturn(0L).when(harvestService).getInitialDelay();
    ErrorServiceImpl errorService = (ErrorServiceImpl) ServiceFactory.getRPMService().getErrorService();
    errorService.addHarvestableToService();
    errorService.harvestable.configure(60, 10);
    Assert.assertEquals(10, errorService.getMaxSamplesStored());
    Map<String, Object> connectionInfo = new HashMap<>();
    Map<String, Object> eventHarvest = new HashMap<>();
    Map<String, Object> harvestLimits = new HashMap<>();
    // 5 is the lowest allowable value
    eventHarvest.put("report_period_ms", 5000L);
    eventHarvest.put("harvest_limits", harvestLimits);
    harvestLimits.put("error_event_data", 100L);
    connectionInfo.put("event_harvest_config", eventHarvest);
    harvestService.startHarvestables(ServiceFactory.getRPMService(), AgentConfigImpl.createAgentConfig(connectionInfo));
    Thread.sleep(500);
    TracedError error = ThrowableError.builder(ServiceFactory.getConfigService().getErrorCollectorConfig(appName), appName, "metricName", new Throwable(), 100).requestUri("requestUri").errorAttributes(Collections.singletonMap("test_attribute", "value")).build();
    ServiceFactory.getRPMService().getErrorService().reportError(error);
    Thread.sleep(5050);
    checkForEvent();
    ((MockRPMService) ServiceFactory.getRPMService()).clearEvents();
    ServiceFactory.getRPMService().getErrorService().reportError(error);
    Thread.sleep(5050);
    checkForEvent();
}
Also used : HashMap(java.util.HashMap) MockServiceManager(com.newrelic.agent.MockServiceManager) ServiceManager(com.newrelic.agent.service.ServiceManager) HarvestServiceImpl(com.newrelic.agent.HarvestServiceImpl) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Example 50 with MockRPMService

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

the class AgentTestUtils method createServiceManager.

public static MockServiceManager createServiceManager(Map<String, Object> configMap) throws Exception {
    AgentConfig config = AgentConfigImpl.createAgentConfig(configMap);
    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("name");
    rpmService.setEverConnected(true);
    rpmService.setErrorService(new ErrorServiceImpl("name"));
    rpmServiceManager.setRPMService(rpmService);
    configService.start();
    serviceManager.start();
    sqlTraceService.start();
    return serviceManager;
}
Also used : SqlTraceServiceImpl(com.newrelic.agent.sql.SqlTraceServiceImpl) 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) SqlTraceService(com.newrelic.agent.sql.SqlTraceService) 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