use of com.newrelic.agent.introspec.TraceSegment in project newrelic-java-agent by newrelic.
the class MetricStateResponseCodeTest method shouldBumpMetricOnceIfAlreadyHaveResponseCode.
/**
* Objective: Calling getResponseCode, then getInputStream, only records the external metric once, and
* only has one External/ segment. The second segment is Custom/.
*/
@Test
public void shouldBumpMetricOnceIfAlreadyHaveResponseCode() throws Exception {
Introspector introspector = InstrumentationTestRunner.getIntrospector();
MetricState target = new MetricState();
callGetResponseCodeThenGetInputStream(target);
// Only one external call.
String transactionName = fetchTransactionName(introspector, "callGetResponseCodeThenGetInputStream");
assertTrue(introspector.getMetricsForTransaction(transactionName).containsKey("External/localhost/HttpURLConnection"));
assertEquals(1, introspector.getMetricsForTransaction(transactionName).get("External/localhost/HttpURLConnection").getCallCount());
// Two child segments within the transaction.
TransactionTrace trace = introspector.getTransactionTracesForTransaction(transactionName).iterator().next();
TraceSegment traceSegment = trace.getInitialTraceSegment();
assertEquals(2, traceSegment.getChildren().size());
// The first segment is external.
TraceSegment renamedSegment = traceSegment.getChildren().get(0);
assertEquals(1, renamedSegment.getCallCount());
assertEquals("External/localhost/HttpURLConnection/getResponseCode", renamedSegment.getName());
// The second is a normal method call.
TraceSegment defaultName = traceSegment.getChildren().get(1);
assertEquals(1, defaultName.getCallCount());
assertEquals("Custom/" + this.getClass().getName() + "/simulatedInstrumentedGetInputStreamMethod", defaultName.getName());
}
use of com.newrelic.agent.introspec.TraceSegment in project newrelic-java-agent by newrelic.
the class ValidationHelper method validateGrpcInteraction.
static void validateGrpcInteraction(TestServer server, String clientTxName, String serverTxName, String fullMethod, String grpcType, String name) {
checkTransactions(2);
Introspector introspector = InstrumentationTestRunner.getIntrospector();
String externalTxSegmentName = "ExternalTransaction/localhost/" + getCrossProcessId() + "/" + serverTxName;
// Verify that CAT works
CatHelper.verifyOneSuccessfulCat(introspector, clientTxName, serverTxName);
// Client side
Collection<TransactionTrace> clientTransactionTrace = introspector.getTransactionTracesForTransaction(clientTxName);
assertEquals(1, clientTransactionTrace.size());
TransactionTrace trace = clientTransactionTrace.iterator().next();
boolean foundSegment = false;
for (TraceSegment segment : trace.getInitialTraceSegment().getChildren()) {
// Verify external request exists and is valid
if (segment.getClassName().equals("External")) {
assertEquals(externalTxSegmentName, segment.getName());
// This is the value of "http.url"
assertEquals("grpc://localhost:" + server.getPort() + "/" + fullMethod, segment.getUri());
assertEquals(1, segment.getCallCount());
assertEquals("", segment.getMethodName());
assertEquals(grpcType, segment.getTracerAttributes().get("grpc.type"));
assertEquals("gRPC", segment.getTracerAttributes().get("component"));
assertEquals(fullMethod, segment.getTracerAttributes().get("http.method"));
foundSegment = true;
}
}
assertTrue("Unable to find client side External/ segment", foundSegment);
// Server side
Collection<TransactionTrace> serverTransactionTrace = introspector.getTransactionTracesForTransaction(serverTxName);
assertEquals(1, serverTransactionTrace.size());
TransactionTrace serverTrace = serverTransactionTrace.iterator().next();
TraceSegment rootSegment = serverTrace.getInitialTraceSegment();
assertTrue(rootSegment.getName().endsWith(fullMethod));
assertEquals(1, rootSegment.getCallCount());
assertEquals(fullMethod, rootSegment.getTracerAttributes().get("request.method"));
assertEquals(0, rootSegment.getTracerAttributes().get("response.status"));
assertEquals(grpcType, rootSegment.getTracerAttributes().get("grpc.type"));
// Custom attributes (to test tracing into customer code)
Collection<TransactionEvent> serverTxEvents = introspector.getTransactionEvents(serverTxName);
assertEquals(1, serverTxEvents.size());
TransactionEvent serverTxEvent = serverTxEvents.iterator().next();
assertNotNull(serverTxEvent);
if (grpcType.equals("BIDI_STREAMING")) {
// For the streaming case, we just ensure that the transaction is available to use for a streaming handler but we do not propagate it
assertEquals("true", serverTxEvent.getAttributes().get("customParameter"));
} else {
assertEquals(name, serverTxEvent.getAttributes().get("sayHelloBefore"));
assertEquals(name, serverTxEvent.getAttributes().get("sayHelloAfter"));
}
assertEquals(0, serverTxEvent.getAttributes().get("response.status"));
assertEquals("grpc://localhost:" + server.getPort() + "/" + fullMethod, serverTxEvent.getAttributes().get("request.uri"));
}
use of com.newrelic.agent.introspec.TraceSegment in project newrelic-java-agent by newrelic.
the class JmsTestFixture method echoTestAssertions.
private void echoTestAssertions() {
Introspector introspector = InstrumentationTestRunner.getIntrospector();
assertEquals(2, introspector.getFinishedTransactionCount());
// Find the requester and responder transactions
final String requesterFullTxName = String.format("OtherTransaction/%s/%s", "Custom", REQUESTER_TX_NAME);
final String responderFullTxName = String.format("OtherTransaction/%s/%s", "Custom", RESPONDER_TX_NAME);
Iterator<String> it = introspector.getTransactionNames().iterator();
String txName1 = it.next();
String txName2 = it.next();
String requester = (txName1.contains(requesterFullTxName)) ? txName1 : txName2;
String responder = (requester.equals(txName1)) ? txName2 : txName1;
assertNotNull(requester);
assertNotNull(responder);
assertNotEquals(requester, responder);
// scoped metrics
assertEquals(1, MetricsHelper.getScopedMetricCount(requester, "Java/com.nr.agent.instrumentation.jms11.integration.JmsTestFixture/echoTest"));
assertEquals(1, MetricsHelper.getScopedMetricCount(requester, "MessageBroker/JMS/Queue/Produce/Named/InstrumentationTestQueue"));
assertEquals(1, MetricsHelper.getScopedMetricCount(requester, "MessageBroker/JMS/Queue/Consume/Temp"));
assertEquals(1, MetricsHelper.getScopedMetricCount(responder, "MessageBroker/JMS/Queue/Consume/Named/InstrumentationTestQueue"));
assertEquals(1, MetricsHelper.getScopedMetricCount(responder, "MessageBroker/JMS/Queue/Produce/Temp"));
// unscoped metrics
assertEquals(1, MetricsHelper.getUnscopedMetricCount("OtherTransactionTotalTime/Custom/JmsEchoTest/Requester"));
assertEquals(2, MetricsHelper.getUnscopedMetricCount("OtherTransaction/all"));
assertEquals(2, MetricsHelper.getUnscopedMetricCount("OtherTransactionTotalTime"));
// transaction events
Collection<TransactionEvent> transactionEvents = introspector.getTransactionEvents(requesterFullTxName);
assertEquals(1, transactionEvents.size());
TransactionEvent transactionEvent = transactionEvents.iterator().next();
// Do not assert on external call count - it is inconsistently implemented and will change in the future
assertTrue(transactionEvent.getExternalDurationInSec() > 0);
transactionEvents = introspector.getTransactionEvents(responderFullTxName);
assertEquals(1, transactionEvents.size());
// Now dig out the transaction trace for the responder (arbitrary choice, could have pulled
// out the requester transaction) and validate that there is one trace segment that matches
// the parameters passed from the platform-specific test code.
Collection<TransactionTrace> traces = introspector.getTransactionTracesForTransaction(responderFullTxName);
assertEquals(1, traces.size());
TransactionTrace trace = traces.iterator().next();
boolean matched = false;
for (TraceSegment segment : trace.getInitialTraceSegment().getChildren()) {
if (segment.getName().equals(params.getSegmentName())) {
if (segment.getClassName().equals(params.getSegmentClassName()) && segment.getMethodName().equals(params.getSegmentMethodName())) {
matched = true;
}
}
assertTrue(segment.getTracerAttributes().containsKey("exclusive_duration_millis"));
}
assertTrue(matched);
// Finally do a simpler verification on the requester trace.
traces = introspector.getTransactionTracesForTransaction(responderFullTxName);
assertEquals(1, traces.size());
}
use of com.newrelic.agent.introspec.TraceSegment in project newrelic-java-agent by newrelic.
the class NingAsyncHttpClient161Tests method testError.
@Test
public void testError() {
String host = "www.thiswebsitedoesntexistreallyatallipromise.com";
String url = "http://" + host + "/";
int status = makeAsyncRequest(url);
assertEquals(-1, status);
// transaction
Introspector introspector = InstrumentationTestRunner.getIntrospector();
assertEquals(1, introspector.getFinishedTransactionCount());
String txName = introspector.getTransactionNames().iterator().next();
// scoped metrics
assertEquals(1, MetricsHelper.getScopedMetricCount(txName, "External/" + host + "/AsyncHttpClient/onThrowable"));
// unscoped metrics
assertEquals(1, MetricsHelper.getUnscopedMetricCount("External/" + host + "/AsyncHttpClient/onThrowable"));
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 ("External".equals(attributes.get("async_context"))) {
assertEquals("", segment.getMethodName());
assertEquals("External Request", segment.getClassName());
assertEquals("External/" + host + "/AsyncHttpClient/onThrowable", 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("AsyncHttpClient", externalRequest.getLibrary());
}
use of com.newrelic.agent.introspec.TraceSegment in project newrelic-java-agent by newrelic.
the class NingAsyncHttpClient161Tests 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();
int status = makeAsyncRequest(url + "?no-transaction=true");
assertEquals(200, status);
// transaction
assertEquals(1, introspector.getFinishedTransactionCount());
String txName = introspector.getTransactionNames().iterator().next();
// scoped metrics
assertEquals(1, MetricsHelper.getScopedMetricCount(txName, "External/" + host + "/AsyncHttpClient/onCompleted"));
// unscoped metrics
assertEquals(1, MetricsHelper.getUnscopedMetricCount("External/" + host + "/AsyncHttpClient/onCompleted"));
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("External")) {
assertEquals("", segment.getMethodName());
assertEquals("External Request", segment.getClassName());
assertEquals("External/" + host + "/AsyncHttpClient/onCompleted", 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("AsyncHttpClient", externalRequest.getLibrary());
assertEquals("onCompleted", externalRequest.getOperation());
assertEquals(Integer.valueOf(200), externalRequest.getStatusCode());
assertEquals("OK", externalRequest.getStatusText());
}
Aggregations