Search in sources :

Example 41 with Introspector

use of com.newrelic.agent.introspec.Introspector in project newrelic-java-agent by newrelic.

the class RabbitMQTest_Integration method testCat.

@Test
public void testCat() throws IOException, InterruptedException {
    final String queueName = UUID.randomUUID().toString();
    channel.queueDeclare(queueName, false, false, true, true, Collections.<String, Object>emptyMap());
    final String replyMessage = "reply";
    channel.basicConsume(queueName, new DefaultConsumer(channel) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            channel.basicPublish(DEFAULT_EXCHANGE, properties.getReplyTo(), new AMQP.BasicProperties(), replyMessage.getBytes());
        }
    });
    Thread thread = new Thread(new Runnable() {

        @Override
        @Trace(dispatcher = true)
        public void run() {
            NewRelic.setTransactionName("Category", "Sender");
            try {
                String tempQueue = channel.queueDeclare().getQueue();
                AMQP.BasicProperties properties = new AMQP.BasicProperties();
                properties.setReplyTo(tempQueue);
                channel.basicPublish(DEFAULT_EXCHANGE, queueName, properties, "message".getBytes());
                QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
                channel.basicConsume(tempQueue, true, queueingConsumer);
                // block
                QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
                assertEquals(replyMessage, new String(delivery.getBody()));
            } catch (IOException e) {
            } catch (InterruptedException e) {
            }
        }
    });
    thread.start();
    thread.join(2000);
    Introspector introspector = InstrumentationTestRunner.getIntrospector();
    String senderTransactioName = "OtherTransaction/Category/Sender";
    String messageListenerTransactionName = String.format("OtherTransaction/Message/RabbitMQ/Queue/Named/%s", queueName);
    final Collection<String> transactionNames = introspector.getTransactionNames();
    assertTrue(transactionNames.contains(senderTransactioName));
    assertTrue(transactionNames.contains(messageListenerTransactionName));
    Map<String, TracedMetricData> senderMetrics = introspector.getMetricsForTransaction(senderTransactioName);
    Map<String, TracedMetricData> messageListenerMetrics = introspector.getMetricsForTransaction(messageListenerTransactionName);
    assertTrue(senderMetrics.containsKey(String.format("MessageBroker/RabbitMQ/Queue/Produce/Named/%s", queueName)));
    assertTrue(senderMetrics.containsKey("MessageBroker/RabbitMQ/Queue/Consume/Temp"));
    assertTrue(messageListenerMetrics.containsKey(String.format("MessageBroker/RabbitMQ/Queue/Consume/Named/%s", queueName)));
    assertTrue(messageListenerMetrics.containsKey("MessageBroker/RabbitMQ/Queue/Produce/Temp"));
    // Ideally, the block below could be replaced with the following line:
    // CatHelper.verifyOneSuccessfulCat(introspector, senderTransactioName, messageListenerTransactionName);
    {
        TransactionTrace senderTT = introspector.getTransactionTracesForTransaction(senderTransactioName).iterator().next();
        TransactionTrace messageListenerTT = introspector.getTransactionTracesForTransaction(messageListenerTransactionName).iterator().next();
        Map<String, Object> senderTTIntrinsics = senderTT.getIntrinsicAttributes();
        Map<String, Object> messageListenerTTIntrinsics = messageListenerTT.getIntrinsicAttributes();
        assertNotNull(senderTTIntrinsics.get("trip_id"));
        assertNotNull(senderTTIntrinsics.get("path_hash"));
        assertNotNull(getAttribute(senderTT, "transaction_guid"));
        assertNotNull(messageListenerTTIntrinsics.get("referring_transaction_guid"));
        assertNotNull(messageListenerTTIntrinsics.get("client_cross_process_id"));
        TransactionEvent senderEvent = introspector.getTransactionEvents(senderTransactioName).iterator().next();
        TransactionEvent messageListenerEvent = introspector.getTransactionEvents(messageListenerTransactionName).iterator().next();
        assertEquals(senderEvent.getMyGuid(), messageListenerEvent.getReferrerGuid());
    }
}
Also used : TracedMetricData(com.newrelic.agent.introspec.TracedMetricData) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Introspector(com.newrelic.agent.introspec.Introspector) IOException(java.io.IOException) Envelope(com.rabbitmq.client.Envelope) Trace(com.newrelic.api.agent.Trace) TransactionTrace(com.newrelic.agent.introspec.TransactionTrace) TransactionEvent(com.newrelic.agent.introspec.TransactionEvent) AMQP(com.rabbitmq.client.AMQP) TransactionTrace(com.newrelic.agent.introspec.TransactionTrace) Map(java.util.Map) Test(org.junit.Test)

