Search in sources :

Example 41 with OtherRootTracer

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

the class JsonTracer method generateRootBackgroundTracer.

private DefaultTracer generateRootBackgroundTracer(Long initTime) {
    long time = getStartTime(initTime);
    ClassMethodSignature sig = new ClassMethodSignature("clazz", tracerName, "()");
    Object reference = new Object();
    int flags = TracerFlags.DISPATCHER;
    if (startTimeNs != 0) {
        flags = TracerFlags.ASYNC;
    }
    DefaultTracer tracer = new OtherRootTracer(Transaction.getTransaction().getTransactionActivity(), sig, reference, new ClassMethodMetricNameFormat(sig, reference), flags, time);
    Transaction.getTransaction().getTransactionActivity().tracerStarted(tracer);
    return tracer;
}
Also used : ClassMethodMetricNameFormat(com.newrelic.agent.tracers.metricname.ClassMethodMetricNameFormat) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) JSONObject(org.json.simple.JSONObject) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer)

Example 42 with OtherRootTracer

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

the class InstrumentationImpl method oldCreateTracer.

// This code path is similar to the 3.16.1 and earlier tracer creation path. It is retained for use by legacy async
// instrumentation: Play1 and async servlet 3.0 instrumentation. The key difference from the "fast path" is that
// this path switches on the TransactionState during creation.
private ExitTracer oldCreateTracer(TransactionActivity txa, Object invocationTarget, int signatureId, String metricName, int flags) {
    // oldCreateTracer behavior below.
    if (txa == null) {
        AgentBridge.TokenAndRefCount tokenAndRefCount = AgentBridge.activeToken.get();
        if (tokenAndRefCount != null && tokenAndRefCount.token != null) {
            // Fast path for scala instrumentation (and potentially others in the future)
            Transaction tx = Transaction.getTransaction(false);
            if (tx == null) {
                if (tokenAndRefCount.token.getTransaction() instanceof Transaction) {
                    tx = (Transaction) tokenAndRefCount.token.getTransaction();
                } else {
                    return null;
                }
            }
            txa = TransactionActivity.create(tx, Integer.MAX_VALUE);
            flags = flags | TracerFlags.ASYNC;
            ClassMethodSignature sig = ClassMethodSignatures.get().get(signatureId);
            MetricNameFormat mnf = MetricNameFormats.getFormatter(invocationTarget, sig, metricName, flags);
            Tracer tracer;
            if (TracerFlags.isRoot(flags)) {
                // Dispatcher || Async
                tracer = new OtherRootTracer(txa, sig, invocationTarget, mnf, flags);
            } else {
                tracer = new DefaultTracer(txa, sig, invocationTarget, mnf, flags);
            }
            txa.tracerStarted(tracer);
            Tracer initiatingTracer = (Tracer) tokenAndRefCount.tracedMethod.getAndSet(tracer);
            tx.startFastAsyncWork(txa, initiatingTracer);
            return noticeTracer(signatureId, flags, tracer);
        } else if (TracerFlags.isAsync(flags)) {
            txa = TransactionActivity.create(null, Integer.MAX_VALUE);
            return startTracer(txa, invocationTarget, signatureId, metricName, flags);
        }
    }
    // Avoid creating tracers for NoOpTransaction, etc.
    com.newrelic.agent.Transaction transaction = com.newrelic.agent.Transaction.getTransaction(TracerFlags.isDispatcher(flags));
    if (transaction == null) {
        return null;
    }
    try {
        if (!TracerFlags.isDispatcher(flags) && !transaction.isStarted()) {
            // if we're not in a transaction and this isn't a dispatcher tracer, bail before we create objects
            return noticeTracer(signatureId, flags, null);
        }
        if (transaction.getTransactionActivity().isLeaf()) {
            return null;
        }
        ClassMethodSignature sig = ClassMethodSignatures.get().get(signatureId);
        return transaction.getTransactionState().getTracer(transaction, invocationTarget, sig, metricName, flags);
    } catch (Throwable t) {
        logger.log(Level.FINEST, t, "createTracer({0}, {1}, {2}, {3})", invocationTarget, signatureId, metricName, flags);
        return null;
    }
}
Also used : Transaction(com.newrelic.agent.Transaction) NoOpTransaction(com.newrelic.agent.bridge.NoOpTransaction) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) ExitTracer(com.newrelic.agent.bridge.ExitTracer) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) UltraLightTracer(com.newrelic.agent.tracers.UltraLightTracer) DefaultSqlTracer(com.newrelic.agent.tracers.DefaultSqlTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootSqlTracer(com.newrelic.agent.tracers.OtherRootSqlTracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) AgentBridge(com.newrelic.agent.bridge.AgentBridge) Transaction(com.newrelic.agent.Transaction) MetricNameFormat(com.newrelic.agent.tracers.metricname.MetricNameFormat) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer)

