Search in sources :

Example 1 with StatsWork

use of com.newrelic.agent.stats.StatsWork in project newrelic-java-agent by newrelic.

the class TransactionProfile method transactionFinished.

public void transactionFinished(TransactionData transactionData) {
    final List<MetricNameTime> cpuTimes = new ArrayList<>();
    for (TransactionActivity activity : transactionData.getTransactionActivities()) {
        ThreadInfo threadInfo = threadMXBean.getThreadInfo(activity.getThreadId(), 0);
        if (null != threadInfo) {
            final List<List<StackTraceElement>> backtraces = new ArrayList<>();
            Map<Tracer, Collection<Tracer>> tracerTree = buildChildren(activity.getTracers(), backtraces);
            String threadName = threadNameNormalizer.getNormalizedThreadName(new BasicThreadInfo(threadInfo));
            threadActivityProfiles.get(threadName).add(activity, tracerTree);
            if (!backtraces.isEmpty()) {
                ProfileTree tree = threadProfiles.get(threadName);
                for (List<StackTraceElement> stack : backtraces) {
                    stack = DiscoveryProfile.getScrubbedStackTrace(stack);
                    Collections.reverse(stack);
                    tree.addStackTrace(stack, true);
                }
            }
            long cpuTime = activity.getTotalCpuTime();
            if (cpuTime > 0) {
                MetricName name = MetricName.create(transactionData.getBlameMetricName(), threadName);
                cpuTimes.add(new MetricNameTime(name, cpuTime));
            }
        }
    }
    if (!cpuTimes.isEmpty()) {
        ServiceFactory.getStatsService().doStatsWork(new StatsWork() {

            @Override
            public void doWork(StatsEngine statsEngine) {
                for (MetricNameTime time : cpuTimes) {
                    statsEngine.getResponseTimeStats(time.name).recordResponseTime(time.cpuTime, TimeUnit.NANOSECONDS);
                }
            }

            @Override
            public String getAppName() {
                return null;
            }
        }, transactionData.getBlameMetricName());
    }
}
Also used : DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) ArrayList(java.util.ArrayList) TransactionActivity(com.newrelic.agent.TransactionActivity) StatsEngine(com.newrelic.agent.stats.StatsEngine) MetricName(com.newrelic.agent.metric.MetricName) ThreadInfo(java.lang.management.ThreadInfo) BasicThreadInfo(com.newrelic.agent.threads.BasicThreadInfo) StatsWork(com.newrelic.agent.stats.StatsWork) BasicThreadInfo(com.newrelic.agent.threads.BasicThreadInfo) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with StatsWork

use of com.newrelic.agent.stats.StatsWork in project newrelic-java-agent by newrelic.

the class SamplerServiceImpl method mergeStatsEngine.

private void mergeStatsEngine(String appName) {
    StatsService statsService = ServiceFactory.getStatsService();
    StatsWork work = new MergeStatsEngine(appName, statsEngine);
    statsService.doStatsWork(work, statsService.getName());
}
Also used : StatsWork(com.newrelic.agent.stats.StatsWork) StatsService(com.newrelic.agent.stats.StatsService) MergeStatsEngine(com.newrelic.agent.stats.MergeStatsEngine)

Example 3 with StatsWork

use of com.newrelic.agent.stats.StatsWork in project newrelic-java-agent by newrelic.

the class SourceLibraryDetectorTest method testSamplerRun.

@Test
public void testSamplerRun() throws Exception {
    sourceLibraryDetector.run();
    ArgumentCaptor<StatsWork> captor = ArgumentCaptor.forClass(StatsWork.class);
    // As of 7.0.0 we are pulling in kotlin as a transitive dependency of JFR-Daemon
    verify(ServiceFactory.getStatsService(), times(3)).doStatsWork(captor.capture(), anyString());
    StatsWork statsWork = captor.getValue();
    // since the RecordMetric class is package private, we can't actually validate the values contained within it
    assertNotNull(statsWork);
}
Also used : StatsWork(com.newrelic.agent.stats.StatsWork) Test(org.junit.Test)

Example 4 with StatsWork

use of com.newrelic.agent.stats.StatsWork in project newrelic-java-agent by newrelic.

the class LogSenderServiceImpl method harvestEvents.

/**
 * Harvest and send the LogEvents
 *
 * @param appName the application to harvest for
 */
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<LogEvent> reservoir = this.reservoirForApp.put(appName, new DistributedSamplingPriorityQueue<>(appName, LOG_SENDER_SERVICE, maxSamplesStored));
    if (reservoir != null && reservoir.size() > 0) {
        try {
            // Send LogEvents
            ServiceFactory.getRPMServiceManager().getOrCreateRPMService(appName).sendLogEvents(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;
                }
            }, LogSenderServiceImpl.class.getName());
            if (reservoir.size() < reservoir.getNumberOfTries()) {
                int dropped = reservoir.getNumberOfTries() - reservoir.size();
                Agent.LOG.log(Level.FINE, "Dropped {0} log events out of {1}.", dropped, reservoir.getNumberOfTries());
            }
        } catch (HttpError e) {
            if (!e.discardHarvestData()) {
                Agent.LOG.log(Level.FINE, "Unable to send log events. Unsent events will be included in the next harvest.", e);
                // Save unsent data by merging it with current data using reservoir algorithm
                DistributedSamplingPriorityQueue<LogEvent> currentReservoir = reservoirForApp.get(appName);
                currentReservoir.retryAll(reservoir);
            } else {
                // discard harvest data
                reservoir.clear();
                Agent.LOG.log(Level.FINE, "Unable to send log events. Unsent events will be dropped.", e);
            }
        } catch (Exception e) {
            // discard harvest data
            reservoir.clear();
            Agent.LOG.log(Level.FINE, "Unable to send log events. Unsent events will be dropped.", e);
        }
    }
}
Also used : LogEvent(com.newrelic.agent.model.LogEvent) StatsEngine(com.newrelic.agent.stats.StatsEngine) StatsWork(com.newrelic.agent.stats.StatsWork) HttpError(com.newrelic.agent.transport.HttpError) DistributedSamplingPriorityQueue(com.newrelic.agent.service.analytics.DistributedSamplingPriorityQueue)

Example 5 with StatsWork

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

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