Search in sources :

Example 1 with Transaction

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

the class InstrumentationImpl method oldCreateSqlTracer.

// This code path is similar to the 3.16.1 and earlier tracer creation path. It is retained for use by legacy
// async instrumentation, including NAPS (Netty, Akka, Play, Scala) and async servlet instrumentation.
private ExitTracer oldCreateSqlTracer(TransactionActivity txa, Object invocationTarget, int signatureId, String metricName, int 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 OtherRootSqlTracer(txa, sig, invocationTarget, mnf, flags);
            } else if (overSegmentLimit(txa)) {
                logger.log(Level.FINEST, "Transaction has exceeded tracer segment limit. Returning ultralight sql tracer.");
                return UltraLightTracer.createClampedSegment(txa, sig);
            } else {
                tracer = new DefaultSqlTracer(txa, sig, invocationTarget, mnf, flags);
            }
            txa.tracerStarted(tracer);
            Tracer initiatingTracer = (Tracer) tokenAndRefCount.tracedMethod.getAndSet(tracer);
            tx.startFastAsyncWork(txa, initiatingTracer);
            return 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 null;
        }
        if (transaction.getTransactionActivity().isLeaf()) {
            return null;
        }
        ClassMethodSignature sig = ClassMethodSignatures.get().get(signatureId);
        return transaction.getTransactionState().getSqlTracer(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) 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) OtherRootSqlTracer(com.newrelic.agent.tracers.OtherRootSqlTracer) DefaultSqlTracer(com.newrelic.agent.tracers.DefaultSqlTracer)

Example 2 with Transaction

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

the class InvocationPoint method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
        if (ignoreApdex) {
            Transaction transaction = Transaction.getTransaction(false);
            if (transaction != null) {
                Dispatcher dispatcher = transaction.getDispatcher();
                if (dispatcher != null) {
                    dispatcher.setIgnoreApdex(true);
                    if (Agent.LOG.isLoggable(Level.FINER)) {
                        String msg = MessageFormat.format("Set Ignore apdex to \"{0}\"", true);
                        Agent.LOG.log(Level.FINER, msg, new Exception());
                    }
                }
            }
        }
        Object t = tracerService.getTracer(tracerFactory, classMethodSignature, args[0], (Object[]) args[1]);
        return t;
    } catch (Throwable t) {
        Agent.LOG.log(Level.FINEST, "Tracer invocation error", t);
    }
    return null;
}
Also used : Transaction(com.newrelic.agent.Transaction) Dispatcher(com.newrelic.agent.dispatchers.Dispatcher)

Example 3 with Transaction

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

the class AbstractTracer method nameTransaction.

@Override
public void nameTransaction(TransactionNamePriority priority) {
    try {
        ClassMethodSignature classMethodSignature = getClassMethodSignature();
        Object invocationTarget = getInvocationTarget();
        String className = invocationTarget == null ? classMethodSignature.getClassName() : invocationTarget.getClass().getName();
        String txName = className + "/" + classMethodSignature.getMethodName();
        Agent.LOG.log(Level.FINER, "Setting transaction name using instrumented class and method: {0}", txName);
        Transaction tx = transactionActivity.getTransaction();
        tx.setTransactionName(priority, false, customPrefix, txName);
    } catch (Throwable t) {
        Agent.LOG.log(Level.FINEST, "nameTransaction", t);
    }
}
Also used : Transaction(com.newrelic.agent.Transaction)

Example 4 with Transaction

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

the class DefaultSqlTracer method doFinish.

@Override
protected void doFinish(Throwable throwable) {
    super.doFinish(throwable);
    // if an error is thrown, stick the sql up in the transaction parameters so that it'll show up in the traced
    // error
    Object sql = getSql();
    if (sql != null) {
        Transaction transaction = getTransaction();
        if (transaction != null) {
            if (getRecordSql().equals(RecordSql.raw)) {
                // Store the raw query string
                getTransaction().getIntrinsicAttributes().put(SQL_PARAMETER_NAME, sql.toString());
            } else if (getRecordSql().equals(RecordSql.obfuscated)) {
                String appName = getTransaction().getApplicationName();
                SqlQueryConverter converter = new SqlQueryConverter(appName, getDatabaseVendor());
                String obfuscatedQueryString = converter.toObfuscatedQueryString(sql.toString());
                // Store the obfuscated query string
                getTransaction().getIntrinsicAttributes().put(SQL_PARAMETER_NAME, obfuscatedQueryString);
            }
        }
    }
}
Also used : Transaction(com.newrelic.agent.Transaction)

Example 5 with Transaction

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

the class DefaultSqlTracer method doFinish.

@Override
protected void doFinish(int opcode, Object returnValue) {
    super.doFinish(opcode, returnValue);
    Transaction transaction = getTransaction();
    if (transaction != null) {
        TransactionTracerConfig transactionTracerConfig = transaction.getTransactionTracerConfig();
        double explainThresholdInNanos = transactionTracerConfig.getExplainThresholdInNanos();
        // The string equality check here is intentional, both values are interned Strings
        if (SqlObfuscator.RAW_SETTING == transactionTracerConfig.getRecordSql() || getDuration() > explainThresholdInNanos) {
            // we have to copy the parameters because they will be cleared later
            Object[] sqlParameters = params == null ? null : new Object[params.length];
            if (sqlParameters != null) {
                System.arraycopy(params, 0, sqlParameters, 0, params.length);
                params = sqlParameters;
            }
        }
        if (isTransactionSegment() && captureSql()) {
            this.sqlObject = getSql();
        }
        parseStatement(returnValue, transaction.getRPMService().getConnectionTimestamp());
        if (isTransactionSegment() && sql != null) {
            if (transactionTracerConfig.isExplainEnabled()) {
                captureExplain(parsedDatabaseStatement, explainThresholdInNanos, transactionTracerConfig);
            } else {
                if (Agent.isDebugEnabled()) {
                    String msg = MessageFormat.format("Statement exceeded threshold?: {0}", getDuration() > explainThresholdInNanos);
                    Agent.LOG.finer(msg);
                }
            }
        }
    }
}
Also used : Transaction(com.newrelic.agent.Transaction) TransactionTracerConfig(com.newrelic.agent.config.TransactionTracerConfig)

Aggregations

Transaction (com.newrelic.agent.Transaction)367 Test (org.junit.Test)281 Tracer (com.newrelic.agent.tracers.Tracer)85 BasicRequestRootTracer (com.newrelic.agent.tracers.servlet.BasicRequestRootTracer)82 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)59 BrowserConfigTest (com.newrelic.agent.browser.BrowserConfigTest)54 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)46 SimpleMetricNameFormat (com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat)43 HashMap (java.util.HashMap)41 TokenImpl (com.newrelic.agent.TokenImpl)35 StartAndThenLink (com.newrelic.agent.TransactionAsyncUtility.StartAndThenLink)31 MockHttpResponse (com.newrelic.agent.tracers.servlet.MockHttpResponse)25 MockHttpRequest (com.newrelic.agent.tracers.servlet.MockHttpRequest)24 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)20 PriorityTransactionName (com.newrelic.agent.transaction.PriorityTransactionName)19 Trace (com.newrelic.api.agent.Trace)18 TransactionThrowable (com.newrelic.agent.transaction.TransactionThrowable)16 Method (java.lang.reflect.Method)16 NewRelicIgnoreTransaction (test.newrelic.test.agent.TraceAnnotationTest.NewRelicIgnoreTransaction)16 TransactionData (com.newrelic.agent.TransactionData)14