Search in sources :

Example 41 with TransactionStats

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

the class KeyTransactionProfileTest method testKeyTransactionsAcrossMultipleThreads.

@Test
public void testKeyTransactionsAcrossMultipleThreads() {
    doReturn(new NoOpSet(Sets.newHashSet(1L, 2L))).when(keyTransactionProfile).getActiveThreadIds();
    // Thread 1: |--- Key Tx (TxA1) ---|---  Non-Key Tx   ---|--- Key Tx (TxA3) ---|
    // Thread 2:                       |--- Key Tx (TxA2) ---|
    long key1StartTime = System.nanoTime();
    StackTraceElement[] keyStackTrace1 = generateStackTraceElements(3, "fileName1");
    keyTransactionProfile.addStackTrace(1, true, ThreadType.BasicThreadType.OTHER, keyStackTrace1);
    long key1EndTime = System.nanoTime();
    long nonKeyStartTime = System.nanoTime();
    StackTraceElement[] nonKeyStackTrace = generateStackTraceElements(3, "fileNameNonKey");
    keyTransactionProfile.addStackTrace(1, true, ThreadType.BasicThreadType.OTHER, nonKeyStackTrace);
    long nonKeyEndTime = System.nanoTime();
    keyTransactionProfile.dispatcherTransactionFinished(generateTransactionData(ImmutableMultimap.of(1L, new Duration(nonKeyStartTime, nonKeyEndTime)), nonKeyStartTime, nonKeyEndTime, "nonKeyTransaction", "App"), new TransactionStats());
    long key2StartTime = System.nanoTime();
    StackTraceElement[] keyStackTrace2 = generateStackTraceElements(3, "fileName2");
    keyTransactionProfile.addStackTrace(2, true, ThreadType.BasicThreadType.OTHER, keyStackTrace2);
    long key2EndTime = System.nanoTime();
    long key3StartTime = System.nanoTime();
    StackTraceElement[] keyStackTrace3 = generateStackTraceElements(3, "fileName3");
    keyTransactionProfile.addStackTrace(1, true, ThreadType.BasicThreadType.OTHER, keyStackTrace3);
    long key3EndTime = System.nanoTime();
    keyTransactionProfile.dispatcherTransactionFinished(generateTransactionData(ImmutableMultimap.of(1L, new Duration(key1StartTime, key1EndTime), 2L, new Duration(key2StartTime, key2EndTime), 1L, new Duration(key3StartTime, key3EndTime)), key1StartTime, key3EndTime, "keyTransaction", "App"), new TransactionStats());
    // This would get called by the sampler thread
    keyTransactionProfile.beforeSampling();
    verify(profile, times(1)).addStackTrace(eq(1L), eq(true), eq(ThreadType.BasicThreadType.OTHER), eq(keyStackTrace1[0]), eq(keyStackTrace1[1]), eq(keyStackTrace1[2]));
    verify(profile, times(1)).addStackTrace(eq(2L), eq(true), eq(ThreadType.BasicThreadType.OTHER), eq(keyStackTrace2[0]), eq(keyStackTrace2[1]), eq(keyStackTrace2[2]));
    verify(profile, times(1)).addStackTrace(eq(1L), eq(true), eq(ThreadType.BasicThreadType.OTHER), eq(keyStackTrace3[0]), eq(keyStackTrace3[1]), eq(keyStackTrace3[2]));
    verify(profile, times(0)).addStackTrace(eq(1L), eq(true), eq(ThreadType.BasicThreadType.OTHER), eq(nonKeyStackTrace[0]), eq(nonKeyStackTrace[1]), eq(nonKeyStackTrace[2]));
    assertEquals(2, keyTransactionProfile.getPendingThreadQueueSizes().size());
    assertTrue(keyTransactionProfile.getPendingThreadQueueSizes().containsKey(1L));
    assertTrue(keyTransactionProfile.getPendingThreadQueueSizes().containsKey(2L));
    assertEquals(0, (long) keyTransactionProfile.getPendingThreadQueueSizes().get(1L));
    assertEquals(0, (long) keyTransactionProfile.getPendingThreadQueueSizes().get(2L));
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) Duration(com.newrelic.agent.Duration) Test(org.junit.Test)

Example 42 with TransactionStats

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

the class KeyTransactionProfileTest method testKeyTransaction.

