Search in sources :

Example 96 with TransactionData

use of com.newrelic.agent.TransactionData in project newrelic-java-agent by newrelic.

the class SpanParentTest method testCrossProcessOnly.

@Test
public void testCrossProcessOnly() throws Exception {
    EnvironmentHolder holder = setupEnvironemntHolder("cross_process_only_test");
    executeCrossProcessOnlyTest();
    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(2, spanEvents.size());
        SpanEvent externalSpanEvent = null;
        SpanEvent datastoreSpanEvent = null;
        for (SpanEvent spanEvent : spanEvents) {
            if (spanEvent.getCategory() == SpanCategory.http) {
                externalSpanEvent = spanEvent;
            } else if (spanEvent.getCategory() == SpanCategory.datastore) {
                datastoreSpanEvent = spanEvent;
            }
        }
        assertNotNull(externalSpanEvent);
        assertNotNull(datastoreSpanEvent);
        // For right now, we are putting cross-process only mode on ice. Poor orphan spans :(
        assertNull(externalSpanEvent.getParentId());
        assertNull(datastoreSpanEvent.getParentId());
    } finally {
        holder.close();
    }
}
Also used : TransactionDataList(com.newrelic.agent.TransactionDataList) EnvironmentHolder(test.newrelic.test.agent.EnvironmentHolder) Tracer(com.newrelic.agent.tracers.Tracer) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) SpanEvent(com.newrelic.agent.model.SpanEvent) TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Example 97 with TransactionData

use of com.newrelic.agent.TransactionData in project newrelic-java-agent by newrelic.

the class PreparedStatementSqlTest method sql.

@Test
public void sql() throws Exception {
    final String originalSql = "select * from test where name = ?";
    TransactionDataList txs = new TransactionDataList();
    ServiceFactory.getTransactionService().addTransactionListener(txs);
    try {
        new StubServlet("dude") {

            private static final long serialVersionUID = 1L;

            @Override
            protected void run(HttpServletRequest request, HttpServletResponse response) throws Exception {
                Connection connection = DatabaseHelper.getConnection();
                PreparedStatement statement = connection.prepareStatement(originalSql);
                statement.setString(1, "test");
                statement.executeQuery();
            }
        };
    } catch (Exception e) {
    // ignore
    }
    Assert.assertEquals(2, txs.size());
    TransactionData transactionData = txs.get(1);
    String actualSql = null;
    for (Tracer tracer : AgentHelper.getTracers(transactionData.getRootTracer())) {
        if (tracer instanceof SqlTracer) {
            actualSql = ((SqlTracer) tracer).getSql().toString();
        }
    }
    Assert.assertNotNull(actualSql);
    Assert.assertEquals("select * from test where name = 'test'", actualSql);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) TransactionDataList(com.newrelic.agent.TransactionDataList) SqlTracer(com.newrelic.agent.tracers.SqlTracer) Tracer(com.newrelic.agent.tracers.Tracer) Connection(java.sql.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) PreparedStatement(java.sql.PreparedStatement) TransactionData(com.newrelic.agent.TransactionData) SqlTracer(com.newrelic.agent.tracers.SqlTracer) Test(org.junit.Test)

Example 98 with TransactionData

use of com.newrelic.agent.TransactionData in project newrelic-java-agent by newrelic.

the class KafkaTest method produceConsumeTest.

