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);
}
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());
}
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));
}
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();
}
}
Aggregations