Search in sources :

Example 11 with StatsWork

use of com.newrelic.agent.stats.StatsWork 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)

Aggregations

StatsWork (com.newrelic.agent.stats.StatsWork)11 StatsEngine (com.newrelic.agent.stats.StatsEngine)8 HttpError (com.newrelic.agent.transport.HttpError)4 Test (org.junit.Test)3 MetricName (com.newrelic.agent.metric.MetricName)2 DistributedSamplingPriorityQueue (com.newrelic.agent.service.analytics.DistributedSamplingPriorityQueue)2 TransactionActivity (com.newrelic.agent.TransactionActivity)1 BrowserConfigTest (com.newrelic.agent.browser.BrowserConfigTest)1 ReservoirManager (com.newrelic.agent.interfaces.ReservoirManager)1 CustomInsightsEvent (com.newrelic.agent.model.CustomInsightsEvent)1 ErrorEvent (com.newrelic.agent.model.ErrorEvent)1 LogEvent (com.newrelic.agent.model.LogEvent)1 IncrementCounter (com.newrelic.agent.stats.IncrementCounter)1 MergeStatsEngine (com.newrelic.agent.stats.MergeStatsEngine)1 StatsService (com.newrelic.agent.stats.StatsService)1 BasicThreadInfo (com.newrelic.agent.threads.BasicThreadInfo)1 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)1 Tracer (com.newrelic.agent.tracers.Tracer)1 ThreadInfo (java.lang.management.ThreadInfo)1 ArrayList (java.util.ArrayList)1