@Test
public void produceConsumeTest() throws Exception {
    EnvironmentHolderSettingsGenerator envHolderSettings = new EnvironmentHolderSettingsGenerator(CONFIG_FILE, "all_enabled_test", CLASS_LOADER);
    EnvironmentHolder holder = new EnvironmentHolder(envHolderSettings);
    holder.setupEnvironment();
    kafkaUnitRule.getKafkaUnit().createTopic(testTopic, 1);
    final KafkaConsumer<String, String> consumer = setupConsumer();
    final CountDownLatch latch = new CountDownLatch(1);
    final ConcurrentLinkedQueue<TransactionData> finishedTransactions = new ConcurrentLinkedQueue<>();
    TransactionListener transactionListener = new TransactionListener() {

        @Override
        public void dispatcherTransactionFinished(TransactionData transactionData, TransactionStats transactionStats) {
            finishedTransactions.add(transactionData);
            latch.countDown();
        }
    };
    ServiceFactory.getTransactionService().addTransactionListener(transactionListener);
    try {
        produceMessage();
        final Future<?> submit = executorService.submit(new Runnable() {

            @Override
            public void run() {
                consumeMessage(consumer);
            }
        });
        submit.get(30, TimeUnit.SECONDS);
        latch.await(30, TimeUnit.SECONDS);
        Assert.assertEquals(2, finishedTransactions.size());
        // Wait for the metrics reporter
        Thread.sleep(1000);
        // Assert on the kafka metrics that we're expecting
        StatsEngine statsEngine = ServiceFactory.getStatsService().getStatsEngineForHarvest(ServiceFactory.getConfigService().getDefaultAgentConfig().getApplicationName());
        Stats messagesConsumed = statsEngine.getStats("MessageBroker/Kafka/Internal/consumer-fetch-manager-metrics/records-consumed-rate");
        Assert.assertNotNull(messagesConsumed);
        Assert.assertTrue(messagesConsumed.getCallCount() > 1);
        Assert.assertTrue(messagesConsumed.getTotal() > 0);
        Stats bytesConsumed = statsEngine.getStats("MessageBroker/Kafka/Internal/consumer-metrics/incoming-byte-rate");
        Assert.assertNotNull(bytesConsumed);
        Assert.assertTrue(bytesConsumed.getCallCount() > 1);
        Assert.assertTrue(bytesConsumed.getTotal() > 0);
        Stats rebalanceAssignedPartition = statsEngine.getStats("MessageBroker/Kafka/Rebalance/Assigned/" + testTopic + "/0");
        Assert.assertNotNull(rebalanceAssignedPartition);
        Assert.assertEquals(1, rebalanceAssignedPartition.getCallCount());
        Stats serializationByTopic = statsEngine.getStats("MessageBroker/Kafka/Serialization/TestTopic");
        Assert.assertNotNull(serializationByTopic);
        // One for the key, one for the value
        Assert.assertEquals(2, serializationByTopic.getCallCount());
        Assert.assertTrue(serializationByTopic.getTotal() > 1);
        Stats deserializationByTopic = statsEngine.getStats("MessageBroker/Kafka/Deserialization/TestTopic");
        Assert.assertNotNull(deserializationByTopic);
        // One for the key, one for the value
        Assert.assertEquals(2, deserializationByTopic.getCallCount());
        Assert.assertTrue(deserializationByTopic.getTotal() > 1);
        // external reporting test
        TransactionData prodTxn = finishedTransactions.poll();
        Collection<Tracer> tracers = prodTxn.getTracers();
        Iterator<Tracer> iterator = tracers.iterator();
        Assert.assertTrue(iterator.hasNext());
        Tracer tracer = iterator.next();
        Assert.assertEquals("MessageBroker/Kafka/Topic/Produce/Named/TestTopic", tracer.getMetricName());
        TransactionData conTxn = finishedTransactions.poll();
        Tracer rootTracer = conTxn.getRootTracer();
        Assert.assertEquals("MessageBroker/Kafka/Topic/Consume/Named/TestTopic", rootTracer.getMetricName());
        Assert.assertNotNull(conTxn.getInboundDistributedTracePayload());
    } finally {
        ServiceFactory.getTransactionService().removeTransactionListener(transactionListener);
        consumer.close();
    }
}
Also used : TransactionListener(com.newrelic.agent.TransactionListener) EnvironmentHolderSettingsGenerator(test.newrelic.EnvironmentHolderSettingsGenerator) EnvironmentHolder(test.newrelic.test.agent.EnvironmentHolder) Tracer(com.newrelic.agent.tracers.Tracer) CountDownLatch(java.util.concurrent.CountDownLatch) StatsEngine(com.newrelic.agent.stats.StatsEngine) TransactionStats(com.newrelic.agent.stats.TransactionStats) TransactionStats(com.newrelic.agent.stats.TransactionStats) Stats(com.newrelic.agent.stats.Stats) TransactionData(com.newrelic.agent.TransactionData) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Test(org.junit.Test)

Example 99 with TransactionData

use of com.newrelic.agent.TransactionData in project newrelic-java-agent by newrelic.

the class TransactionEventsServiceTest method testUserParametersDisabled.

@Test
public void testUserParametersDisabled() throws Exception {
    setup(true, false, TEST_RESERVOIR_SIZE);
    Map<String, Object> userParams = new LazyMapImpl<>();
    userParams.put("key1", "value1");
    userParams.put("key2", "value2");
    TransactionData transactionData = generateTransactionDataAndComplete(userParams, APP_NAME);
    TransactionStats transactionStats = new TransactionStats();
    transactionStats.getUnscopedStats().getOrCreateResponseTimeStats(MetricNames.DISPATCHER).recordResponseTime(8, TimeUnit.MILLISECONDS);
    // populate the eventData map
    service.harvestEvents(APP_NAME);
    DistributedSamplingPriorityQueue<TransactionEvent> currentEventData = getEventData(APP_NAME);
    assertEquals(0, currentEventData.size());
    service.dispatcherTransactionFinished(transactionData, transactionStats);
    assertEquals(1, currentEventData.size());
    assertEquals(100f / TimeConversion.MILLISECONDS_PER_SECOND, currentEventData.peek().getDuration(), 0);
    assertTrue(currentEventData.peek().getUserAttributesCopy().isEmpty());
    service.harvestEvents(APP_NAME);
    currentEventData = getEventData(APP_NAME);
    assertEquals(0, currentEventData.size());
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) EventTestHelper.generateTransactionData(com.newrelic.agent.service.analytics.EventTestHelper.generateTransactionData) TransactionData(com.newrelic.agent.TransactionData) LazyMapImpl(com.newrelic.agent.LazyMapImpl) Test(org.junit.Test)

Example 100 with TransactionData

use of com.newrelic.agent.TransactionData in project newrelic-java-agent by newrelic.

the class TransactionEventsServiceTest method testSyntheticsBuffering1.

@Test
public void testSyntheticsBuffering1() throws Exception {
    setup(true, true, TEST_RESERVOIR_SIZE);
    this.rpmService.setSendAnalyticsEventsException(new HttpError("", HttpResponseCode.REQUEST_TIMEOUT, 0));
    TransactionData transactionData = generateSyntheticTransactionData();
    TransactionStats transactionStats = new TransactionStats();
    service.dispatcherTransactionFinished(transactionData, transactionStats);
    service.harvestEvents(APP_NAME);
    assertEquals(1, service.pendingSyntheticsHeaps.size());
    assertEquals(1, service.pendingSyntheticsHeaps.getFirst().size());
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) HttpError(com.newrelic.agent.transport.HttpError) EventTestHelper.generateTransactionData(com.newrelic.agent.service.analytics.EventTestHelper.generateTransactionData) 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