Search in sources :

Example 16 with Tracer

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;
}
Also used : HashMap(java.util.HashMap) SqlTracer(com.newrelic.agent.tracers.SqlTracer) Tracer(com.newrelic.agent.tracers.Tracer) Collection(java.util.Collection) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 17 with Tracer

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;
}
Also used : Tracer(com.newrelic.agent.tracers.Tracer) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) AbstractTracer(com.newrelic.agent.tracers.AbstractTracer)

Example 18 with 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);
            }
        }
    }
}
Also used : Transaction(com.newrelic.agent.Transaction) Tracer(com.newrelic.agent.tracers.Tracer) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) AbstractTracer(com.newrelic.agent.tracers.AbstractTracer)

Example 19 with Tracer

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()));
}
Also used : ExitTracer(com.newrelic.agent.bridge.ExitTracer) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) Test(org.junit.Test)

Example 20 with Tracer

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"));
}
Also used : ExitTracer(com.newrelic.agent.bridge.ExitTracer) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Aggregations

Tracer (com.newrelic.agent.tracers.Tracer)263 Test (org.junit.Test)195 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)104 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)99 Transaction (com.newrelic.agent.Transaction)86 BasicRequestRootTracer (com.newrelic.agent.tracers.servlet.BasicRequestRootTracer)54 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)41 ExitTracer (com.newrelic.agent.bridge.ExitTracer)39 TokenImpl (com.newrelic.agent.TokenImpl)35 StartAndThenLink (com.newrelic.agent.TransactionAsyncUtility.StartAndThenLink)31 OtherRootSqlTracer (com.newrelic.agent.tracers.OtherRootSqlTracer)25 TransactionData (com.newrelic.agent.TransactionData)24 HashMap (java.util.HashMap)24 SqlTracer (com.newrelic.agent.tracers.SqlTracer)20 ArrayList (java.util.ArrayList)19 UltraLightTracer (com.newrelic.agent.tracers.UltraLightTracer)17 TransactionStats (com.newrelic.agent.stats.TransactionStats)16 MockRPMService (com.newrelic.agent.MockRPMService)15 JSONArray (org.json.simple.JSONArray)15 JSONObject (org.json.simple.JSONObject)14