Search in sources :

Example 1 with TracedActivity

use of com.newrelic.agent.bridge.TracedActivity in project newrelic-java-agent by newrelic.

the class FlyweightTracerTest method tracedActivityFromFlyweight.

@Trace(excludeFromTransactionTrace = true, leaf = true)
private void tracedActivityFromFlyweight() {
    // When you are in a flyweight method you should not be allowed to start a traced activity due to the
    // fact that we have no real parent tracer to tie the tracer to. We can revisit this in the future if need be
    // but it would require a significant number of changes to the agent
    TracedActivity tracedActivity = AgentBridge.getAgent().getTransaction().createAndStartTracedActivity();
    Assert.assertTrue(tracedActivity instanceof NoOpSegment);
}
Also used : NoOpSegment(com.newrelic.agent.bridge.NoOpSegment) TracedActivity(com.newrelic.agent.bridge.TracedActivity) Trace(com.newrelic.api.agent.Trace)

Example 2 with TracedActivity

use of com.newrelic.agent.bridge.TracedActivity in project newrelic-java-agent by newrelic.

the class SegmentTest method testAsyncNamedTracedActivity.

/**
 * 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 testAsyncNamedTracedActivity() throws InterruptedException {
    final Tracer root = makeTransaction();
    Assert.assertNotNull(root);
    Assert.assertNotNull(root.getTransactionActivity().getTransaction());
    Thread.sleep(1);
    final TracedActivity activity = AgentBridge.getAgent().getTransaction().createAndStartTracedActivity();
    Assert.assertNotNull(activity);
    final AtomicReference<TracedMethod> tracedMethod = new AtomicReference<>();
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            tracedMethod.set(activity.getTracedMethod());
            activity.end();
        }
    };
    t.start();
    t.join();
    root.finish(Opcodes.ARETURN, null);
    // assert num children == 2
    assertTrue(root.getTransactionActivity().getTransaction().isFinished());
    assertTrue(tracedMethod.get() instanceof Tracer);
    assertEquals("Transaction activity context was not properly set", "activity", ((Tracer) tracedMethod.get()).getTransactionActivity().getAsyncContext());
    assertEquals("Segment name not set properly", "Custom/Unnamed Segment", ((Tracer) tracedMethod.get()).getMetricName());
    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());
}
Also used : TracedActivity(com.newrelic.agent.bridge.TracedActivity) 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) TracedMethod(com.newrelic.agent.bridge.TracedMethod) Test(org.junit.Test)

Example 3 with TracedActivity

use of com.newrelic.agent.bridge.TracedActivity in project newrelic-java-agent by newrelic.

the class SegmentTest method testExclusiveTime.

@Test
public void testExclusiveTime() throws InterruptedException {
    DefaultTracer root = (DefaultTracer) makeTransaction();
    Assert.assertNotNull(root);
    Assert.assertNotNull(root.getTransactionActivity().getTransaction());
    final com.newrelic.agent.bridge.TracedActivity tracedActivity = AgentBridge.getAgent().getTransaction().createAndStartTracedActivity();
    DefaultTracer underlyingTracer = (DefaultTracer) tracedActivity.getTracedMethod();
    Thread.sleep(10);
    tracedActivity.end();
    // Assuming the exclusive duration of the tracedActivity tracer is correct, verify the parent's exclusive
    // duration is calculated correctly
    root.performFinishWork(root.getStartTime() + 100, Opcodes.ARETURN, null);
    assertTrue(root.getTransactionActivity().getTransaction().isFinished());
    assertEquals(100, root.getExclusiveDuration());
    assertTrue(underlyingTracer.getExclusiveDuration() >= TimeUnit.MILLISECONDS.toNanos(10));
}
Also used : DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) TracedActivity(com.newrelic.agent.bridge.TracedActivity) Test(org.junit.Test)

Example 4 with TracedActivity

use of com.newrelic.agent.bridge.TracedActivity in project newrelic-java-agent by newrelic.

the class SegmentTest method testAsyncWithTimeout.

/**
 * 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 testAsyncWithTimeout() throws InterruptedException {
    inlineExpirationService.setTimeout(0);
    try {
        final Tracer root = makeTransaction();
        Assert.assertNotNull(root);
        Assert.assertNotNull(root.getTransactionActivity().getTransaction());
        Thread.sleep(1);
        final com.newrelic.agent.bridge.TracedActivity tracedActivity = AgentBridge.getAgent().getTransaction().createAndStartTracedActivity();
        Assert.assertNotNull(tracedActivity);
        Assert.assertSame("Segment must be child of root tracer", root, tracedActivity.getTracedMethod().getParentTracedMethod());
        // Don't start the thread. The timeout is configured to 3 seconds.
        // Allow it to expire and then run the code that implements it.
        TransactionService transactionService = ServiceFactory.getTransactionService();
        for (int i = 0; i < 30 && transactionService.getExpiredTransactionCount() <= 0; i++) {
            ServiceFactory.getTransactionService().processQueue();
            // Wait for up to ~15 seconds or until an expired transaction appears
            Thread.sleep(500);
        }
        root.finish(Opcodes.ARETURN, null);
        assertTrue(root.getTransactionActivity().getTransaction().isFinished());
        assertEquals(2, root.getTransactionActivity().getTransaction().getCountOfRunningAndFinishedTransactionActivities());
        assertEquals(2, getNumTracers(root.getTransactionActivity().getTransaction()));
    } finally {
        inlineExpirationService.clearTimeout();
    }
}
Also used : AsyncTransactionService(com.newrelic.agent.service.async.AsyncTransactionService) ExitTracer(com.newrelic.agent.bridge.ExitTracer) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) TracedActivity(com.newrelic.agent.bridge.TracedActivity) Test(org.junit.Test)

Aggregations

TracedActivity (com.newrelic.agent.bridge.TracedActivity)4 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)3 Test (org.junit.Test)3 ExitTracer (com.newrelic.agent.bridge.ExitTracer)2 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)2 Tracer (com.newrelic.agent.tracers.Tracer)2 NoOpSegment (com.newrelic.agent.bridge.NoOpSegment)1 TracedMethod (com.newrelic.agent.bridge.TracedMethod)1 AsyncTransactionService (com.newrelic.agent.service.async.AsyncTransactionService)1 Trace (com.newrelic.api.agent.Trace)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1