Example 43 with OtherRootTracer

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

the class InstrumentationImpl method createTracer.

/**
 * Optimized createTracer call for weaved and XML instrumentation. We do not know if either a TransactionActivity or
 * a Transaction is present on the thread. If present, we do not know if the Transaction has been started.
 */
@Override
public ExitTracer createTracer(Object invocationTarget, int signatureId, String metricName, int flags) {
    try {
        if (ServiceFactory.getServiceManager().isStopped()) {
            return null;
        }
        if (ServiceFactory.getServiceManager().getCircuitBreakerService().isTripped()) {
            return null;
        }
        TransactionActivity txa = TransactionActivity.get();
        if (txa != null) {
            if (txa.getRootTracer() != null && txa.getRootTracer().isAsync() && txa.getTransaction() == null) {
                txa = null;
            }
        }
        if (!Agent.canFastPath()) {
            // legacy async instrumentation is in use
            return oldCreateTracer(txa, invocationTarget, signatureId, metricName, flags);
        }
        if (txa == null) {
            AgentBridge.TokenAndRefCount tokenAndRefCount = AgentBridge.activeToken.get();
            if (tokenAndRefCount != null && tokenAndRefCount.token != null) {
                // Fast path for scala instrumentation (and potentially others in the future)
                Transaction tx = Transaction.getTransaction(false);
                if (tx == null) {
                    if (tokenAndRefCount.token.getTransaction() instanceof Transaction) {
                        tx = (Transaction) tokenAndRefCount.token.getTransaction();
                    } else {
                        return null;
                    }
                }
                txa = TransactionActivity.create(tx, Integer.MAX_VALUE);
                flags = flags | TracerFlags.ASYNC;
                ClassMethodSignature sig = ClassMethodSignatures.get().get(signatureId);
                MetricNameFormat mnf = MetricNameFormats.getFormatter(invocationTarget, sig, metricName, flags);
                Tracer tracer;
                if (TracerFlags.isRoot(flags)) {
                    // Dispatcher || Async
                    tracer = new OtherRootTracer(txa, sig, invocationTarget, mnf, flags);
                } else {
                    tracer = new DefaultTracer(txa, sig, invocationTarget, mnf, flags);
                }
                txa.tracerStarted(tracer);
                Tracer initiatingTracer = (Tracer) AgentBridge.activeToken.get().tracedMethod.getAndSet(tracer);
                tx.startFastAsyncWork(txa, initiatingTracer);
                return noticeTracer(signatureId, flags, tracer);
            } else if (TracerFlags.isDispatcher(flags)) {
                // Traditional first-time creation of a new transaction
                com.newrelic.agent.Transaction.getTransaction(true);
                txa = TransactionActivity.get();
            } else if (TracerFlags.isAsync(flags)) {
                // Create a transaction activity without a transaction
                txa = TransactionActivity.create(null, Integer.MAX_VALUE);
            }
            if (txa == null) {
                // or we are running on an Agent thread.
                return noticeTracer(signatureId, flags, null);
            }
            return noticeTracer(signatureId, flags, startTracer(txa, invocationTarget, signatureId, metricName, flags));
        }
        // traditional tracer creation code, we check it before creating any objects.
        if (!TracerFlags.isRoot(flags) && !txa.isStarted()) {
            return noticeTracer(signatureId, flags, null);
        }
        Tracer result = null;
        if (txa.checkTracerStart()) {
            try {
                ClassMethodSignature sig = ClassMethodSignatures.get().get(signatureId);
                // Metric naming. When we come here from an @Trace that doesn't specify a metric name, a
                // very common case, getFormatter() will return one of a couple of reusable instances of
                // MetricNameFormat, so we avoid proliferating small objects. But when we come here from
                // XML instrumentation, getFormatter() is forced to create a new MNF instance every time.
                // This proved to be messy to optimize, so unfortunately has been left as-is. 2015-05.
                MetricNameFormat mnf = MetricNameFormats.getFormatter(invocationTarget, sig, metricName, flags);
                if (TracerFlags.isDispatcher(flags) || (TracerFlags.isAsync(flags) && txa.getTransaction() != null && !txa.isStarted())) {
                    result = new OtherRootTracer(txa, sig, invocationTarget, mnf);
                } else {
                    result = new DefaultTracer(txa, sig, invocationTarget, mnf, flags);
                }
            } finally {
                txa.unlockTracerStart();
            }
            txa.tracerStarted(result);
        }
        return noticeTracer(signatureId, flags, result);
    } catch (Throwable t) {
        logger.log(Level.FINEST, t, "createTracer({0}, {1}, {2}, {3})", invocationTarget, signatureId, metricName, flags);
        return null;
    }
}
Also used : Transaction(com.newrelic.agent.Transaction) NoOpTransaction(com.newrelic.agent.bridge.NoOpTransaction) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) ExitTracer(com.newrelic.agent.bridge.ExitTracer) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) UltraLightTracer(com.newrelic.agent.tracers.UltraLightTracer) DefaultSqlTracer(com.newrelic.agent.tracers.DefaultSqlTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootSqlTracer(com.newrelic.agent.tracers.OtherRootSqlTracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) AgentBridge(com.newrelic.agent.bridge.AgentBridge) TransactionActivity(com.newrelic.agent.TransactionActivity) MetricNameFormat(com.newrelic.agent.tracers.metricname.MetricNameFormat) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer)

