use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class TransactionTrace method buildChildren.
@VisibleForTesting
public static Map<Tracer, Collection<Tracer>> buildChildren(Collection<Tracer> tracers) {
if (tracers == null || tracers.isEmpty()) {
return Collections.emptyMap();
}
Map<Tracer, Collection<Tracer>> children = new HashMap<>();
for (Tracer tracer : tracers) {
Tracer parentTracer = tracer.getParentTracer();
while (null != parentTracer && !parentTracer.isTransactionSegment()) {
parentTracer = parentTracer.getParentTracer();
}
if (tracer.getAgentAttribute("async_context") != null && parentTracer != null) {
parentTracer.setAgentAttribute(HAS_ASYNC_CHILD_ATT, Boolean.TRUE);
}
Collection<Tracer> kids = children.get(parentTracer);
if (kids == null) {
kids = new ArrayList<>(parentTracer == null ? 1 : Math.max(1, parentTracer.getChildCount()));
children.put(parentTracer, kids);
}
kids.add(tracer);
}
return children;
}
use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class ServletAsyncTransactionStateImpl method resumeRootTracer.
private Tracer resumeRootTracer() {
stopAsyncProcessingTracer();
Tracer tracer = rootTracer;
rootTracer = null;
return tracer;
}
use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class ServletAsyncTransactionStateImpl method complete.
@Override
public /**
* Call this method when an asynchronous operation is complete.
*/
void complete() {
if (!state.compareAndSet(State.SUSPENDING, State.RUNNING)) {
return;
}
if (Agent.LOG.isFinerEnabled()) {
Agent.LOG.finer(MessageFormat.format("Completing transaction {0}", transaction));
}
// currentTx may be null
Transaction currentTx = Transaction.getTransaction(false);
if (currentTx != transaction) {
Transaction.clearTransaction();
Transaction.setTransaction(transaction);
}
try {
Tracer tracer = resumeRootTracer();
if (tracer != null) {
tracer.finish(Opcodes.ARETURN, null);
}
} finally {
if (currentTx != transaction) {
Transaction.clearTransaction();
if (currentTx != null) {
// JAVA-2644
Transaction.setTransaction(currentTx);
}
}
}
}
use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class SegmentTest method testAsync.
/**
* tracer(dispatcher) start --tracedActivity start --tracedActivity finish (On a different thread)
* tracer finish
* <p>
* Transaction must have two txas due to the Segment being finished on another thread.
* Segment tracer must correctly link to root tracer.
*/
@Test
public void testAsync() 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 Async Segment");
Assert.assertNotNull(segment);
Thread t = new Thread() {
@Override
public void run() {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Need to check this before the segment ends
Assert.assertSame("Segment must be child of root tracer", root, segment.getTracedMethod().getParentTracedMethod());
segment.end();
}
};
t.start();
t.join();
root.finish(Opcodes.ARETURN, null);
ServiceFactory.getTransactionService().processQueue();
// assert num children == 2
assertTrue(root.getTransactionActivity().getTransaction().isFinished());
assertEquals(2, root.getTransactionActivity().getTransaction().getCountOfRunningAndFinishedTransactionActivities());
assertEquals(2, getNumTracers(root.getTransactionActivity().getTransaction()));
}
use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class SegmentTest method testSegmentTracerAttributes.
@Test
public void testSegmentTracerAttributes() throws InterruptedException {
// Assert that an async Segment has three attributes:
// 1) start_thread: name of thread that started the segment
// 2) end_thread: name of thread tht ended the segment
// 3) async_context: "segment-api"
Tracer root = makeTransaction();
Assert.assertNotNull(root);
Assert.assertNotNull(root.getTransactionActivity().getTransaction());
final Transaction txn = root.getTransactionActivity().getTransaction();
final Segment segment = txn.startSegment(MetricNames.CUSTOM, "Segment Name");
final AtomicReference<Tracer> segmentTracerRef = new AtomicReference<>();
Thread finishThread = new Thread(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName("Second Thread");
segmentTracerRef.set(segment.getTracer());
segment.end();
}
});
finishThread.start();
finishThread.join();
root.finish(Opcodes.ARETURN, null);
assertTrue(root.getTransactionActivity().getTransaction().isFinished());
Tracer segmentTracer = segmentTracerRef.get();
assertEquals("Initiating thread does not match", Thread.currentThread().getName(), segmentTracer.getAgentAttribute(Segment.START_THREAD));
assertEquals("Ending thread does not match", "Second Thread", segmentTracer.getAgentAttribute(Segment.END_THREAD));
assertEquals("segment-api", segmentTracer.getAgentAttribute("async_context"));
}
Aggregations