Search in sources :

Example 41 with StatsEngine

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

Example 42 with StatsEngine

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);
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) StatsEngineImpl(com.newrelic.agent.stats.StatsEngineImpl) TransactionTraceService(com.newrelic.agent.trace.TransactionTraceService) StatsEngine(com.newrelic.agent.stats.StatsEngine)

Example 43 with StatsEngine

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();
}
Also used : StatsEngineImpl(com.newrelic.agent.stats.StatsEngineImpl) Stats(com.newrelic.agent.stats.Stats) 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 44 with StatsEngine

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();
}
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 45 with StatsEngine

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());
}
Also used : ErrorEvent(com.newrelic.agent.model.ErrorEvent) StatsEngine(com.newrelic.agent.stats.StatsEngine) MockRPMService(com.newrelic.agent.MockRPMService)

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