Search in sources :

Example 1 with TransactionData

use of com.newrelic.agent.TransactionData in project newrelic-java-agent by newrelic.

the class RandomTransactionSampler method harvest.

@Override
public List<TransactionTrace> harvest(String appName) {
    TransactionData td = expensiveTransaction.get();
    if (td == FINISHED) {
        return Collections.emptyList();
    }
    if (td == null) {
        return Collections.emptyList();
    }
    if (td.getApplicationName() != appName) {
        return Collections.emptyList();
    }
    if (shouldFinish()) {
        td = expensiveTransaction.getAndSet(FINISHED);
        stop();
    } else {
        td = expensiveTransaction.getAndSet(null);
    }
    tracesSent++;
    return getTransactionTrace(td);
}
Also used : TransactionData(com.newrelic.agent.TransactionData)

Example 2 with TransactionData

use of com.newrelic.agent.TransactionData in project newrelic-java-agent by newrelic.

the class TransactionTraceSampler method noticeTransaction.

@Override
public boolean noticeTransaction(TransactionData td) {
    if (!exceedsThreshold(td)) {
        return false;
    }
    TransactionData maxTrace = expensiveTransaction.get();
    if (maxTrace != null && getScore(maxTrace) >= getScore(td)) {
        return false;
    }
    readLock.lock();
    try {
        return noticeTransactionUnderLock(td);
    } finally {
        readLock.unlock();
    }
}
Also used : TransactionData(com.newrelic.agent.TransactionData)

Example 3 with TransactionData

use of com.newrelic.agent.TransactionData in project newrelic-java-agent by newrelic.

the class TransactionTraceSampler method noticeTransactionUnderLock.

private boolean noticeTransactionUnderLock(TransactionData td) {
    Long lastScore = tracedTransactions.get(td.getBlameMetricName());
    if (lastScore != null && getScore(td) <= lastScore) {
        return false;
    }
    while (true) {
        TransactionData current = expensiveTransaction.get();
        if (current != null && getScore(current) >= getScore(td)) {
            return false;
        }
        if (expensiveTransaction.compareAndSet(current, td)) {
            if (Agent.LOG.isLoggable(Level.FINER)) {
                String msg = MessageFormat.format("Captured expensive transaction trace for {0} {1}", td.getApplicationName(), td);
                Agent.LOG.finer(msg);
            }
            return true;
        }
    }
}
Also used : TransactionData(com.newrelic.agent.TransactionData)

Example 4 with TransactionData

use of com.newrelic.agent.TransactionData in project newrelic-java-agent by newrelic.

the class TransactionTraceSampler method harvest.

@Override
public List<TransactionTrace> harvest(String appName) {
    TransactionData td = null;
    writeLock.lock();
    try {
        td = harvestUnderLock(appName);
    } finally {
        writeLock.unlock();
    }
    if (td == null) {
        return Collections.emptyList();
    }
    if (Agent.LOG.isLoggable(Level.FINER)) {
        String msg = MessageFormat.format("Sending transaction trace for {0} {1}", td.getApplicationName(), td);
        Agent.LOG.finer(msg);
    }
    TransactionTrace trace = TransactionTrace.getTransactionTrace(td);
    List<TransactionTrace> traces = new ArrayList<>(1);
    traces.add(trace);
    return traces;
}
Also used : ArrayList(java.util.ArrayList) TransactionData(com.newrelic.agent.TransactionData)

Example 5 with TransactionData

use of com.newrelic.agent.TransactionData in project newrelic-java-agent by newrelic.

the class TransactionTraceServiceTest method transactionSamplers.