@Test
public void testKeyTransaction() {
    doReturn(new NoOpSet(Sets.newHashSet(1L))).when(keyTransactionProfile).getActiveThreadIds();
    // Create non-key transactions (followed by a key transaction on the same thread)
    keyTransactionProfile.addStackTrace(1, true, ThreadType.BasicThreadType.OTHER, generateStackTraceElements(3, "fileName"));
    long keyTransactionStartTime = System.nanoTime();
    StackTraceElement[] keyStackTrace = generateStackTraceElements(3, "fileName");
    keyTransactionProfile.addStackTrace(1, true, ThreadType.BasicThreadType.OTHER, keyStackTrace);
    long keyTransactionEndTime = System.nanoTime();
    // This should match the key transaction and leave the non-key transaction in the queue
    keyTransactionProfile.dispatcherTransactionFinished(generateTransactionData(ImmutableMultimap.of(1L, new Duration(keyTransactionStartTime, keyTransactionEndTime)), keyTransactionStartTime, keyTransactionEndTime, "keyTransaction", "App"), new TransactionStats());
    // This would get called by the sampler thread
    keyTransactionProfile.beforeSampling();
    verify(profile, times(1)).addStackTrace(eq(1L), eq(true), eq(ThreadType.BasicThreadType.OTHER), eq(keyStackTrace[0]), eq(keyStackTrace[1]), eq(keyStackTrace[2]));
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) Duration(com.newrelic.agent.Duration) Test(org.junit.Test)

Example 43 with TransactionStats

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

the class SpringBootTest method testDuplicateTransactions.

