use of com.newrelic.agent.introspec.Introspector in project newrelic-java-agent by newrelic.
the class OkHttp44Test method testExternal.
@Test
public void testExternal() throws Exception {
URI endpoint = server.getEndPoint();
httpClientExternal(endpoint.toString(), false, 2000);
Introspector introspector = InstrumentationTestRunner.getIntrospector();
Assert.assertEquals(2, introspector.getFinishedTransactionCount());
String txOne = null;
for (String txName : introspector.getTransactionNames()) {
if (txName.matches(".*OkHttp44Test.*")) {
txOne = txName;
}
}
String host1 = endpoint.getHost();
Assert.assertNotNull("Transaction not found", txOne);
Assert.assertEquals(1, MetricsHelper.getScopedMetricCount(txOne, "External/" + host1 + "/OkHttp/execute"));
Assert.assertEquals(1, MetricsHelper.getUnscopedMetricCount("External/" + host1 + "/OkHttp/execute"));
// external rollups
Assert.assertEquals(1, MetricsHelper.getUnscopedMetricCount("External/" + host1 + "/all"));
Assert.assertEquals(1, MetricsHelper.getUnscopedMetricCount("External/allOther"));
Assert.assertEquals(1, MetricsHelper.getUnscopedMetricCount("External/all"));
// verify timing of External/all metrics
TracedMetricData externalMetrics = InstrumentationTestRunner.getIntrospector().getUnscopedMetrics().get("External/all");
assertNotNull(externalMetrics);
assertTrue(externalMetrics.getTotalTimeInSec() > 2);
Collection<TransactionEvent> transactionEvents = introspector.getTransactionEvents(txOne);
Assert.assertEquals(1, transactionEvents.size());
TransactionEvent transactionEvent = transactionEvents.iterator().next();
Assert.assertEquals(1, transactionEvent.getExternalCallCount());
Assert.assertTrue(transactionEvent.getExternalDurationInSec() > 0);
Collection<ExternalRequest> externalRequests = introspector.getExternalRequests(txOne);
Assert.assertEquals(1, externalRequests.size());
ExternalRequest externalRequest = externalRequests.iterator().next();
Assert.assertEquals(1, externalRequest.getCount());
Assert.assertEquals(host1, externalRequest.getHostname());
Assert.assertEquals("OkHttp", externalRequest.getLibrary());
Assert.assertEquals("execute", externalRequest.getOperation());
Assert.assertEquals(Integer.valueOf(200), externalRequest.getStatusCode());
// the test server does return the trailing space, this client does not trim it
Assert.assertEquals("OK ", externalRequest.getStatusText());
}
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());
}
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, true, Collections.<String, Object>emptyMap());
channel.queueBind(queueName, exchangeName, 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.Builder builder = new AMQP.BasicProperties().builder();
builder.replyTo(tempQueue);
channel.basicPublish(exchangeName, queueName, builder.build(), "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());
}
use of com.newrelic.agent.introspec.Introspector in project newrelic-java-agent by newrelic.
the class PlayAsyncHttpClientTests method testSuccess.
@Test
public void testSuccess() throws Exception {
Introspector introspector = InstrumentationTestRunner.getIntrospector();
URI endpoint = server.getEndPoint();
String host = endpoint.getHost();
String url = endpoint.toURL().toExternalForm();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger result = new AtomicInteger();
makeAsyncRequest(url + "?no-transaction=true", standaloneWSResponse -> {
result.set(standaloneWSResponse.status());
latch.countDown();
});
latch.await(30, TimeUnit.SECONDS);
assertEquals(200, result.get());
// transaction
assertEquals(1, introspector.getFinishedTransactionCount(TimeUnit.SECONDS.toMillis(30)));
String txName = introspector.getTransactionNames().iterator().next();
// scoped metrics
assertEquals(1, MetricsHelper.getScopedMetricCount(txName, "External/" + host + "/PlayWS/get"));
// unscoped metrics
assertEquals(1, MetricsHelper.getUnscopedMetricCount("External/" + host + "/PlayWS/get"));
assertEquals(1, MetricsHelper.getUnscopedMetricCount("External/" + host + "/all"));
assertEquals(1, MetricsHelper.getUnscopedMetricCount("External/all"));
assertEquals(1, MetricsHelper.getUnscopedMetricCount("External/allOther"));
// events
Collection<TransactionEvent> transactionEvents = introspector.getTransactionEvents(txName);
assertEquals(1, transactionEvents.size());
TransactionEvent transactionEvent = transactionEvents.iterator().next();
assertEquals(1, transactionEvent.getExternalCallCount());
assertTrue(transactionEvent.getExternalDurationInSec() > 0);
// traces
Collection<TransactionTrace> traces = introspector.getTransactionTracesForTransaction(txName);
assertEquals(1, traces.size());
TransactionTrace trace = traces.iterator().next();
for (TraceSegment segment : trace.getInitialTraceSegment().getChildren()) {
Map<String, Object> attributes = segment.getTracerAttributes();
if (attributes.get("async_context").equals("segment-api")) {
assertEquals("", segment.getMethodName());
assertEquals("PlayWS", segment.getClassName());
assertEquals("External/" + host + "/PlayWS/get", segment.getName());
assertEquals(url, segment.getUri());
assertEquals(1, segment.getCallCount());
}
}
// external request information
Collection<ExternalRequest> externalRequests = introspector.getExternalRequests(txName);
assertEquals(1, externalRequests.size());
ExternalRequest externalRequest = externalRequests.iterator().next();
assertEquals(1, externalRequest.getCount());
assertEquals(host, externalRequest.getHostname());
assertEquals("PlayWS", externalRequest.getLibrary());
assertEquals("get", externalRequest.getOperation());
}
use of com.newrelic.agent.introspec.Introspector in project newrelic-java-agent by newrelic.
the class RabbitMQTest_Integration 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, true, 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 = String.format("OtherTransaction/Message/RabbitMQ/Queue/Named/%s", queueName);
final Collection<String> transactionNames = introspector.getTransactionNames();
assertTrue(transactionNames.contains(expectedTransactionName));
Map<String, TracedMetricData> metrics = introspector.getMetricsForTransaction(expectedTransactionName);
assertTrue(metrics.containsKey(String.format("MessageBroker/RabbitMQ/Queue/Consume/Named/%s", queueName)));
}
Aggregations