Example 44 with OtherRootTracer

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

the class AgentImplTest method testAsyncTracerShouldNotStartTxn.

@Test
public void testAsyncTracerShouldNotStartTxn() {
    TransactionActivity txa = TransactionActivity.create(null, 0);
    ClassMethodSignature sig = new ClassMethodSignature("class", "method", "methodDesc");
    OtherRootTracer tracer = new OtherRootTracer(txa, sig, null, null, TracerFlags.ASYNC, System.nanoTime());
    txa.tracerStarted(tracer);
    Transaction txn = agentImpl.getTransaction();
    Assert.assertTrue(txn instanceof NoOpTransaction);
    Assert.assertFalse(txn instanceof com.newrelic.agent.Transaction);
}
Also used : ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) Transaction(com.newrelic.agent.bridge.Transaction) NoOpTransaction(com.newrelic.agent.bridge.NoOpTransaction) NoOpTransaction(com.newrelic.agent.bridge.NoOpTransaction) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) Test(org.junit.Test)

Example 45 with OtherRootTracer

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

the class WebRequestDispatcherTest method createDispatcher.

private WebRequestDispatcher createDispatcher(MockHttpRequest httpRequest) throws Exception {
    Transaction tx = Transaction.getTransaction();
    Response httpResponse = new MockHttpResponse();
    ClassMethodSignature sig = new ClassMethodSignature(getClass().getName(), "methodName", "()V");
    DefaultTracer tracer = new OtherRootTracer(tx, sig, this, new SimpleMetricNameFormat("test"));
    tx.getTransactionActivity().tracerStarted(tracer);
    WebRequestDispatcher dispatcher = new WebRequestDispatcher(httpRequest, httpResponse, tx);
    tx.setDispatcher(dispatcher);
    return dispatcher;
}
Also used : MockHttpResponse(com.newrelic.agent.tracers.servlet.MockHttpResponse) Response(com.newrelic.api.agent.Response) Transaction(com.newrelic.agent.Transaction) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) SimpleMetricNameFormat(com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat) MockHttpResponse(com.newrelic.agent.tracers.servlet.MockHttpResponse) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer)

Aggregations

OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)51 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)45 Transaction (com.newrelic.agent.Transaction)35 SimpleMetricNameFormat (com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat)34 Test (org.junit.Test)31 BrowserConfigTest (com.newrelic.agent.browser.BrowserConfigTest)17 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)15 Tracer (com.newrelic.agent.tracers.Tracer)15 MockHttpResponse (com.newrelic.agent.tracers.servlet.MockHttpResponse)11 Response (com.newrelic.api.agent.Response)10 MockHttpRequest (com.newrelic.agent.tracers.servlet.MockHttpRequest)9 MetricNameFormat (com.newrelic.agent.tracers.metricname.MetricNameFormat)8 ExtendedRequest (com.newrelic.api.agent.ExtendedRequest)8 Request (com.newrelic.api.agent.Request)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)8 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)8 MockHttpServletRequest (org.apache.struts.mock.MockHttpServletRequest)8 MockHttpServletResponse (org.apache.struts.mock.MockHttpServletResponse)8 TransactionDataList (com.newrelic.agent.TransactionDataList)6