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());
}
}
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());
}
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);
}
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);
}
}
}
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);
}
}
}
Aggregations