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());
}
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());
}
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());
}
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();
}
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;
}
Aggregations