Search in sources :

Example 96 with Tracer

use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.

the class TransactionTraceTest method testOneTracerInTransactionWithDepthChildren.

@Test
public void testOneTracerInTransactionWithDepthChildren() {
    // transaction
    long start = System.currentTimeMillis();
    Transaction.getTransaction();
    Tracer rootTracer = IntrospectorImplTest.createOtherTracer("rootOnly");
    Transaction.getTransaction().getTransactionActivity().tracerStarted(rootTracer);
    Tracer child1 = IntrospectorImplTest.createDefaultTracer("default1");
    Transaction.getTransaction().getTransactionActivity().tracerStarted(child1);
    Tracer child2 = IntrospectorImplTest.createDefaultTracer("default2");
    Transaction.getTransaction().getTransactionActivity().tracerStarted(child2);
    Tracer child3 = IntrospectorImplTest.createDefaultTracer("default3");
    Transaction.getTransaction().getTransactionActivity().tracerStarted(child3);
    child3.setAgentAttribute("myatt", "hello");
    child3.setAgentAttribute("myNumber", 99);
    child3.finish(Opcodes.RETURN, 0);
    child2.finish(Opcodes.RETURN, 0);
    child1.finish(Opcodes.RETURN, 0);
    rootTracer.finish(Opcodes.RETURN, 0);
    long end = System.currentTimeMillis();
    // data check
    Assert.assertEquals(1, impl.getFinishedTransactionCount());
    Map<String, TracedMetricData> metrics = impl.getUnscopedMetrics();
    TracedMetricData txMetric = metrics.get("OtherTransaction/rootOnly");
    Collection<TransactionTrace> traces = impl.getTransactionTracesForTransaction("OtherTransaction/rootOnly");
    Assert.assertEquals(1, traces.size());
    TransactionTrace trace = traces.iterator().next();
    Assert.assertEquals(txMetric.getTotalTimeInSec(), trace.getResponseTimeInSec(), .01);
    Assert.assertEquals(trace.getWallClockDurationInSec(), trace.getResponseTimeInSec(), .01);
    Assert.assertTrue(trace.getStartTime() >= start);
    Assert.assertTrue(trace.getStartTime() <= end);
    TraceSegment segment = trace.getInitialTraceSegment();
    Assert.assertEquals(1, segment.getChildren().size());
    TraceSegment seg = segment.getChildren().get(0);
    long relativeStart = segment.getRelativeStartTime();
    long relativeEnd = segment.getRelativeEndTime();
    int count = 0;
    while (seg != null) {
        count++;
        Assert.assertTrue(seg.getRelativeStartTime() >= relativeStart);
        Assert.assertTrue(seg.getRelativeEndTime() <= relativeEnd);
        Assert.assertTrue(seg.getRelativeStartTime() <= seg.getRelativeEndTime());
        relativeStart = seg.getRelativeStartTime();
        relativeEnd = seg.getRelativeEndTime();
        Map<String, Object> info = seg.getTracerAttributes();
        Assert.assertNull(info.get("async_context"));
        Assert.assertNotNull(info.get("exclusive_duration_millis"));
        Assert.assertTrue(seg.getMethodName().startsWith("default"));
        Assert.assertTrue(seg.getName().startsWith("Custom/default"));
        Assert.assertEquals("MyClass", seg.getClassName());
        if (seg.getName().equals("Custom/default3")) {
            Assert.assertEquals("hello", info.get("myatt"));
            Assert.assertEquals(99, info.get("myNumber"));
            Assert.assertEquals(0, seg.getChildren().size());
            seg = null;
        } else {
            Assert.assertEquals(1, seg.getChildren().size());
            seg = seg.getChildren().get(0);
        }
    }
    Assert.assertEquals(3, count);
}
Also used : TracedMetricData(com.newrelic.agent.introspec.TracedMetricData) Tracer(com.newrelic.agent.tracers.Tracer) TransactionTrace(com.newrelic.agent.introspec.TransactionTrace) TraceSegment(com.newrelic.agent.introspec.TraceSegment) Test(org.junit.Test)