@Test
public void transactionSamplers() throws Exception {
    Map<String, Object> configMap = createConfigMap();
    Map<String, Object> ttConfigMap = new HashMap<>();
    configMap.put("transaction_tracer", ttConfigMap);
    ttConfigMap.put(TransactionTracerConfigImpl.ENABLED, true);
    ttConfigMap.put(TransactionTracerConfigImpl.COLLECT_TRACES, true);
    createServiceManager(configMap);
    TransactionTraceService ttService = ServiceFactory.getTransactionTraceService();
    ITransactionSampler transactionSampler = new ITransactionSampler() {

        private final List<TransactionData> tds = new ArrayList<>();

        @Override
        public boolean noticeTransaction(TransactionData td) {
            tds.add(td);
            return true;
        }

        @Override
        public List<TransactionTrace> harvest(String appName) {
            List<TransactionTrace> traces = new ArrayList<>();
            for (TransactionData td : tds) {
                TransactionTrace trace = TransactionTrace.getTransactionTrace(td);
                traces.add(trace);
            }
            return traces;
        }

        @Override
        public void stop() {
            tds.clear();
        }
    };
    ITransactionSampler transactionSampler2 = new ITransactionSampler() {

        private final List<TransactionData> tds = new ArrayList<>();

        @Override
        public boolean noticeTransaction(TransactionData td) {
            tds.add(td);
            return true;
        }

        @Override
        public List<TransactionTrace> harvest(String appName) {
            List<TransactionTrace> traces = new ArrayList<>();
            for (TransactionData td : tds) {
                TransactionTrace trace = TransactionTrace.getTransactionTrace(td);
                traces.add(trace);
            }
            return traces;
        }

        @Override
        public void stop() {
            tds.clear();
        }
    };
    ttService.addTransactionTraceSampler(transactionSampler);
    ttService.addTransactionTraceSampler(transactionSampler2);
    TransactionData td = createTransactionData("/en/betting/Baseball/*", APP_NAME, 100L);
    ttService.dispatcherTransactionFinished(td, null);
    ttService.afterHarvest(APP_NAME);
    MockRPMService rpmService = (MockRPMService) ServiceFactory.getRPMServiceManager().getOrCreateRPMService(APP_NAME);
    List<TransactionTrace> traces = rpmService.getTraces();
    Assert.assertEquals(1, traces.size());
    Assert.assertEquals("/en/betting/Baseball/*", traces.get(0).getRequestUri());
    Assert.assertEquals(100L, traces.get(0).getDuration());
    traces.clear();
    traces = rpmService.getTraces();
    Assert.assertEquals(0, traces.size());
    ttService.removeTransactionTraceSampler(transactionSampler);
    ttService.removeTransactionTraceSampler(transactionSampler2);
    ttService.dispatcherTransactionFinished(td, null);
    ttService.afterHarvest(APP_NAME);
    Assert.assertEquals(0, transactionSampler.harvest(APP_NAME).size());
    Assert.assertEquals(0, transactionSampler2.harvest(APP_NAME).size());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TransactionData(com.newrelic.agent.TransactionData) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Aggregations

TransactionData (com.newrelic.agent.TransactionData)145 Test (org.junit.Test)136 TransactionDataList (com.newrelic.agent.TransactionDataList)46 TransactionStats (com.newrelic.agent.stats.TransactionStats)40 HashMap (java.util.HashMap)34 StatsEngine (com.newrelic.agent.stats.StatsEngine)29 Tracer (com.newrelic.agent.tracers.Tracer)23 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)17 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)16 Transaction (com.newrelic.agent.Transaction)15 EventTestHelper.generateTransactionData (com.newrelic.agent.service.analytics.EventTestHelper.generateTransactionData)15 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)15 JSONArray (org.json.simple.JSONArray)15 MockRPMService (com.newrelic.agent.MockRPMService)14 Map (java.util.Map)14 TransactionService (com.newrelic.agent.TransactionService)12 Java7IncompatibleTest (com.newrelic.test.marker.Java7IncompatibleTest)12 JSONObject (org.json.simple.JSONObject)12 TransactionDataTestBuilder (com.newrelic.agent.TransactionDataTestBuilder)10 SpanEvent (com.newrelic.agent.model.SpanEvent)10