use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class SegmentTest method testIgnoreAsync.
/**
* Ignored async segment should not be included in the transaction and not prevent the transaction from
* finishing.
*/
@Test
public void testIgnoreAsync() throws InterruptedException {
final Tracer root = makeTransaction();
Assert.assertNotNull(root);
Assert.assertNotNull(root.getTransactionActivity().getTransaction());
Thread.sleep(1);
final Segment segment = root.getTransactionActivity().getTransaction().startSegment(MetricNames.CUSTOM, "Custom Segment");
root.finish(Opcodes.ARETURN, null);
Thread t = new Thread() {
@Override
public void run() {
try {
Thread.sleep(5);
assertFalse(root.getTransactionActivity().getTransaction().isFinished());
segment.ignoreIfUnfinished();
} catch (InterruptedException e) {
e.printStackTrace();
}
segment.end();
}
};
t.start();
t.join();
assertTrue(root.getTransactionActivity().getTransaction().isFinished());
assertEquals(1, root.getTransactionActivity().getTransaction().getCountOfRunningAndFinishedTransactionActivities());
assertEquals(1, getNumTracers(root.getTransactionActivity().getTransaction()));
}
use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class SegmentTest method testSpanParenting.
@Test
public void testSpanParenting() {
SpanEventsService spanEventService = ServiceFactory.getSpanEventService();
SamplingPriorityQueue<SpanEvent> eventPool = spanEventService.getOrCreateDistributedSamplingReservoir(APP_NAME);
Transaction.clearTransaction();
Tracer rootTracer = makeTransaction();
rootTracer.getTransactionActivity().getTransaction().setPriorityIfNotNull(1.0f);
rootTracer.setMetricName("RootTracer");
Transaction tx = rootTracer.getTransactionActivity().getTransaction();
Segment segment = tx.startSegment("custom", "segment");
final HashMap<String, String> headers = new HashMap<>();
segment.addOutboundRequestHeaders(new OutboundHeaders() {
@Override
public HeaderType getHeaderType() {
return HeaderType.HTTP;
}
@Override
public void setHeader(String name, String value) {
headers.put(name, value);
}
});
String payload = headers.get("newrelic");
segment.end();
rootTracer.finish(Opcodes.ARETURN, null);
Transaction.clearTransaction();
Tracer secondRootTracer = makeTransaction();
secondRootTracer.setMetricName("SecondRootTracer");
Transaction secondTxn = secondRootTracer.getTransactionActivity().getTransaction();
secondTxn.acceptDistributedTracePayload(payload);
secondRootTracer.finish(Opcodes.ARETURN, null);
List<SpanEvent> spanEvents = eventPool.asList();
Assert.assertNotNull(spanEvents);
assertEquals(3, spanEvents.size());
SpanEvent rootTracerSpanEvent = findSpanByName(spanEvents, "RootTracer");
SpanEvent segmentSpanEvent = findSpanByName(spanEvents, "custom/segment");
SpanEvent secondRootTracerSpanEvent = findSpanByName(spanEvents, "SecondRootTracer");
assertEquals(rootTracerSpanEvent.getTraceId(), segmentSpanEvent.getTraceId());
assertEquals(segmentSpanEvent.getTraceId(), secondRootTracerSpanEvent.getTraceId());
assertEquals(rootTracerSpanEvent.getGuid(), segmentSpanEvent.getParentId());
assertEquals(segmentSpanEvent.getGuid(), secondRootTracerSpanEvent.getParentId());
}
use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class SegmentTest method confirmTxnGc.
@Test
public void confirmTxnGc() {
// Ensure that holding a Segment does not prevent transactions from being collected.
Tracer rootTracer = makeTransaction();
Transaction txn = rootTracer.getTransactionActivity().getTransaction();
// Sanity check: we have a txn
Assert.assertNotNull(rootTracer);
assertEquals("Thread local txn has a different value", txn, Transaction.getTransaction(false));
Segment seg = txn.startSegment(MetricNames.CUSTOM, "My Little Segment");
Assert.assertNotNull(seg);
rootTracer.finish(Opcodes.ARETURN, null);
seg.end();
assertTrue(txn.isFinished());
// let it go, let it go,
// Can't hold it back anymore
// - Queen Elsa
txn = null;
rootTracer = null;
// If this test flickers, consider filing a JIRA to make this a soak test
// Please, please, run the gc
System.gc();
assertNull(seg.getParent());
// Need to find if the underlying txn is still alive.
com.newrelic.api.agent.Transaction tx = seg.getTransaction();
assertNull("Transaction was not garbage collected", tx);
}
use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class SegmentTest method testSyncNamedTracedActivity.
/**
* tracer(dispatcher) start
* --tracedActivity start
* tracer finish
* --tracedActivity finish
* <p>
* Transaction must have two txas due to the tracedActivity being finished after its parent tracer is finished.
* Segment tracer must correctly link to root tracer.
*/
@Test
public void testSyncNamedTracedActivity() throws InterruptedException {
Tracer root = makeTransaction();
Assert.assertNotNull(root);
Assert.assertNotNull(root.getTransactionActivity().getTransaction());
Thread.sleep(1);
final Segment segment = root.getTransactionActivity().getTransaction().startSegment(MetricNames.CUSTOM, "Custom Segment");
Assert.assertNotNull(segment);
root.finish(Opcodes.ARETURN, null);
Thread.sleep(1);
final AtomicReference<TracedMethod> tracedMethod = new AtomicReference<>();
tracedMethod.set(segment.getTracedMethod());
segment.end();
assertTrue(root.getTransactionActivity().getTransaction().isFinished());
assertTrue(tracedMethod.get() instanceof Tracer);
assertEquals("Segment name was not properly set", "activity", ((Tracer) tracedMethod.get()).getTransactionActivity().getAsyncContext());
assertEquals(2, root.getTransactionActivity().getTransaction().getCountOfRunningAndFinishedTransactionActivities());
assertEquals(2, getNumTracers(root.getTransactionActivity().getTransaction()));
Assert.assertSame("Segment must be child of root tracer", root, tracedMethod.get().getParentTracedMethod());
}
use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class SegmentTest method testSegmentAddCustomAttributeAsync.
@Test
public void testSegmentAddCustomAttributeAsync() throws InterruptedException {
Tracer root = makeTransaction();
final Transaction txn = root.getTransactionActivity().getTransaction();
final Segment segment = txn.startSegment("custom", "Segment Name");
final AtomicReference<Tracer> segmentTracerRef = new AtomicReference<>();
Thread finishThread = new Thread(new Runnable() {
@Override
public void run() {
segment.addCustomAttribute("redbeans", "rice");
segment.addCustomAttribute("numBeans", 400);
segment.addCustomAttribute("sausage", true);
segment.addCustomAttribute(null, "Keys cant be null");
Map<String, Object> extras = new HashMap<>();
extras.put("pickles", null);
extras.put("hotSauce", true);
segment.addCustomAttributes(extras);
Thread.currentThread().setName("Second Thread");
segmentTracerRef.set(segment.getTracer());
segment.end();
}
});
finishThread.start();
finishThread.join();
Tracer tracer = segmentTracerRef.get();
assertEquals(4, tracer.getCustomAttributes().size());
assertEquals("rice", tracer.getCustomAttributes().get("redbeans"));
assertEquals(400, tracer.getCustomAttributes().get("numBeans"));
assertTrue((Boolean) tracer.getCustomAttributes().get("sausage"));
assertTrue((Boolean) tracer.getCustomAttributes().get("hotSauce"));
}
Aggregations