use of com.newrelic.agent.stats.StatsEngine in project newrelic-java-agent by newrelic.
the class InsightsServiceImpl method harvestEvents.
public void harvestEvents(final String appName) {
if (!getIsEnabledForApp(ServiceFactory.getConfigService().getAgentConfig(appName), appName)) {
reservoirForApp.remove(appName);
return;
}
if (maxSamplesStored <= 0) {
clearReservoir(appName);
return;
}
long startTimeInNanos = System.nanoTime();
final DistributedSamplingPriorityQueue<CustomInsightsEvent> reservoir = this.reservoirForApp.put(appName, new DistributedSamplingPriorityQueue<CustomInsightsEvent>(appName, "Insights Service", maxSamplesStored));
if (reservoir != null && reservoir.size() > 0) {
try {
ServiceFactory.getRPMServiceManager().getOrCreateRPMService(appName).sendCustomAnalyticsEvents(maxSamplesStored, reservoir.getNumberOfTries(), Collections.unmodifiableList(reservoir.asList()));
final long durationInNanos = System.nanoTime() - startTimeInNanos;
ServiceFactory.getStatsService().doStatsWork(new StatsWork() {
@Override
public void doWork(StatsEngine statsEngine) {
recordSupportabilityMetrics(statsEngine, durationInNanos, reservoir);
}
@Override
public String getAppName() {
return appName;
}
}, reservoir.getServiceName());
if (reservoir.size() < reservoir.getNumberOfTries()) {
int dropped = reservoir.getNumberOfTries() - reservoir.size();
Agent.LOG.log(Level.FINE, "Dropped {0} custom events out of {1}.", dropped, reservoir.getNumberOfTries());
}
} catch (HttpError e) {
if (!e.discardHarvestData()) {
Agent.LOG.log(Level.FINE, "Unable to send custom events. Unsent events will be included in the next harvest.", e);
// Save unsent data by merging it with current data using reservoir algorithm
DistributedSamplingPriorityQueue<CustomInsightsEvent> currentReservoir = reservoirForApp.get(appName);
currentReservoir.retryAll(reservoir);
} else {
// discard harvest data
reservoir.clear();
Agent.LOG.log(Level.FINE, "Unable to send custom events. Unsent events will be dropped.", e);
}
} catch (Exception e) {
// discard harvest data
reservoir.clear();
Agent.LOG.log(Level.FINE, "Unable to send custom events. Unsent events will be dropped.", e);
}
}
}
use of com.newrelic.agent.stats.StatsEngine 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.StatsEngine 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.StatsEngine in project newrelic-java-agent by newrelic.
the class HarvestServiceTest method reportingPeriod.
@Test
public void reportingPeriod() 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();
}
};
TestHarvestService harvestService = new TestHarvestService();
harvestService.setReportingPeriod(500L);
harvestService.start();
harvestService.startHarvest(rpmService);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
harvestService.stop();
}
use of com.newrelic.agent.stats.StatsEngine in project newrelic-java-agent by newrelic.
the class ErrorServiceTest method checkForEvent.
private void checkForEvent() {
StatsEngine statsEngineForHarvest = ServiceFactory.getStatsService().getStatsEngineForHarvest(EventTestHelper.APP_NAME);
Assert.assertTrue(statsEngineForHarvest.getStats(MetricName.create(MetricNames.SUPPORTABILITY_ERROR_SERVICE_TRANSACTION_ERROR_SENT)).hasData());
Assert.assertTrue(statsEngineForHarvest.getStats(MetricName.create(MetricNames.SUPPORTABILITY_ERROR_SERVICE_TRANSACTION_ERROR_SEEN)).hasData());
Assert.assertEquals(1, ((MockRPMService) ServiceFactory.getRPMService()).getEvents().size());
ErrorEvent errorEvent = (ErrorEvent) Iterables.get(((MockRPMService) ServiceFactory.getRPMService()).getEvents(), 0);
Assert.assertEquals(errorEvent.getUserAttributesCopy().get("test_attribute"), "value");
Assert.assertEquals("TransactionError", errorEvent.getType());
}
Aggregations