Example 42 with Introspector

use of com.newrelic.agent.introspec.Introspector in project newrelic-java-agent by newrelic.

the class RabbitMQTest method testMessageListener.

@Test
public void testMessageListener() throws IOException, InterruptedException {
    final String queueName = UUID.randomUUID().toString();
    final String messageForListener = "Hello message listener!";
    channel.queueDeclare(queueName, false, false, false, true, Collections.<String, Object>emptyMap());
    channel.basicPublish(DEFAULT_EXCHANGE, queueName, new AMQP.BasicProperties(), messageForListener.getBytes());
    channel.basicConsume(queueName, new DefaultConsumer(channel) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            assertEquals(messageForListener, new String(body));
        }
    });
    // Let handleDelivery Transaction to finish.
    Thread.sleep(1000);
    Introspector introspector = InstrumentationTestRunner.getIntrospector();
    String expectedTransactionName = "OtherTransaction/Message/RabbitMQ/Exchange/Named/Default";
    final Collection<String> transactionNames = introspector.getTransactionNames();
    assertTrue(transactionNames.contains(expectedTransactionName));
    // Do not record consume metric, message has already been delivered
    Map<String, TracedMetricData> metrics = introspector.getMetricsForTransaction(expectedTransactionName);
    assertFalse(metrics.containsKey("MessageBroker/RabbitMQ/Exchange/Consume/Named/Default"));
}
Also used : TracedMetricData(com.newrelic.agent.introspec.TracedMetricData) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) AMQP(com.rabbitmq.client.AMQP) Introspector(com.newrelic.agent.introspec.Introspector) IOException(java.io.IOException) Envelope(com.rabbitmq.client.Envelope) Test(org.junit.Test)

Example 43 with Introspector

use of com.newrelic.agent.introspec.Introspector in project newrelic-java-agent by newrelic.

the class RabbitMQTest method testProduceConsumePurge.

@Test
public void testProduceConsumePurge() throws IOException {
    Introspector introspector = InstrumentationTestRunner.getIntrospector();
    Map<String, Object> headers = new HashMap<>();
    headers.put("keyOne", 1);
    headers.put("keyTwo", 2);
    String queueOne = UUID.randomUUID().toString();
    putGetAndPurge(DEFAULT_EXCHANGE, "direct", queueOne, null, null, null);
    String queueOneTxn = MessageFormat.format("OtherTransaction/Test/{0}", queueOne);
    assertTrue(introspector.getTransactionNames().contains(queueOneTxn));
    assertProduceConsumePurgeMetrics("Default", queueOne, introspector.getMetricsForTransaction(queueOneTxn));
    assertProduceConsumeTraceAttrs(introspector.getTransactionTracesForTransaction(queueOneTxn).iterator().next(), "Default", null, null, Collections.<String, Object>emptyMap());
    String queueTwo = UUID.randomUUID().toString();
    putGetAndPurge("MyExchange", "direct", queueTwo, "replyTo", "correlation-id", headers);
    String queueTwoTxn = MessageFormat.format("OtherTransaction/Test/{0}", queueTwo);
    assertTrue(introspector.getTransactionNames().contains(queueTwoTxn));
    assertProduceConsumePurgeMetrics("MyExchange", queueTwo, introspector.getMetricsForTransaction(queueTwoTxn));
    assertProduceConsumeTraceAttrs(introspector.getTransactionTracesForTransaction(queueTwoTxn).iterator().next(), "MyExchange", "replyTo", "correlation-id", Collections.<String, Object>emptyMap());
    String queueThree = UUID.randomUUID().toString();
    putGetAndPurge("direct", "direct", queueThree, null, null, null);
    String queueThreeTxn = MessageFormat.format("OtherTransaction/Test/{0}", queueThree);
    assertTrue(introspector.getTransactionNames().contains(queueThreeTxn));
    assertProduceConsumePurgeMetrics("direct", queueThree, introspector.getMetricsForTransaction(queueThreeTxn));
    assertProduceConsumeTraceAttrs(introspector.getTransactionTracesForTransaction(queueThreeTxn).iterator().next(), "direct", null, null, Collections.<String, Object>emptyMap());
    String queueFour = UUID.randomUUID().toString();
    putGetAndPurge("TopicExchange", "topic", queueFour, "replyTo", null, headers);
    String queueFourTxn = MessageFormat.format("OtherTransaction/Test/{0}", queueFour);
    assertTrue(introspector.getTransactionNames().contains(queueFourTxn));
    assertProduceConsumePurgeMetrics("TopicExchange", queueFour, introspector.getMetricsForTransaction(queueFourTxn));
    assertProduceConsumeTraceAttrs(introspector.getTransactionTracesForTransaction(queueFourTxn).iterator().next(), "TopicExchange", "replyTo", null, Collections.<String, Object>emptyMap());
    String queueFive = UUID.randomUUID().toString();
    putGetAndPurge("headers", "headers", queueFive, null, "correlation-id", headers);
    String queueFiveTxn = MessageFormat.format("OtherTransaction/Test/{0}", queueFive);
    assertTrue(introspector.getTransactionNames().contains(queueFiveTxn));
    assertProduceConsumePurgeMetrics("headers", queueFive, introspector.getMetricsForTransaction(queueFiveTxn));
    assertProduceConsumeTraceAttrs(introspector.getTransactionTracesForTransaction(queueFiveTxn).iterator().next(), "headers", null, "correlation-id", Collections.<String, Object>emptyMap());
}
Also used : HashMap(java.util.HashMap) Introspector(com.newrelic.agent.introspec.Introspector) Test(org.junit.Test)

