use of com.newrelic.agent.stats.StatsEngine in project newrelic-java-agent by newrelic.
the class TransactionEventsService method harvestEvents.
public void harvestEvents(final String appName) {
long startTimeInNanos = System.nanoTime();
beforeHarvestSynthetics(appName);
int targetStored = config.getTargetSamplesStored();
DistributedSamplingPriorityQueue<TransactionEvent> currentReservoir = reservoirForApp.get(appName);
int decidedLast = AdaptiveSampling.decidedLast(currentReservoir, targetStored);
// Now the reservoir for per-transaction analytic events from ordinary non-synthetic transactions
final DistributedSamplingPriorityQueue<TransactionEvent> reservoirToSend = reservoirForApp.put(appName, new DistributedSamplingPriorityQueue<TransactionEvent>(appName, "Transaction Event Service", maxSamplesStored, decidedLast, targetStored));
if (reservoirToSend != null && reservoirToSend.size() > 0) {
try {
ServiceFactory.getRPMServiceManager().getOrCreateRPMService(appName).sendAnalyticsEvents(maxSamplesStored, reservoirToSend.getNumberOfTries(), Collections.unmodifiableList(reservoirToSend.asList()));
final long durationInNanos = System.nanoTime() - startTimeInNanos;
ServiceFactory.getStatsService().doStatsWork(new StatsWork() {
@Override
public void doWork(StatsEngine statsEngine) {
recordSupportabilityMetrics(statsEngine, durationInNanos, reservoirToSend);
}
@Override
public String getAppName() {
return appName;
}
}, reservoirToSend.getServiceName());
} catch (HttpError e) {
if (!e.discardHarvestData()) {
Agent.LOG.log(Level.FINE, "Unable to send events for regular transactions. Data for this harvest will be resampled and the operation will be retried.", e);
// Save unsent data by merging it with current data using reservoir algorithm
currentReservoir = reservoirForApp.get(appName);
currentReservoir.retryAll(reservoirToSend);
} else {
// discard harvest data
reservoirToSend.clear();
Agent.LOG.log(Level.FINE, "Unable to send events for regular transactions. Data for this harvest will be dropped.", e);
}
} catch (Exception e) {
// discard harvest data
reservoirToSend.clear();
Agent.LOG.log(Level.FINE, "Unable to send events for regular transactions. Data for this harvest will be dropped.", e);
}
}
}
use of com.newrelic.agent.stats.StatsEngine in project newrelic-java-agent by newrelic.
the class HarvestServiceTest method multipleRPMServices.
@Test
public void multipleRPMServices() 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();
}
};
final CountDownLatch latch2 = new CountDownLatch(2);
MyRPMService rpmService2 = new MyRPMService() {
@Override
public void harvest(StatsEngine statsEngine) {
latch2.countDown();
}
};
TestHarvestService harvestService = new TestHarvestService();
harvestService.setReportingPeriod(500L);
harvestService.start();
harvestService.startHarvest(rpmService);
harvestService.startHarvest(rpmService2);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS));
harvestService.stop();
}
use of com.newrelic.agent.stats.StatsEngine in project newrelic-java-agent by newrelic.
the class HarvestServiceTest method harvestNowWhenHarvestNotRunning.
@Test
public void harvestNowWhenHarvestNotRunning() throws Exception {
Environment environment = ServiceFactory.getEnvironmentService().getEnvironment();
environment.setServerPort(null);
final AtomicInteger harvestCount = new AtomicInteger();
MyRPMService rpmService = new MyRPMService() {
@Override
public void harvest(StatsEngine statsEngine) {
harvestCount.incrementAndGet();
}
};
TestHarvestService harvestService = new TestHarvestService(666000L);
harvestService.setReportingPeriod(1000000L);
harvestService.start();
harvestService.startHarvest(rpmService);
// try to do an immediate harvest
harvestService.harvestNow();
// should run harvest
Assert.assertEquals(1, harvestCount.get());
harvestService.stop();
}
use of com.newrelic.agent.stats.StatsEngine in project newrelic-java-agent by newrelic.
the class HarvestServiceTest method harvestListener.
@Test
public void harvestListener() throws Exception {
Environment environment = ServiceFactory.getEnvironmentService().getEnvironment();
environment.setServerPort(null);
final CountDownLatch latch = new CountDownLatch(1);
MyRPMService rpmService = new MyRPMService() {
@Override
public void harvest(StatsEngine statsEngine) {
latch.countDown();
}
};
final CountDownLatch latch2 = new CountDownLatch(1);
HarvestListener harvestListener = new HarvestListener() {
@Override
public void beforeHarvest(String appName, StatsEngine statsEngine) {
latch2.countDown();
}
@Override
public void afterHarvest(String appName) {
}
};
TestHarvestService harvestService = new TestHarvestService();
harvestService.setReportingPeriod(500L);
harvestService.addHarvestListener(harvestListener);
harvestService.start();
harvestService.startHarvest(rpmService);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS));
harvestService.stop();
}
use of com.newrelic.agent.stats.StatsEngine 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());
}
Aggregations