Search in sources :

Example 76 with TransactionStats

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());
}
Also used : Duration(com.newrelic.agent.Duration) ParseException(org.json.simple.parser.ParseException) IOException(java.io.IOException) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Multimap(com.google.common.collect.Multimap) TransactionStats(com.newrelic.agent.stats.TransactionStats) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 77 with TransactionStats

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;
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) ExitTracer(com.newrelic.agent.bridge.ExitTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer)

Example 78 with 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);
}
Also used : TransactionEvent(com.newrelic.agent.service.analytics.TransactionEvent) TransactionStats(com.newrelic.agent.stats.TransactionStats) TransactionEventsService(com.newrelic.agent.service.analytics.TransactionEventsService) TransactionDataToDistributedTraceIntrinsics(com.newrelic.agent.service.analytics.TransactionDataToDistributedTraceIntrinsics)

Example 79 with TransactionStats

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());
        }
    }
}
Also used : ResponseTimeStats(com.newrelic.agent.stats.ResponseTimeStats) ArrayList(java.util.ArrayList) TransactionStats(com.newrelic.agent.stats.TransactionStats) ResponseTimeStats(com.newrelic.agent.stats.ResponseTimeStats) Stats(com.newrelic.agent.stats.Stats) JSONObject(org.json.simple.JSONObject)

Example 80 with TransactionStats

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"));
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) TransactionService(com.newrelic.agent.TransactionService) HashMap(java.util.HashMap) TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Aggregations

TransactionStats (com.newrelic.agent.stats.TransactionStats)104 Test (org.junit.Test)90 TransactionData (com.newrelic.agent.TransactionData)40 EventTestHelper.generateTransactionData (com.newrelic.agent.service.analytics.EventTestHelper.generateTransactionData)15 Tracer (com.newrelic.agent.tracers.Tracer)15 HashMap (java.util.HashMap)14 SpanEvent (com.newrelic.agent.model.SpanEvent)13 TransactionService (com.newrelic.agent.TransactionService)12 WebRequestDispatcher (com.newrelic.agent.dispatchers.WebRequestDispatcher)11 SpanEventsService (com.newrelic.agent.service.analytics.SpanEventsService)11 SpanEventsServiceImpl (com.newrelic.agent.service.analytics.SpanEventsServiceImpl)11 ArrayList (java.util.ArrayList)11 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)10 Map (java.util.Map)9 Transaction (com.newrelic.agent.Transaction)8 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)8 ResponseTimeStats (com.newrelic.agent.stats.ResponseTimeStats)7 TransactionEvent (com.newrelic.agent.service.analytics.TransactionEvent)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 JSONArray (org.json.simple.JSONArray)5