Search in sources :

Example 26 with TransactionStats

use of com.newrelic.agent.stats.TransactionStats in project newrelic-java-agent by newrelic.

the class BasicRequestDispatcherTracerTest method requestXStartHeaderRecordMetricsMultipleServers.

@Test
public void requestXStartHeaderRecordMetricsMultipleServers() throws Exception {
    MockHttpRequest httpRequest = new MockHttpRequest();
    long nowInMicroseconds = TimeUnit.MICROSECONDS.convert(Transaction.getTransaction().getWallClockStartTimeMs(), TimeUnit.MILLISECONDS);
    long requestStartTimeInMicroseconds1 = nowInMicroseconds - 30000;
    long requestStartTimeInMicroseconds2 = nowInMicroseconds - 20000;
    httpRequest.setHeader(QueueTimeTracker.REQUEST_X_START_HEADER, "server1 t=" + requestStartTimeInMicroseconds1 + "server2 t=" + requestStartTimeInMicroseconds2);
    WebRequestDispatcher dispatcher = createDispatcher(httpRequest);
    dispatcher.transactionFinished("WebTransaction/Uri/test", stats);
    long txStartTimeInMicroseconds = TimeUnit.MICROSECONDS.convert(dispatcher.getTransaction().getWallClockStartTimeMs(), TimeUnit.MILLISECONDS);
    float expected1 = (float) (requestStartTimeInMicroseconds2 - requestStartTimeInMicroseconds1) / TimeConversion.MICROSECONDS_PER_SECOND;
    float expected2 = (float) (txStartTimeInMicroseconds - requestStartTimeInMicroseconds2) / TimeConversion.MICROSECONDS_PER_SECOND;
    float expectedTotal = (float) (txStartTimeInMicroseconds - requestStartTimeInMicroseconds1) / TimeConversion.MICROSECONDS_PER_SECOND;
    TransactionStats statsEngine = new TransactionStats();
    dispatcher.recordHeaderMetrics(statsEngine);
    String spec = MetricName.QUEUE_TIME.getName();
    Assert.assertEquals(1, statsEngine.getUnscopedStats().getOrCreateResponseTimeStats(spec).getCallCount());
    assertDelta(expectedTotal, statsEngine.getUnscopedStats().getOrCreateResponseTimeStats(spec).getTotal(), .001);
    Assert.assertEquals(1, statsEngine.getSize());
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) WebRequestDispatcher(com.newrelic.agent.dispatchers.WebRequestDispatcher) Test(org.junit.Test)

Example 27 with TransactionStats

use of com.newrelic.agent.stats.TransactionStats in project newrelic-java-agent by newrelic.

the class BasicRequestDispatcherTracerTest method requestXStartAndXQueueHeaderRecordMetrics.

@Test
public void requestXStartAndXQueueHeaderRecordMetrics() throws Exception {
    MockHttpRequest httpRequest = new MockHttpRequest();
    long nowInMicroseconds = TimeUnit.MICROSECONDS.convert(Transaction.getTransaction().getWallClockStartTimeMs(), TimeUnit.MILLISECONDS);
    long requestStartTimeInMicroseconds = nowInMicroseconds - 30000;
    long queueStartTimeInMicroseconds = nowInMicroseconds - 10000;
    httpRequest.setHeader(QueueTimeTracker.REQUEST_X_START_HEADER, "server1 t=" + requestStartTimeInMicroseconds);
    httpRequest.setHeader(QueueTimeTracker.REQUEST_X_QUEUE_START_HEADER, "t=" + queueStartTimeInMicroseconds);
    WebRequestDispatcher dispatcher = createDispatcher(httpRequest);
    dispatcher.transactionFinished("WebTransaction/Uri/test", stats);
    float expectedQueueTime = (float) (nowInMicroseconds - queueStartTimeInMicroseconds) / TimeConversion.MICROSECONDS_PER_SECOND;
    TransactionStats statsEngine = new TransactionStats();
    dispatcher.recordHeaderMetrics(statsEngine);
    String spec = MetricName.QUEUE_TIME.getName();
    Assert.assertEquals(1, statsEngine.getUnscopedStats().getOrCreateResponseTimeStats(spec).getCallCount());
    assertDelta(expectedQueueTime, statsEngine.getUnscopedStats().getOrCreateResponseTimeStats(spec).getTotal(), .01);
    Assert.assertEquals(1, statsEngine.getSize());
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) WebRequestDispatcher(com.newrelic.agent.dispatchers.WebRequestDispatcher) Test(org.junit.Test)

Example 28 with TransactionStats

use of com.newrelic.agent.stats.TransactionStats in project newrelic-java-agent by newrelic.

the class DistributedTraceServiceImplTest method txnFinished.

@Test
public void txnFinished() {
    DistributedTraceServiceImpl distributedTraceService = new DistributedTraceServiceImpl();
    TransactionStats transactionStats = new TransactionStats();
    Map<String, Object> intrinsicAttributes = new HashMap<>();
    long startTimeInMillis = System.currentTimeMillis();
    long responseTimeInNanos = TimeUnit.MILLISECONDS.toNanos(1350);
    TransactionData transactionData = createTransactionData(intrinsicAttributes, startTimeInMillis, responseTimeInNanos, null);
    distributedTraceService.dispatcherTransactionFinished(transactionData, transactionStats);
    SimpleStatsEngine unscopedStats = transactionStats.getUnscopedStats();
    String responseTime = MessageFormat.format(MetricNames.DURATION_BY_PARENT_UNKNOWN_ALL, "HTTPS");
    String errors = MessageFormat.format(MetricNames.ERRORS_BY_PARENT_UNKNOWN, "HTTPS");
    assertEquals(1.35, unscopedStats.getOrCreateResponseTimeStats(responseTime).getTotal(), 0.01f);
    assertEquals(1, unscopedStats.getStats(errors).getCallCount());
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) HashMap(java.util.HashMap) SimpleStatsEngine(com.newrelic.agent.stats.SimpleStatsEngine) TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Example 29 with TransactionStats

