Search in sources :

Example 36 with TransactionData

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());
}
Also used : TransactionListener(com.newrelic.agent.TransactionListener) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransactionStats(com.newrelic.agent.stats.TransactionStats) PriorityTransactionName(com.newrelic.agent.transaction.PriorityTransactionName) TransactionData(com.newrelic.agent.TransactionData) CountDownLatch(java.util.concurrent.CountDownLatch) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 37 with TransactionData

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();
    }
}
Also used : EnvironmentHolder(test.newrelic.test.agent.EnvironmentHolder) Tracer(com.newrelic.agent.tracers.Tracer) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) TransactionDataList(com.newrelic.agent.TransactionDataList) TransactionEvent(com.newrelic.agent.service.analytics.TransactionEvent) TransactionEventsService(com.newrelic.agent.service.analytics.TransactionEventsService) SpanEvent(com.newrelic.agent.model.SpanEvent) TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Example 38 with TransactionData

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());
}
Also used : TransactionInsights(com.newrelic.agent.service.analytics.InsightsServiceImpl.TransactionInsights) TransactionData(com.newrelic.agent.TransactionData) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Example 39 with TransactionData

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));
}
Also used : TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Example 40 with TransactionData

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));
}
Also used : TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Aggregations

TransactionData (com.newrelic.agent.TransactionData)145 Test (org.junit.Test)136 TransactionDataList (com.newrelic.agent.TransactionDataList)46 TransactionStats (com.newrelic.agent.stats.TransactionStats)40 HashMap (java.util.HashMap)34 StatsEngine (com.newrelic.agent.stats.StatsEngine)29 Tracer (com.newrelic.agent.tracers.Tracer)23 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)17 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)16 Transaction (com.newrelic.agent.Transaction)15 EventTestHelper.generateTransactionData (com.newrelic.agent.service.analytics.EventTestHelper.generateTransactionData)15 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)15 JSONArray (org.json.simple.JSONArray)15 MockRPMService (com.newrelic.agent.MockRPMService)14 Map (java.util.Map)14 TransactionService (com.newrelic.agent.TransactionService)12 Java7IncompatibleTest (com.newrelic.test.marker.Java7IncompatibleTest)12 JSONObject (org.json.simple.JSONObject)12 TransactionDataTestBuilder (com.newrelic.agent.TransactionDataTestBuilder)10 SpanEvent (com.newrelic.agent.model.SpanEvent)10