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