use of com.newrelic.agent.stats.TransactionStats in project newrelic-java-agent by newrelic.

the class ApiCallingAsyncTest method testNaming1.

@Test(timeout = 10000)
public void testNaming1() throws Exception {
    final Request req = new Request() {

        @Override
        public HeaderType getHeaderType() {
            return null;
        }

        @Override
        public String getHeader(String name) {
            return null;
        }

        @Override
        public String getRequestURI() {
            return null;
        }

        @Override
        public String getRemoteUser() {
            return null;
        }

        @Override
        public Enumeration<?> getParameterNames() {
            return null;
        }

        @Override
        public String[] getParameterValues(String name) {
            return null;
        }

        @Override
        public Object getAttribute(String name) {
            return null;
        }

        @Override
        public String getCookieValue(String name) {
            return null;
        }
    };
    final Response resp = new Response() {

        @Override
        public HeaderType getHeaderType() {
            return null;
        }

        @Override
        public void setHeader(String name, String value) {
        }

        @Override
        public int getStatus() throws Exception {
            return 0;
        }

        @Override
        public String getStatusMessage() throws Exception {
            return null;
        }

        @Override
        public String getContentType() {
            return null;
        }
    };
    // The two threads call APIs in different orders. The purpose of the test is to ensure
    // that the same metrics get reported either way.
    Thread t1 = new Thread() {

        @Override
        @Trace(dispatcher = true)
        public void run() {
            NewRelic.setTransactionName(null, "MyOtherTransaction");
            NewRelic.setRequestAndResponse(req, resp);
        }
    };
    t1.start();
    t1.join();
    TransactionStats s1 = getStats();
    Thread t2 = new Thread() {

        @Override
        @Trace(dispatcher = true)
        public void run() {
            NewRelic.setRequestAndResponse(req, resp);
            NewRelic.setTransactionName(null, "MyOtherTransaction");
        }
    };
    t2.start();
    t2.join();
    TransactionStats s2 = getStats();
    assertSameKeys(s1, s2, new String[] { "Java/com.newrelic.agent.async.ApiCallingAsyncTest\\$\\d/run" });
}
Also used : Response(com.newrelic.api.agent.Response) TransactionStats(com.newrelic.agent.stats.TransactionStats) Request(com.newrelic.api.agent.Request) Test(org.junit.Test)

Example 30 with TransactionStats

use of com.newrelic.agent.stats.TransactionStats in project newrelic-java-agent by newrelic.

the class AsyncTest method dispatcherTransactionStatsFinished.

@Override
public void dispatcherTransactionStatsFinished(TransactionData transactionData, TransactionStats transactionStats) {
    TransactionStats statsCopy = new TransactionStats();
    try {
        // Create deep copy of transactionStats object
        for (Map.Entry<String, StatsBase> entry : transactionStats.getUnscopedStats().getStatsMap().entrySet()) {
            statsCopy.getUnscopedStats().getStatsMap().put(entry.getKey(), (StatsBase) entry.getValue().clone());
        }
        for (Map.Entry<String, StatsBase> entry : transactionStats.getScopedStats().getStatsMap().entrySet()) {
            statsCopy.getScopedStats().getStatsMap().put(entry.getKey(), (StatsBase) entry.getValue().clone());
        }
    } catch (Exception e) {
        statsCopy = transactionStats;
    }
    timesSet++;
    data = transactionData;
    stats = statsCopy;
    dataList.add(transactionData);
    statsList.add(statsCopy);
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) StatsBase(com.newrelic.agent.stats.StatsBase) Map(java.util.Map)

Aggregations

TransactionStats (com.newrelic.agent.stats.TransactionStats)104 Test (org.junit.Test)90 TransactionData (com.newrelic.agent.TransactionData)40 EventTestHelper.generateTransactionData (com.newrelic.agent.service.analytics.EventTestHelper.generateTransactionData)15 Tracer (com.newrelic.agent.tracers.Tracer)15 HashMap (java.util.HashMap)14 SpanEvent (com.newrelic.agent.model.SpanEvent)13 TransactionService (com.newrelic.agent.TransactionService)12 WebRequestDispatcher (com.newrelic.agent.dispatchers.WebRequestDispatcher)11 SpanEventsService (com.newrelic.agent.service.analytics.SpanEventsService)11 SpanEventsServiceImpl (com.newrelic.agent.service.analytics.SpanEventsServiceImpl)11 ArrayList (java.util.ArrayList)11 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)10 Map (java.util.Map)9 Transaction (com.newrelic.agent.Transaction)8 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)8 ResponseTimeStats (com.newrelic.agent.stats.ResponseTimeStats)7 TransactionEvent (com.newrelic.agent.service.analytics.TransactionEvent)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 JSONArray (org.json.simple.JSONArray)5