use of com.newrelic.agent.stats.TransactionStats in project newrelic-java-agent by newrelic.
the class ProfileTest method testAsyncStackTracesMultipleThreads.
@Test
public void testAsyncStackTracesMultipleThreads() throws Exception {
ExecutorService singleThreadExecutor1 = Executors.newSingleThreadExecutor();
ExecutorService singleThreadExecutor2 = Executors.newSingleThreadExecutor();
Future<Long> getThreadId1 = singleThreadExecutor1.submit(new Callable<Long>() {
@Override
public Long call() throws Exception {
return Thread.currentThread().getId();
}
});
Future<Long> getThreadId2 = singleThreadExecutor2.submit(new Callable<Long>() {
@Override
public Long call() throws Exception {
return Thread.currentThread().getId();
}
});
final Long threadId1 = getThreadId1.get(10, TimeUnit.SECONDS);
final Long threadId2 = getThreadId2.get(10, TimeUnit.SECONDS);
ProfilerParameters parameters = new ProfilerParameters(-1L, 0L, 0L, false, false, false, "keyTransaction", "asyncStackTraces");
final KeyTransactionProfile profile = new KeyTransactionProfile(new Profile(parameters));
profile.start();
StackTraceElement[] keyTransactionTrace1 = new StackTraceElement[] { createSimpleStackTrace("keyTransaction1"), createSimpleStackTrace("keyTransaction1"), createSimpleStackTrace("keyTransaction1") };
final long startTime1 = System.nanoTime();
profile.addStackTrace(threadId1, true, ThreadType.BasicThreadType.OTHER, keyTransactionTrace1);
final long endTime1 = System.nanoTime();
StackTraceElement[] keyTransactionTrace2 = new StackTraceElement[] { createSimpleStackTrace("keyTransaction2"), createSimpleStackTrace("keyTransaction2"), createSimpleStackTrace("keyTransaction2") };
final long startTime2 = System.nanoTime();
profile.addStackTrace(threadId2, true, ThreadType.BasicThreadType.OTHER, keyTransactionTrace2);
final long endTime2 = System.nanoTime();
Future<?> transactionFinished = singleThreadExecutor2.submit(new Runnable() {
@Override
public void run() {
Multimap<Long, Duration> threadIdToDuration = ArrayListMultimap.create();
threadIdToDuration.put(threadId1, new Duration(startTime1, endTime1));
threadIdToDuration.put(threadId2, new Duration(startTime2, endTime2));
profile.dispatcherTransactionFinished(generateTransactionData(threadIdToDuration, startTime1, endTime2, "asyncStackTraces"), new TransactionStats());
}
});
transactionFinished.get(10, TimeUnit.SECONDS);
profile.end();
ProfileTree requestProfileTree = profile.getProfileTree(ThreadType.BasicThreadType.OTHER);
Collection<ProfileSegment> rootSegments = requestProfileTree.getRootSegments();
assertNotNull(rootSegments);
assertEquals(2, rootSegments.size());
}
use of com.newrelic.agent.stats.TransactionStats in project newrelic-java-agent by newrelic.
the class Transaction method transactionFinishedActivityMerging.
private TransactionStats transactionFinishedActivityMerging() {
// here the last transaction activity is not on the completed list
TransactionStats transactionStats = txStats;
long totalCpuTime = 0;
boolean reportingCpu = true;
// this is for the legacy async
Object val = getIntrinsicAttributes().remove(AttributeNames.CPU_TIME_PARAMETER_NAME);
if (val instanceof Long) {
totalCpuTime = (Long) val;
if (totalCpuTime < 0) {
reportingCpu = false;
}
}
// iterate over transaction activities
for (TransactionActivity kid : getFinishedChildren()) {
// merge activity stats
if (transactionStats == null) {
transactionStats = kid.getTransactionStats();
} else {
TransactionStats stats = kid.getTransactionStats();
transactionStats.getScopedStats().mergeStats(stats.getScopedStats());
transactionStats.getUnscopedStats().mergeStats(stats.getUnscopedStats());
}
// merge totalTime, set end time
if (kid.getRootTracer() != null) {
Tracer rootTracer = kid.getRootTracer();
transactionTime.markTransactionActivityAsDone(rootTracer.getEndTime(), rootTracer.getDuration());
if (Agent.LOG.isFinestEnabled()) {
Map<String, Object> tracerAtts = rootTracer.getAgentAttributes();
if (tracerAtts != null && !tracerAtts.isEmpty()) {
Agent.LOG.log(Level.FINEST, "Tracer Attributes for {0} are {1}", rootTracer, tracerAtts);
}
}
}
// all or nothing
if (reportingCpu) {
if (kid.getTotalCpuTime() >= 0) {
totalCpuTime += kid.getTotalCpuTime();
} else {
reportingCpu = false;
}
}
}
if (reportingCpu && totalCpuTime > 0) {
// -1 means it was not computed
getIntrinsicAttributes().put(AttributeNames.CPU_TIME_PARAMETER_NAME, totalCpuTime);
}
return transactionStats;
}
use of com.newrelic.agent.stats.TransactionStats in project newrelic-java-agent by newrelic.
the class DataCollectionConfigCrossAgentTest method createAndVerifyTransactionEvent.
private void createAndVerifyTransactionEvent(Long expectedCount, Long expectedEndpointCount) {
TransactionDataToDistributedTraceIntrinsics transactionDataToDistributedTraceIntrinsics = mock(TransactionDataToDistributedTraceIntrinsics.class);
TransactionEventsService transactionEventsService = new TransactionEventsService(transactionDataToDistributedTraceIntrinsics);
serviceManager.setTransactionEventsService(transactionEventsService);
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);
TransactionStats transactionStats = new TransactionStats();
transactionEventsService.dispatcherTransactionFinished(transactionData, transactionStats);
}
// Verify that the correct number of events were stored in the reservoir
DistributedSamplingPriorityQueue<TransactionEvent> eventQueue = transactionEventsService.getOrCreateDistributedSamplingReservoir(APP_NAME);
assertNotNull(eventQueue);
assertEquals(expectedCount.intValue(), eventQueue.size());
// Verify that we sent (or didn't send) the appropriate events
transactionEventsService.harvestEvents(APP_NAME);
int transactionEventsSeen = rpmService.getTransactionEventsSeen();
assertEquals(expectedEndpointCount.intValue(), transactionEventsSeen);
}
use of com.newrelic.agent.stats.TransactionStats in project newrelic-java-agent by newrelic.
the class DistributedTraceCrossAgentTest method assertExpectedMetrics.
private void assertExpectedMetrics(ArrayList metrics, TransactionStats transactionStats) {
Assert.assertNotNull(transactionStats);
for (Object metric : metrics) {
ArrayList expectedStats = (ArrayList) metric;
String expectedMetricName = (String) expectedStats.get(0);
Long expectedMetricCount = (Long) ((JSONArray) metric).get(1);
final String message = String.format("Expected call count %d for: %s", expectedMetricCount, expectedMetricName);
if (expectedMetricName.startsWith("Supportability") || expectedMetricName.startsWith("ErrorsByCaller")) {
Stats actualStat = transactionStats.getUnscopedStats().getStats(expectedMetricName);
Assert.assertEquals(message, expectedMetricCount.intValue(), actualStat.getCallCount());
} else {
ResponseTimeStats actualStat = transactionStats.getUnscopedStats().getOrCreateResponseTimeStats(expectedMetricName);
Assert.assertEquals(message, expectedMetricCount.intValue(), actualStat.getCallCount());
}
}
}
use of com.newrelic.agent.stats.TransactionStats in project newrelic-java-agent by newrelic.
the class ErrorServiceTest method testDistributedTracingAttributes.
@Test
public void testDistributedTracingAttributes() throws Exception {
Map<String, Object> config = new HashMap<>();
Map<String, Object> dtConfig = new HashMap<>();
dtConfig.put("enabled", true);
config.put("distributed_tracing", dtConfig);
Map<String, Object> spanConfig = new HashMap<>();
spanConfig.put("collect_span_events", true);
config.put("span_events", spanConfig);
EventTestHelper.createServiceManager(config);
TransactionData transactionData = createTransactionData(200, new Throwable("Surprising error"), false);
TransactionService txService = ServiceFactory.getTransactionService();
TransactionStats transactionStats = new TransactionStats();
txService.transactionFinished(transactionData, transactionStats);
List<TracedError> tracedErrors = ServiceFactory.getRPMService().getErrorService().getAndClearTracedErrors();
TracedError tracedError = tracedErrors.get(0);
Assert.assertEquals("Surprising error", tracedError.getMessage());
Map<String, ?> intrinsicAtts = tracedError.getIntrinsicAtts();
Assert.assertTrue(intrinsicAtts.containsKey("traceId"));
Assert.assertTrue(intrinsicAtts.containsKey("guid"));
Assert.assertTrue(intrinsicAtts.containsKey("priority"));
Assert.assertTrue(intrinsicAtts.containsKey("sampled"));
}
Aggregations