@Test
public void testDuplicateTransactions() throws Exception {
    final AtomicInteger txCounter = new AtomicInteger(0);
    final AtomicInteger finishedTxCount = new AtomicInteger(0);
    final AtomicReference<String> finishedTxString = new AtomicReference<>();
    try (ConfigurableApplicationContext context = SpringApplication.run(ArticleResource.class)) {
        ServiceFactory.getTransactionService().addTransactionListener(new ExtendedTransactionListener() {

            @Override
            public void dispatcherTransactionStarted(Transaction transaction) {
                txCounter.incrementAndGet();
            }

            @Override
            public void dispatcherTransactionCancelled(Transaction transaction) {
            // no-op
            }

            @Override
            public void dispatcherTransactionFinished(TransactionData transactionData, TransactionStats transactionStats) {
                txCounter.decrementAndGet();
                if (transactionData.getBlameMetricName().startsWith("OtherTransaction")) {
                    return;
                }
                finishedTxCount.incrementAndGet();
                finishedTxString.set(transactionData.getBlameMetricName());
            }
        });
        int port = (int) context.getBean("port");
        HttpURLConnection connection = (HttpURLConnection) new URL("http", "localhost", port, "/").openConnection();
        int responseCode = connection.getResponseCode();
        assertEquals(200, responseCode);
        // 20 * 250ms = 5 seconds
        int timeout = 10;
        while (timeout > 0 && txCounter.get() > 0) {
            Thread.sleep(250);
            timeout--;
        }
        assertEquals(1, finishedTxCount.get());
        assertEquals("WebTransaction/SpringController/ (GET)", finishedTxString.get());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) URL(java.net.URL) ExtendedTransactionListener(com.newrelic.agent.ExtendedTransactionListener) TransactionStats(com.newrelic.agent.stats.TransactionStats) HttpURLConnection(java.net.HttpURLConnection) Transaction(com.newrelic.agent.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Example 44 with TransactionStats

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

the class CrossProcessStateCatApiTest method setUpTransaction.

private void setUpTransaction(Transaction tx, TransactionActivity txa, Object lock, Dispatcher dispatcher, CrossProcessConfig config, String guid) {
    when(txa.getTransaction()).thenReturn(tx);
    when(tx.getLock()).thenReturn(lock);
    when(tx.getDispatcher()).thenReturn(dispatcher);
    when(tx.getCrossProcessConfig()).thenReturn(config);
    DistributedTracePayloadImpl distributedTracePayload = DistributedTracePayloadImpl.createDistributedTracePayload("", "", "", 0f);
    when(tx.createDistributedTracePayload(guid)).thenReturn(distributedTracePayload);
    TransactionStats transactionStats = Mockito.mock(TransactionStats.class);
    SimpleStatsEngine stats = Mockito.mock(SimpleStatsEngine.class);
    when(stats.getOrCreateResponseTimeStats(anyString())).thenReturn(Mockito.mock(ResponseTimeStats.class));
    when(transactionStats.getUnscopedStats()).thenReturn(stats);
    when(txa.getTransactionStats()).thenReturn(transactionStats);
    when(tx.getTransactionActivity()).thenReturn(txa);
    InboundHeaders headers = Mockito.mock(InboundHeaders.class);
    InboundHeaderState inboundHeaderState = new InboundHeaderState(tx, headers);
    when(tx.getInboundHeaderState()).thenReturn(inboundHeaderState);
    PriorityTransactionName priorityTransactionName = PriorityTransactionName.create("Something/Or/other", "category", TransactionNamePriority.FRAMEWORK);
    when(tx.getPriorityTransactionName()).thenReturn(priorityTransactionName);
    TransactionCounts txnCounts = Mockito.mock(TransactionCounts.class);
    when(txnCounts.isOverTracerSegmentLimit()).thenReturn(false);
    when(tx.getTransactionCounts()).thenReturn(txnCounts);
}
Also used : ResponseTimeStats(com.newrelic.agent.stats.ResponseTimeStats) TransactionStats(com.newrelic.agent.stats.TransactionStats) PriorityTransactionName(com.newrelic.agent.transaction.PriorityTransactionName) InboundHeaders(com.newrelic.api.agent.InboundHeaders) SimpleStatsEngine(com.newrelic.agent.stats.SimpleStatsEngine) TransactionCounts(com.newrelic.agent.transaction.TransactionCounts) DistributedTracePayloadImpl(com.newrelic.agent.tracing.DistributedTracePayloadImpl)

Example 45 with TransactionStats

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

the class CrossProcessStateTest method processOutboundResponseHeaders.

@Test
public void processOutboundResponseHeaders() {
    String incomingId = "6#66";
    String obfuscatedAppData = Obfuscator.obfuscateNameUsingKey("[\"6#66\",\"TestTransaction\\/name\",0.0,0.0,12345,\"5001D\",false]", encodingKey);
    cps.processOutboundResponseHeaders(null, 0);
    TransactionStats txStats = mock(TransactionStats.class);
    TransactionActivity ta = mock(TransactionActivity.class);
    when(tx.getTransactionActivity()).thenReturn(ta);
    when(ta.getTransactionStats()).thenReturn(txStats);
    SimpleStatsEngine statsEngine = mock(SimpleStatsEngine.class);
    when(txStats.getUnscopedStats()).thenReturn(statsEngine);
    ResponseTimeStats stats = mock(ResponseTimeStats.class);
    when(statsEngine.getOrCreateResponseTimeStats(anyString())).thenReturn(stats);
    InboundHeaderState ihs = mock(InboundHeaderState.class);
    when(ihs.getClientCrossProcessId()).thenReturn(incomingId);
    when(ihs.isTrustedCatRequest()).thenReturn(true);
    when(tx.getInboundHeaderState()).thenReturn(ihs);
    AgentConfig agentConfig = mock(AgentConfig.class);
    DistributedTracingConfig distributedTracingConfig = mock(DistributedTracingConfig.class);
    when(distributedTracingConfig.isEnabled()).thenReturn(false);
    when(agentConfig.getDistributedTracingConfig()).thenReturn(distributedTracingConfig);
    when(tx.getAgentConfig()).thenReturn(agentConfig);
    PriorityTransactionName txName = mock(PriorityTransactionName.class);
    when(tx.getPriorityTransactionName()).thenReturn(txName);
    when(txName.getName()).thenReturn("TestTransaction/name");
    when(tx.getGuid()).thenReturn("5001D");
    when(config.getCrossProcessId()).thenReturn(incomingId);
    cps.processOutboundResponseHeaders(outboundHeaders, 12345);
    verify(outboundHeaders).setHeader(eq("X-NewRelic-App-Data"), eq(obfuscatedAppData));
    cps.processOutboundResponseHeaders(outboundHeaders, 12345);
    verify(outboundHeaders, Mockito.times(2)).getHeaderType();
    verifyNoMoreInteractions(outboundHeaders);
    verify(config, atLeastOnce()).isCrossApplicationTracing();
    verify(config, atLeastOnce()).getCrossProcessId();
    verify(config, atLeastOnce()).getEncodingKey();
    verify(tx, atLeastOnce()).getAgentConfig();
    verify(tx, atLeastOnce()).getCrossProcessConfig();
    verify(tx, atLeastOnce()).getInboundHeaderState();
    verify(tx, atLeastOnce()).isIgnore();
    verify(tx, atLeastOnce()).getLock();
    verify(tx, atLeastOnce()).getGuid();
    verify(tx, atLeastOnce()).freezeTransactionName();
    verify(tx, atLeastOnce()).getRunningDurationInNanos();
    verify(tx, atLeastOnce()).getExternalTime();
    verify(tx, atLeastOnce()).getPriorityTransactionName();
    verify(txName, atLeastOnce()).getName();
    verify(tx, atLeastOnce()).getTransactionActivity();
    verify(ta, atLeastOnce()).getTransactionStats();
    verify(txStats, atLeastOnce()).getUnscopedStats();
    verify(statsEngine, atLeastOnce()).getOrCreateResponseTimeStats(anyString());
    verify(stats, atLeastOnce()).recordResponseTime(anyLong(), any(TimeUnit.class));
    verifyNoMoreInteractions(txStats, statsEngine, stats, txName);
}
Also used : ResponseTimeStats(com.newrelic.agent.stats.ResponseTimeStats) AgentConfig(com.newrelic.agent.config.AgentConfig) DistributedTracingConfig(com.newrelic.agent.config.DistributedTracingConfig) TransactionStats(com.newrelic.agent.stats.TransactionStats) PriorityTransactionName(com.newrelic.agent.transaction.PriorityTransactionName) TimeUnit(java.util.concurrent.TimeUnit) SimpleStatsEngine(com.newrelic.agent.stats.SimpleStatsEngine) Matchers.anyString(org.mockito.Matchers.anyString) 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