use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class TransactionTraceTest method testOneTracerInTransactionWithDepthChildren.
@Test
public void testOneTracerInTransactionWithDepthChildren() {
// transaction
long start = System.currentTimeMillis();
Transaction.getTransaction();
Tracer rootTracer = IntrospectorImplTest.createOtherTracer("rootOnly");
Transaction.getTransaction().getTransactionActivity().tracerStarted(rootTracer);
Tracer child1 = IntrospectorImplTest.createDefaultTracer("default1");
Transaction.getTransaction().getTransactionActivity().tracerStarted(child1);
Tracer child2 = IntrospectorImplTest.createDefaultTracer("default2");
Transaction.getTransaction().getTransactionActivity().tracerStarted(child2);
Tracer child3 = IntrospectorImplTest.createDefaultTracer("default3");
Transaction.getTransaction().getTransactionActivity().tracerStarted(child3);
child3.setAgentAttribute("myatt", "hello");
child3.setAgentAttribute("myNumber", 99);
child3.finish(Opcodes.RETURN, 0);
child2.finish(Opcodes.RETURN, 0);
child1.finish(Opcodes.RETURN, 0);
rootTracer.finish(Opcodes.RETURN, 0);
long end = System.currentTimeMillis();
// data check
Assert.assertEquals(1, impl.getFinishedTransactionCount());
Map<String, TracedMetricData> metrics = impl.getUnscopedMetrics();
TracedMetricData txMetric = metrics.get("OtherTransaction/rootOnly");
Collection<TransactionTrace> traces = impl.getTransactionTracesForTransaction("OtherTransaction/rootOnly");
Assert.assertEquals(1, traces.size());
TransactionTrace trace = traces.iterator().next();
Assert.assertEquals(txMetric.getTotalTimeInSec(), trace.getResponseTimeInSec(), .01);
Assert.assertEquals(trace.getWallClockDurationInSec(), trace.getResponseTimeInSec(), .01);
Assert.assertTrue(trace.getStartTime() >= start);
Assert.assertTrue(trace.getStartTime() <= end);
TraceSegment segment = trace.getInitialTraceSegment();
Assert.assertEquals(1, segment.getChildren().size());
TraceSegment seg = segment.getChildren().get(0);
long relativeStart = segment.getRelativeStartTime();
long relativeEnd = segment.getRelativeEndTime();
int count = 0;
while (seg != null) {
count++;
Assert.assertTrue(seg.getRelativeStartTime() >= relativeStart);
Assert.assertTrue(seg.getRelativeEndTime() <= relativeEnd);
Assert.assertTrue(seg.getRelativeStartTime() <= seg.getRelativeEndTime());
relativeStart = seg.getRelativeStartTime();
relativeEnd = seg.getRelativeEndTime();
Map<String, Object> info = seg.getTracerAttributes();
Assert.assertNull(info.get("async_context"));
Assert.assertNotNull(info.get("exclusive_duration_millis"));
Assert.assertTrue(seg.getMethodName().startsWith("default"));
Assert.assertTrue(seg.getName().startsWith("Custom/default"));
Assert.assertEquals("MyClass", seg.getClassName());
if (seg.getName().equals("Custom/default3")) {
Assert.assertEquals("hello", info.get("myatt"));
Assert.assertEquals(99, info.get("myNumber"));
Assert.assertEquals(0, seg.getChildren().size());
seg = null;
} else {
Assert.assertEquals(1, seg.getChildren().size());
seg = seg.getChildren().get(0);
}
}
Assert.assertEquals(3, count);
}
use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class ApiTest method runTestExternalCatAPI.
@Trace(dispatcher = true)
private void runTestExternalCatAPI() {
URL myURL = null;
try {
Thread.sleep(1000);
myURL = new URL("http://localhost:8088");
HttpUriRequest request = RequestBuilder.get().setUri(myURL.toURI()).build();
ApiTestHelper.OutboundWrapper outboundWrapper = new ApiTestHelper.OutboundWrapper(request, HeaderType.HTTP);
com.newrelic.api.agent.TracedMethod tracedMethod = NewRelic.getAgent().getTracedMethod();
tracedMethod.addOutboundRequestHeaders(outboundWrapper);
CloseableHttpClient connection = HttpClientBuilder.create().build();
CloseableHttpResponse response = connection.execute(request);
ApiTestHelper.InboundWrapper inboundHeaders = new ApiTestHelper.InboundWrapper(response, HeaderType.HTTP);
ExternalParameters params = HttpParameters.library("HttpClient").uri(myURL.toURI()).procedure("execute").inboundHeaders(inboundHeaders).build();
NewRelic.getAgent().getTransaction().getTracedMethod().reportAsExternal(params);
Tracer rootTracer = Transaction.getTransaction().getRootTracer();
rootTracer.finish(0, null);
Map<String, Object> attributes = rootTracer.getAgentAttributes();
Assert.assertNotNull(attributes.get("transaction_guid"));
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class CircuitBreakerApiTest method runTestExternalCatAPI.
@Trace(dispatcher = true)
private void runTestExternalCatAPI() {
URL myURL = null;
try {
Thread.sleep(1000);
myURL = new URL("http://localhost:8080");
HttpUriRequest request = RequestBuilder.get().setUri(myURL.toURI()).build();
ApiTestHelper.OutboundWrapper outboundWrapper = new ApiTestHelper.OutboundWrapper(request, HeaderType.HTTP);
TracedMethod tracedMethod = NewRelic.getAgent().getTracedMethod();
tracedMethod.addOutboundRequestHeaders(outboundWrapper);
Assert.assertTrue(request.getHeaders("X-NewRelic-ID").length == 0);
ApiTestHelper.DummyRequest incomingRequest = new ApiTestHelper.DummyRequest(HeaderType.HTTP);
ApiTestHelper.DummyResponse response = new ApiTestHelper.DummyResponse(HeaderType.HTTP);
NewRelic.getAgent().getTransaction().setWebRequest(incomingRequest);
NewRelic.getAgent().getTransaction().setWebResponse(response);
NewRelic.getAgent().getTransaction().addOutboundResponseHeaders();
NewRelic.getAgent().getTransaction().markResponseSent();
Assert.assertFalse(response.didSetHeader());
Tracer rootTracer = Transaction.getTransaction().getRootTracer();
Assert.assertNull(rootTracer);
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class CircuitBreakerApiTest method runTestExternalAPI.
@Trace(dispatcher = true)
private void runTestExternalAPI() {
String library = "HttpClient";
URI uri = null;
try {
uri = new URI("http://localhost:8080/test/this/path?name=Bob");
} catch (URISyntaxException e) {
e.printStackTrace();
}
String operation = "execute";
NewRelic.getAgent().getTracedMethod().reportAsExternal(GenericParameters.library(library).uri(uri).procedure(operation).build());
Tracer rootTracer = Transaction.getTransaction().getRootTracer();
Assert.assertNull(rootTracer);
}
use of com.newrelic.agent.tracers.Tracer 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();
}
}
Aggregations