Search in sources :

Example 21 with Tracer

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()));
}
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 22 with Tracer

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());
}
Also used : HashMap(java.util.HashMap) ExitTracer(com.newrelic.agent.bridge.ExitTracer) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) HeaderType(com.newrelic.api.agent.HeaderType) SpanEvent(com.newrelic.agent.model.SpanEvent) OutboundHeaders(com.newrelic.api.agent.OutboundHeaders) Test(org.junit.Test)

Example 23 with Tracer

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);
}
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 24 with Tracer

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());
}
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) TracedMethod(com.newrelic.agent.bridge.TracedMethod) Test(org.junit.Test)

Example 25 with Tracer

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"));
}
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) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) 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