Example 44 with Introspector

use of com.newrelic.agent.introspec.Introspector in project newrelic-java-agent by newrelic.

the class RabbitMQTest method testCat.

@Test
public void testCat() throws IOException, InterruptedException {
    final Map<String, Object> deliveryHeaders = new HashMap<>();
    final Map<String, Object> consumerHeaders = new HashMap<>();
    final String queueName = UUID.randomUUID().toString();
    final String replyMessage = "reply";
    final String exchangeName = "MyFavoriteExchange";
    channel.exchangeDeclare(exchangeName, "topic");
    channel.queueDeclare(queueName, false, false, false, true, Collections.<String, Object>emptyMap());
    channel.queueBind(queueName, exchangeName, queueName);
    System.out.println("Queue name is " + queueName);
    channel.basicConsume(queueName, new DefaultConsumer(channel) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            channel.basicPublish(DEFAULT_EXCHANGE, properties.getReplyTo(), new AMQP.BasicProperties(), replyMessage.getBytes());
            consumerHeaders.putAll(properties.getHeaders());
        }
    });
    Thread thread = new Thread(new Runnable() {

        @Override
        @Trace(dispatcher = true)
        public void run() {
            NewRelic.setTransactionName("Category", "Sender");
            try {
                String tempQueue = channel.queueDeclare().getQueue();
                AMQP.BasicProperties basicProperties = new AMQP.BasicProperties();
                basicProperties.setReplyTo(tempQueue);
                channel.basicPublish(exchangeName, queueName, basicProperties, "message".getBytes());
                QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
                channel.basicConsume(tempQueue, true, queueingConsumer);
                // block
                QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
                deliveryHeaders.putAll(delivery.getProperties().getHeaders());
                assertEquals(replyMessage, new String(delivery.getBody()));
            } catch (IOException | InterruptedException ignored) {
            }
        }
    });
    thread.start();
    thread.join(2000);
    Introspector introspector = InstrumentationTestRunner.getIntrospector();
    String senderTransactioName = "OtherTransaction/Category/Sender";
    String messageListenerTransactionName = "OtherTransaction/Message/RabbitMQ/Exchange/Named/MyFavoriteExchange";
    Collection<String> transactionNames = introspector.getTransactionNames();
    assertTrue(transactionNames.contains(senderTransactioName));
    assertTrue(transactionNames.contains(messageListenerTransactionName));
    Map<String, TracedMetricData> senderMetrics = introspector.getMetricsForTransaction(senderTransactioName);
    assertTrue(senderMetrics.containsKey("MessageBroker/RabbitMQ/Exchange/Produce/Named/MyFavoriteExchange"));
    assertTrue(senderMetrics.containsKey("MessageBroker/RabbitMQ/Exchange/Consume/Named/Default"));
    Map<String, TracedMetricData> messageListenerMetrics = introspector.getMetricsForTransaction(messageListenerTransactionName);
    // Do not record consume metric in listener
    assertFalse(messageListenerMetrics.containsKey("MessageBroker/RabbitMQ/Exchange/Consume/Named/MyFavoriteExchange"));
    assertTrue(messageListenerMetrics.containsKey("MessageBroker/RabbitMQ/Exchange/Produce/Named/Default"));
    // Test one-way CAT. Both transactions do a publish/consume
    assertTrue(consumerHeaders.containsKey("NewRelicTransaction"));
    assertTrue(consumerHeaders.containsKey("NewRelicID"));
    assertTrue(deliveryHeaders.containsKey("NewRelicTransaction"));
    assertTrue(deliveryHeaders.containsKey("NewRelicID"));
    TransactionEvent senderEvent = introspector.getTransactionEvents(senderTransactioName).iterator().next();
    TransactionEvent messageListenerEvent = introspector.getTransactionEvents(messageListenerTransactionName).iterator().next();
    assertEquals(senderEvent.getMyGuid(), messageListenerEvent.getReferrerGuid());
    assertEquals(senderEvent.getMyPathHash(), messageListenerEvent.getReferringPathHash());
}
Also used : TracedMetricData(com.newrelic.agent.introspec.TracedMetricData) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) HashMap(java.util.HashMap) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Introspector(com.newrelic.agent.introspec.Introspector) IOException(java.io.IOException) Envelope(com.rabbitmq.client.Envelope) Trace(com.newrelic.api.agent.Trace) TransactionTrace(com.newrelic.agent.introspec.TransactionTrace) TransactionEvent(com.newrelic.agent.introspec.TransactionEvent) AMQP(com.rabbitmq.client.AMQP) Test(org.junit.Test)