Example 97 with Tracer

use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.

the class ApiTest method runTestExternalCatAPI.

@Trace(dispatcher = true)
private void runTestExternalCatAPI() {
    URL myURL = null;
    try {
        Thread.sleep(1000);
        myURL = new URL("http://localhost:8088");
        HttpUriRequest request = RequestBuilder.get().setUri(myURL.toURI()).build();
        ApiTestHelper.OutboundWrapper outboundWrapper = new ApiTestHelper.OutboundWrapper(request, HeaderType.HTTP);
        com.newrelic.api.agent.TracedMethod tracedMethod = NewRelic.getAgent().getTracedMethod();
        tracedMethod.addOutboundRequestHeaders(outboundWrapper);
        CloseableHttpClient connection = HttpClientBuilder.create().build();
        CloseableHttpResponse response = connection.execute(request);
        ApiTestHelper.InboundWrapper inboundHeaders = new ApiTestHelper.InboundWrapper(response, HeaderType.HTTP);
        ExternalParameters params = HttpParameters.library("HttpClient").uri(myURL.toURI()).procedure("execute").inboundHeaders(inboundHeaders).build();
        NewRelic.getAgent().getTransaction().getTracedMethod().reportAsExternal(params);
        Tracer rootTracer = Transaction.getTransaction().getRootTracer();
        rootTracer.finish(0, null);
        Map<String, Object> attributes = rootTracer.getAgentAttributes();
        Assert.assertNotNull(attributes.get("transaction_guid"));
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) ExternalParameters(com.newrelic.api.agent.ExternalParameters) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Trace(com.newrelic.api.agent.Trace)

Example 98 with Tracer

use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.

the class CircuitBreakerApiTest method runTestExternalCatAPI.

@Trace(dispatcher = true)
private void runTestExternalCatAPI() {
    URL myURL = null;
    try {
        Thread.sleep(1000);
        myURL = new URL("http://localhost:8080");
        HttpUriRequest request = RequestBuilder.get().setUri(myURL.toURI()).build();
        ApiTestHelper.OutboundWrapper outboundWrapper = new ApiTestHelper.OutboundWrapper(request, HeaderType.HTTP);
        TracedMethod tracedMethod = NewRelic.getAgent().getTracedMethod();
        tracedMethod.addOutboundRequestHeaders(outboundWrapper);
        Assert.assertTrue(request.getHeaders("X-NewRelic-ID").length == 0);
        ApiTestHelper.DummyRequest incomingRequest = new ApiTestHelper.DummyRequest(HeaderType.HTTP);
        ApiTestHelper.DummyResponse response = new ApiTestHelper.DummyResponse(HeaderType.HTTP);
        NewRelic.getAgent().getTransaction().setWebRequest(incomingRequest);
        NewRelic.getAgent().getTransaction().setWebResponse(response);
        NewRelic.getAgent().getTransaction().addOutboundResponseHeaders();
        NewRelic.getAgent().getTransaction().markResponseSent();
        Assert.assertFalse(response.didSetHeader());
        Tracer rootTracer = Transaction.getTransaction().getRootTracer();
        Assert.assertNull(rootTracer);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Tracer(com.newrelic.agent.tracers.Tracer) TracedMethod(com.newrelic.api.agent.TracedMethod) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) Trace(com.newrelic.api.agent.Trace)

Example 99 with Tracer

use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.

the class CircuitBreakerApiTest method runTestExternalAPI.

@Trace(dispatcher = true)
private void runTestExternalAPI() {
    String library = "HttpClient";
    URI uri = null;
    try {
        uri = new URI("http://localhost:8080/test/this/path?name=Bob");
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    String operation = "execute";
    NewRelic.getAgent().getTracedMethod().reportAsExternal(GenericParameters.library(library).uri(uri).procedure(operation).build());
    Tracer rootTracer = Transaction.getTransaction().getRootTracer();
    Assert.assertNull(rootTracer);
}
Also used : Tracer(com.newrelic.agent.tracers.Tracer) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Trace(com.newrelic.api.agent.Trace)

Example 100 with Tracer

use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.

the class SpanParentTest method testSpanAndTransactionParenting.

@Test
public void testSpanAndTransactionParenting() throws Exception {
    EnvironmentHolder holder = setupEnvironemntHolder("all_enabled_test");
    executeSpanAndTransactionParentingTest();
    try {
        TransactionDataList transactionList = holder.getTransactionList();
        assertEquals(2, transactionList.size());
        TransactionData tx1 = transactionList.get(0);
        TransactionData tx2 = transactionList.get(1);
        Collection<Tracer> tracers1 = tx1.getTracers();
        // Only a "rootTracer" on this transaction (root tracer is not in this list)
        assertEquals(0, tracers1.size());
        Collection<Tracer> tracers2 = tx2.getTracers();
        // 1 "rootTracer" (not in this list) + 2 non-external/datastore tracers + 2 external datastore tracers
        assertEquals(4, tracers2.size());
        SpanEventsService spanEventsService = ServiceFactory.getServiceManager().getSpanEventsService();
        String appName = ServiceFactory.getConfigService().getDefaultAgentConfig().getApplicationName();
        SamplingPriorityQueue<SpanEvent> spanEventsPool = spanEventsService.getOrCreateDistributedSamplingReservoir(appName);
        assertNotNull(spanEventsPool);
        List<SpanEvent> spanEvents = spanEventsPool.asList();
        spanEventsPool.clear();
        assertNotNull(spanEvents);
        assertEquals(6, spanEvents.size());
        SpanEvent rootSpanEvent = null;
        Set<String> spanEventGuids = new HashSet<>();
        for (SpanEvent spanEvent : spanEvents) {
            if (spanEvent.getParentId() == null) {
                rootSpanEvent = spanEvent;
            }
            spanEventGuids.add(spanEvent.getGuid());
        }
        assertNotNull(rootSpanEvent);
        assertEquals(6, spanEventGuids.size());
        // Ensure that spans are only parented to other spans
        for (SpanEvent spanEvent : spanEvents) {
            if (spanEvent.getParentId() == null) {
                continue;
            }
            assertTrue(spanEventGuids.contains(spanEvent.getParentId()));
        }
        TransactionEventsService transactionEventsService = ServiceFactory.getServiceManager().getTransactionEventsService();
        DistributedSamplingPriorityQueue<TransactionEvent> txEventPool = transactionEventsService.getOrCreateDistributedSamplingReservoir(appName);
        assertNotNull(txEventPool);
        List<TransactionEvent> txEvents = txEventPool.asList();
        txEventPool.clear();
        assertNotNull(txEvents);
        assertEquals(2, txEvents.size());
        TransactionEvent parent = null;
        TransactionEvent child = null;
        for (TransactionEvent txEvent : txEvents) {
            if (txEvent.getParentId() == null) {
                parent = txEvent;
            } else {
                child = txEvent;
            }
        }
        assertNotNull(parent);
        assertNotNull(child);
        assertNull(parent.getParentId());
        assertEquals(parent.getGuid(), child.getParentId());
    } finally {
        holder.close();
    }
}
Also used : EnvironmentHolder(test.newrelic.test.agent.EnvironmentHolder) Tracer(com.newrelic.agent.tracers.Tracer) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) TransactionDataList(com.newrelic.agent.TransactionDataList) TransactionEvent(com.newrelic.agent.service.analytics.TransactionEvent) TransactionEventsService(com.newrelic.agent.service.analytics.TransactionEventsService) SpanEvent(com.newrelic.agent.model.SpanEvent) TransactionData(com.newrelic.agent.TransactionData) 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