Search in sources :

Example 91 with StatsEngine

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);
        }
    }
}
Also used : StatsWork(com.newrelic.agent.stats.StatsWork) HttpError(com.newrelic.agent.transport.HttpError) StatsEngine(com.newrelic.agent.stats.StatsEngine)

Example 92 with StatsEngine

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();
}
Also used : Environment(com.newrelic.agent.environment.Environment) CountDownLatch(java.util.concurrent.CountDownLatch) StatsEngine(com.newrelic.agent.stats.StatsEngine) Test(org.junit.Test) AgentConfigFactoryTest(com.newrelic.agent.config.AgentConfigFactoryTest)

Example 93 with StatsEngine

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Environment(com.newrelic.agent.environment.Environment) StatsEngine(com.newrelic.agent.stats.StatsEngine) Test(org.junit.Test) AgentConfigFactoryTest(com.newrelic.agent.config.AgentConfigFactoryTest)

Example 94 with StatsEngine

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();
}
Also used : Environment(com.newrelic.agent.environment.Environment) CountDownLatch(java.util.concurrent.CountDownLatch) StatsEngine(com.newrelic.agent.stats.StatsEngine) Test(org.junit.Test) AgentConfigFactoryTest(com.newrelic.agent.config.AgentConfigFactoryTest)

Example 95 with StatsEngine

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());
}
Also used : HashMap(java.util.HashMap) ConnectionConfigListener(com.newrelic.agent.ConnectionConfigListener) ApdexStats(com.newrelic.agent.stats.ApdexStats) MockRPMServiceManager(com.newrelic.agent.MockRPMServiceManager) WebRequestDispatcher(com.newrelic.agent.dispatchers.WebRequestDispatcher) StatsEngine(com.newrelic.agent.stats.StatsEngine) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Aggregations

StatsEngine (com.newrelic.agent.stats.StatsEngine)96 Test (org.junit.Test)78 TransactionDataList (com.newrelic.agent.TransactionDataList)36 StatsEngineImpl (com.newrelic.agent.stats.StatsEngineImpl)31 TransactionData (com.newrelic.agent.TransactionData)29 HashMap (java.util.HashMap)25 JmxMetric (com.newrelic.agent.jmx.metrics.JmxMetric)20 StatsWork (com.newrelic.agent.stats.StatsWork)8 MockRPMService (com.newrelic.agent.MockRPMService)7 ArrayList (java.util.ArrayList)6 AgentConfigFactoryTest (com.newrelic.agent.config.AgentConfigFactoryTest)5 Environment (com.newrelic.agent.environment.Environment)5 MetricName (com.newrelic.agent.metric.MetricName)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Attribute (javax.management.Attribute)5 MBeanServer (javax.management.MBeanServer)5 ResponseTimeStats (com.newrelic.agent.stats.ResponseTimeStats)4 Stats (com.newrelic.agent.stats.Stats)4 HttpError (com.newrelic.agent.transport.HttpError)4 CountStatistic (javax.management.j2ee.statistics.CountStatistic)4