use of com.newrelic.agent.TransactionData in project newrelic-java-agent by newrelic.
the class TraceAnnotationTest method testCallAsyncAnnotatedThreadInsideOfTxnMultiThreaded.
/* Case 4: @Trace(async = true), multithreaded with a started Transaction */
@Test
public void testCallAsyncAnnotatedThreadInsideOfTxnMultiThreaded() throws InterruptedException {
Transaction.clearTransaction();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean metricsAssertionsPassed = new AtomicBoolean(false);
// AgentBridge getTransaction call should return null if no Transaction exists in ThreadLocal
Assert.assertNull(AgentBridge.getAgent().getTransaction(false));
// Transaction listener which will only countdown the latch when a specific txn finishes
ServiceFactory.getTransactionService().addTransactionListener(new TransactionListener() {
@Override
public void dispatcherTransactionFinished(TransactionData transactionData, TransactionStats transactionStats) {
PriorityTransactionName ptn = transactionData.getTransaction().getPriorityTransactionName();
String txnMetric1 = "Java/test.newrelic.test.agent.TraceAnnotationTest$AsyncAnnotatedThreadInsideOfTxn/run";
String txnMetric2 = "Java/test.newrelic.test.agent.TraceAnnotationTest/callAsyncAnnotatedThreadInsideOfTxnMultiThreaded";
Map<String, StatsBase> scopedStatsMap = transactionStats.getScopedStats().getStatsMap();
if (ptn.getPartialName().equals("/MyCategory/TracedAsyncTxn")) {
try {
Assert.assertTrue("The following metric should exist: " + txnMetric1, scopedStatsMap.containsKey(txnMetric1));
Assert.assertTrue("The following metric should exist: " + txnMetric2, scopedStatsMap.containsKey(txnMetric2));
metricsAssertionsPassed.set(true);
} catch (Throwable t) {
metricsAssertionsPassed.set(false);
t.printStackTrace();
} finally {
latch.countDown();
}
}
}
});
callAsyncAnnotatedThreadInsideOfTxnMultiThreaded();
latch.await();
Assert.assertTrue("Metric assertions didn't pass", metricsAssertionsPassed.get());
}
use of com.newrelic.agent.TransactionData in project newrelic-java-agent by newrelic.
the class SpanParentTest method testSpanAndTransactionParenting.
@Test
public void testSpanAndTransactionParenting() throws Exception {
EnvironmentHolder holder = setupEnvironemntHolder("all_enabled_test");
executeSpanAndTransactionParentingTest();
try {
TransactionDataList transactionList = holder.getTransactionList();
assertEquals(2, transactionList.size());
TransactionData tx1 = transactionList.get(0);
TransactionData tx2 = transactionList.get(1);
Collection<Tracer> tracers1 = tx1.getTracers();
// Only a "rootTracer" on this transaction (root tracer is not in this list)
assertEquals(0, tracers1.size());
Collection<Tracer> tracers2 = tx2.getTracers();
// 1 "rootTracer" (not in this list) + 2 non-external/datastore tracers + 2 external datastore tracers
assertEquals(4, tracers2.size());
SpanEventsService spanEventsService = ServiceFactory.getServiceManager().getSpanEventsService();
String appName = ServiceFactory.getConfigService().getDefaultAgentConfig().getApplicationName();
SamplingPriorityQueue<SpanEvent> spanEventsPool = spanEventsService.getOrCreateDistributedSamplingReservoir(appName);
assertNotNull(spanEventsPool);
List<SpanEvent> spanEvents = spanEventsPool.asList();
spanEventsPool.clear();
assertNotNull(spanEvents);
assertEquals(6, spanEvents.size());
SpanEvent rootSpanEvent = null;
Set<String> spanEventGuids = new HashSet<>();
for (SpanEvent spanEvent : spanEvents) {
if (spanEvent.getParentId() == null) {
rootSpanEvent = spanEvent;
}
spanEventGuids.add(spanEvent.getGuid());
}
assertNotNull(rootSpanEvent);
assertEquals(6, spanEventGuids.size());
// Ensure that spans are only parented to other spans
for (SpanEvent spanEvent : spanEvents) {
if (spanEvent.getParentId() == null) {
continue;
}
assertTrue(spanEventGuids.contains(spanEvent.getParentId()));
}
TransactionEventsService transactionEventsService = ServiceFactory.getServiceManager().getTransactionEventsService();
DistributedSamplingPriorityQueue<TransactionEvent> txEventPool = transactionEventsService.getOrCreateDistributedSamplingReservoir(appName);
assertNotNull(txEventPool);
List<TransactionEvent> txEvents = txEventPool.asList();
txEventPool.clear();
assertNotNull(txEvents);
assertEquals(2, txEvents.size());
TransactionEvent parent = null;
TransactionEvent child = null;
for (TransactionEvent txEvent : txEvents) {
if (txEvent.getParentId() == null) {
parent = txEvent;
} else {
child = txEvent;
}
}
assertNotNull(parent);
assertNotNull(child);
assertNull(parent.getParentId());
assertEquals(parent.getGuid(), child.getParentId());
} finally {
holder.close();
}
}
use of com.newrelic.agent.TransactionData in project newrelic-java-agent by newrelic.
the class InsightsServiceImplTest method testTransactionHarvest.
@Test
public void testTransactionHarvest() throws Exception {
final Map<String, Object> config = createConfig(true, 2);
InsightsServiceImpl insights = createService(config);
insights.addHarvestableToService(appName);
TransactionInsights txInsights = new TransactionInsights(AgentConfigImpl.createAgentConfig(config));
txInsights.recordCustomEvent("everything", ImmutableMap.<String, Object>of("is", 5, "awesome", true));
txInsights.recordCustomEvent("class", ImmutableMap.<String, Object>of("name", "Foo", "number", 666));
txInsights.recordCustomEvent("method", ImmutableMap.of("className", "Foo", "methodName", "getSomething"));
// Invalid:
insights.recordCustomEvent("invalid-type", ImmutableMap.<String, Object>of("name", "Foo", "number", 666));
Map<String, String> m;
m = Collections.singletonMap("key", "value");
insights.recordCustomEvent(null, m);
m = Collections.singletonMap(null, null);
insights.recordCustomEvent("bothNull", m);
m = Collections.singletonMap("key", null);
insights.recordCustomEvent("valueNull", m);
m = Collections.singletonMap(null, "value");
insights.recordCustomEvent("keyNull", m);
m = new HashMap<>();
m.put("goodKey1", "goodValue");
m.put("goodKey2", null);
m.put(null, null);
insights.recordCustomEvent("AllKeyValueCombos", m);
MockRPMService analyticsData = new MockRPMService();
Mockito.when(ServiceFactory.getServiceManager().getRPMServiceManager().getOrCreateRPMService(appName)).thenReturn(analyticsData);
TransactionData transactionData = Mockito.mock(TransactionData.class);
Mockito.when(transactionData.getApplicationName()).thenReturn(appName);
Mockito.when(transactionData.getInsightsData()).thenReturn(txInsights);
insights.transactionListener.dispatcherTransactionFinished(transactionData, null);
insights.harvestHarvestables();
assertEquals(2, analyticsData.getEvents().size());
}
use of com.newrelic.agent.TransactionData in project newrelic-java-agent by newrelic.
the class SpanEventCreationDeciderTest method shouldAlwaysSayYesIfConfigEnabled.
@Test
public void shouldAlwaysSayYesIfConfigEnabled() {
mockInfiniteTracing(true);
TransactionData mockTransactionData = mock(TransactionData.class);
when(mockTransactionData.sampled()).thenReturn(true);
SpanEventCreationDecider target = new SpanEventCreationDecider(configService);
assertTrue(target.shouldCreateSpans(mockTransactionData));
}
use of com.newrelic.agent.TransactionData in project newrelic-java-agent by newrelic.
the class SpanEventCreationDeciderTest method shouldSayYesIfConfigEnabledAndTransactionNotSampled.
@Test
public void shouldSayYesIfConfigEnabledAndTransactionNotSampled() {
mockInfiniteTracing(true);
TransactionData mockTransactionData = mock(TransactionData.class);
when(mockTransactionData.sampled()).thenReturn(false);
SpanEventCreationDecider target = new SpanEventCreationDecider(configService);
assertTrue(target.shouldCreateSpans(mockTransactionData));
}
Aggregations