use of com.newrelic.agent.stats.StatsEngineImpl in project newrelic-java-agent by newrelic.
the class CPUSamplerTest method test.
@Test
public void test() throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("host", "localhost");
map.put("port", 3000);
map.put("ssl", false);
map.put("license_key", "bootstrap_newrelic_admin_license_key_000");
createServiceManager(map);
StatsEngine statsEngine = new StatsEngineImpl();
CPUHarvester harvester = new CPUHarvester();
for (int i = 0; i < 10000; i++) {
harvester.recordCPU(statsEngine);
statsEngine.getMetricNames();
}
List<MetricName> harvest = statsEngine.getMetricNames();
CountStats stats = null;
for (MetricName data : harvest) {
if (MetricNames.CPU.equals(data.getName())) {
stats = (CountStats) statsEngine.getStats(data);
break;
}
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out);
stats.writeJSONString(writer);
writer.close();
System.err.println(out.toString());
}
use of com.newrelic.agent.stats.StatsEngineImpl in project newrelic-java-agent by newrelic.
the class DataCollectionConfigCrossAgentTest method createAndVerifyTransactionTrace.
private void createAndVerifyTransactionTrace(Long expectedCount, Long expectedEndpointCount) {
TransactionTraceService transactionTraceService = new TransactionTraceService();
serviceManager.setTransactionTraceService(transactionTraceService);
long eventsToCreate = 1;
if (expectedCount > 1) {
eventsToCreate = expectedCount;
}
for (long i = 0; i < eventsToCreate; i++) {
TransactionData transactionData = EventTestHelper.generateTransactionDataAndComplete(Collections.<String, Object>emptyMap(), APP_NAME, 10000);
TransactionStats transactionStats = new TransactionStats();
transactionTraceService.dispatcherTransactionFinished(transactionData, transactionStats);
}
// Verify that we sent (or didn't send) the appropriate traces
StatsEngine statsEngine = new StatsEngineImpl();
transactionTraceService.beforeHarvest(APP_NAME, statsEngine);
transactionTraceService.afterHarvest(APP_NAME);
int transactionTracesSeen = rpmService.getTransactionTracesSeen();
assertEquals(expectedEndpointCount.intValue(), transactionTracesSeen);
}
use of com.newrelic.agent.stats.StatsEngineImpl in project newrelic-java-agent by newrelic.
the class HarvestServiceTest method metricLimit.
@Test
public void metricLimit() throws Exception {
Environment environment = ServiceFactory.getEnvironmentService().getEnvironment();
environment.setServerPort(null);
final CountDownLatch latch = new CountDownLatch(2);
MyRPMService rpmService = new MyRPMService() {
@Override
public void harvest(StatsEngine statsEngine) {
latch.countDown();
if (latch.getCount() == 1) {
Assert.assertEquals(MetricIdRegistry.METRIC_LIMIT + 100, statsEngine.getSize());
} else {
Assert.assertEquals(1, statsEngine.getSize());
}
}
};
TestHarvestService harvestService = new TestHarvestService();
harvestService.setReportingPeriod(500L);
harvestService.start();
StatsEngineImpl statsEngine = new StatsEngineImpl();
for (int i = 0; i < MetricIdRegistry.METRIC_LIMIT + 100; i++) {
Stats stats = statsEngine.getStats("Test" + String.valueOf(i));
stats.recordDataPoint(100f);
}
ServiceFactory.getStatsService().doStatsWork(new MergeStatsWork("test", statsEngine), "statsWorkTest");
harvestService.startHarvest(rpmService);
Assert.assertTrue(latch.await(5L, TimeUnit.SECONDS));
harvestService.stop();
}
use of com.newrelic.agent.stats.StatsEngineImpl 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.stats.StatsEngineImpl in project newrelic-java-agent by newrelic.
the class RPMServiceTest method doTestTracedErrorsSizeLimit.
private void doTestTracedErrorsSizeLimit() throws Exception {
List<String> appNames = new ArrayList<>(1);
appNames.add("MyApplication");
final AtomicInteger errorSentCount = new AtomicInteger(0);
RPMService svc = new RPMService(appNames, null, null, new DataSenderListener() {
@Override
public void dataSent(String method, String encoding, String uri, byte[] rawDataSent) {
if (method.equals("error_data")) {
errorSentCount.incrementAndGet();
// Check that the raw data sent is less than the collector limit of 1MB (1000000 bytes)
assertTrue(rawDataSent.length < 1000000);
}
}
@Override
public void dataReceived(String method, String encoding, String uri, Map<?, ?> rawDataReceived) {
if (method.equals("error_data")) {
// The collector should let us know it only recieved 2 error traces (instead of 5)
assertEquals(2L, rawDataReceived.get("return_value"));
}
}
}, Collections.<AgentConnectionEstablishedListener>emptyList());
((MockRPMServiceManager) ServiceFactory.getRPMServiceManager()).setRPMService(svc);
svc.launch();
// are big enough to push the final traced error over the limit and thus prevent it from being sent.
for (int i = 0; i < 5; i++) {
// Each of these adds 249090 bytes, so we can successfully store 4 (996360 bytes -- 996468 with padding)
// but the 5th should push it over the limit so we will end up cutting the array in half
// (which rounds down to 2 elements).
svc.getErrorService().reportError(new LargeStackThrowableError(null, "", new Exception("Test"), System.currentTimeMillis(), null, null, null, null, null, 97500));
}
StatsEngineImpl harvestStatsEngine = new StatsEngineImpl();
((ErrorServiceImpl) svc.getErrorService()).harvestTracedErrors("MyApplication", harvestStatsEngine);
// This will collect the traced errors
svc.harvest(harvestStatsEngine);
Thread.sleep(500);
// one set of errors should get sent because the first will error out
assertEquals(1, errorSentCount.get());
svc.shutdown();
}
Aggregations