Example 45 with Introspector

use of com.newrelic.agent.introspec.Introspector in project newrelic-java-agent by newrelic.

the class MySQLInstrumentedTest method testParametrizedRequests.

@Test
public void testParametrizedRequests() {
    // Given
    Introspector introspector = InstrumentationTestRunner.getIntrospector();
    DatastoreHelper helper = new DatastoreHelper("MySQL");
    // When
    R2dbcTestUtils.parametrizedRequests(connection);
    // Then
    assertEquals(1, introspector.getFinishedTransactionCount(1000));
    assertEquals(1, introspector.getTransactionNames().size());
    String transactionName = introspector.getTransactionNames().stream().findFirst().orElse("");
    helper.assertScopedStatementMetricCount(transactionName, "INSERT", "USERS", 1);
    helper.assertScopedStatementMetricCount(transactionName, "SELECT", "USERS", 3);
    helper.assertScopedStatementMetricCount(transactionName, "UPDATE", "USERS", 1);
    helper.assertScopedStatementMetricCount(transactionName, "DELETE", "USERS", 1);
    helper.assertAggregateMetrics();
    helper.assertUnscopedOperationMetricCount("INSERT", 1);
    helper.assertUnscopedOperationMetricCount("SELECT", 3);
    helper.assertUnscopedOperationMetricCount("UPDATE", 1);
    helper.assertUnscopedOperationMetricCount("DELETE", 1);
    helper.assertUnscopedStatementMetricCount("INSERT", "USERS", 1);
    helper.assertUnscopedStatementMetricCount("SELECT", "USERS", 3);
    helper.assertUnscopedStatementMetricCount("UPDATE", "USERS", 1);
    helper.assertUnscopedStatementMetricCount("DELETE", "USERS", 1);
}
Also used : Introspector(com.newrelic.agent.introspec.Introspector) DatastoreHelper(com.newrelic.agent.introspec.DatastoreHelper) Test(org.junit.Test)

Aggregations

Introspector (com.newrelic.agent.introspec.Introspector)452 Test (org.junit.Test)432 TransactionEvent (com.newrelic.agent.introspec.TransactionEvent)150 Java16IncompatibleTest (com.newrelic.test.marker.Java16IncompatibleTest)88 Java17IncompatibleTest (com.newrelic.test.marker.Java17IncompatibleTest)88 Java11IncompatibleTest (com.newrelic.test.marker.Java11IncompatibleTest)86 Java12IncompatibleTest (com.newrelic.test.marker.Java12IncompatibleTest)86 Java13IncompatibleTest (com.newrelic.test.marker.Java13IncompatibleTest)86 Java14IncompatibleTest (com.newrelic.test.marker.Java14IncompatibleTest)86 Java15IncompatibleTest (com.newrelic.test.marker.Java15IncompatibleTest)86 DatastoreHelper (com.newrelic.agent.introspec.DatastoreHelper)69 ExternalRequest (com.newrelic.agent.introspec.ExternalRequest)51 TracedMetricData (com.newrelic.agent.introspec.TracedMetricData)51 URI (java.net.URI)41 Java7IncompatibleTest (com.newrelic.test.marker.Java7IncompatibleTest)32 TransactionTrace (com.newrelic.agent.introspec.TransactionTrace)25 TraceSegment (com.newrelic.agent.introspec.TraceSegment)19 Matchers.containsString (org.hamcrest.Matchers.containsString)16 Java10IncompatibleTest (com.newrelic.test.marker.Java10IncompatibleTest)13 Java9IncompatibleTest (com.newrelic.test.marker.Java9IncompatibleTest)13