Search in sources :

Example 11 with MetricNameFormat

use of com.newrelic.agent.tracers.metricname.MetricNameFormat 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 12 with MetricNameFormat

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

the class InstrumentationImpl method startSqlTracer.

private Tracer startSqlTracer(TransactionActivity txa, Object target, int signatureId, String metricName, int flags) {
    ClassMethodSignature sig = ClassMethodSignatures.get().get(signatureId);
    MetricNameFormat mnf = MetricNameFormats.getFormatter(target, sig, metricName, flags);
    Tracer tracer;
    if (TracerFlags.isRoot(flags)) {
        // Dispatcher || Async
        tracer = new OtherRootSqlTracer(txa, sig, target, mnf, flags, System.nanoTime());
    } else {
        tracer = new DefaultSqlTracer(txa, sig, target, mnf, flags);
    }
    txa.tracerStarted(tracer);
    return tracer;
}
Also used : 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) MetricNameFormat(com.newrelic.agent.tracers.metricname.MetricNameFormat) OtherRootSqlTracer(com.newrelic.agent.tracers.OtherRootSqlTracer) DefaultSqlTracer(com.newrelic.agent.tracers.DefaultSqlTracer)

Example 13 with MetricNameFormat

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

the class InstrumentationImpl method createSqlTracer.

// I don't like having this method be a copy/paste of the createTracer method above but I do not
// want to introduce a performance hit for the default tracer path just to support sql tracers.
@Override
public ExitTracer createSqlTracer(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 oldCreateSqlTracer(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 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.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 null;
            }
            return startSqlTracer(txa, invocationTarget, signatureId, metricName, flags);
        }
        // traditional tracer creation code, we check it before creating any objects.
        if (!TracerFlags.isRoot(flags) && !txa.isStarted()) {
            return 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 OtherRootSqlTracer(txa, sig, invocationTarget, mnf);
                } else if (overSegmentLimit(txa)) {
                    logger.log(Level.FINEST, "Transaction has exceeded tracer segment limit. Returning ultralight sql tracer.");
                    result = UltraLightTracer.createClampedSegment(txa, sig);
                } else {
                    result = new DefaultSqlTracer(txa, sig, invocationTarget, mnf, flags);
                }
            } finally {
                txa.unlockTracerStart();
            }
            txa.tracerStarted(result);
        }
        return 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) 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) OtherRootSqlTracer(com.newrelic.agent.tracers.OtherRootSqlTracer) DefaultSqlTracer(com.newrelic.agent.tracers.DefaultSqlTracer)

Example 14 with MetricNameFormat

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

the class DefaultTracer method setMetricNameFormatInfo.

@Override
public void setMetricNameFormatInfo(String metricName, String transactionSegmentName, String transactionSegmentUri) {
    MetricNameFormat format = new SimpleMetricNameFormat(metricName, transactionSegmentName, transactionSegmentUri);
    setMetricNameFormat(format);
}
Also used : SimpleMetricNameFormat(com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat) MetricNameFormat(com.newrelic.agent.tracers.metricname.MetricNameFormat) SimpleMetricNameFormat(com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat)

Example 15 with MetricNameFormat

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

the class AbstractExternalComponentTracer method finish.

@Override
public void finish(Throwable throwable) {
    if (throwable instanceof UnknownHostException) {
        host = UNKNOWN_HOST;
        MetricNameFormat metricNameFormat = getMetricNameFormat();
        if (metricNameFormat instanceof ExternalComponentNameFormat) {
            setMetricNameFormat(((ExternalComponentNameFormat) metricNameFormat).cloneWithNewHost(UNKNOWN_HOST));
        }
    }
    super.finish(throwable);
}
Also used : UnknownHostException(java.net.UnknownHostException) MetricNameFormat(com.newrelic.agent.tracers.metricname.MetricNameFormat)

Aggregations

MetricNameFormat (com.newrelic.agent.tracers.metricname.MetricNameFormat)21 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)13 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)12 Tracer (com.newrelic.agent.tracers.Tracer)11 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)10 Transaction (com.newrelic.agent.Transaction)8 DefaultSqlTracer (com.newrelic.agent.tracers.DefaultSqlTracer)8 OtherRootSqlTracer (com.newrelic.agent.tracers.OtherRootSqlTracer)8 UltraLightTracer (com.newrelic.agent.tracers.UltraLightTracer)8 SimpleMetricNameFormat (com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat)7 Test (org.junit.Test)7 ExitTracer (com.newrelic.agent.bridge.ExitTracer)6 AgentBridge (com.newrelic.agent.bridge.AgentBridge)4 NoOpTransaction (com.newrelic.agent.bridge.NoOpTransaction)4 TransactionActivity (com.newrelic.agent.TransactionActivity)3 ClassMethodMetricNameFormat (com.newrelic.agent.tracers.metricname.ClassMethodMetricNameFormat)3 DefaultMetricNameFormat (com.newrelic.agent.tracers.metricname.DefaultMetricNameFormat)3 SkipTracer (com.newrelic.agent.tracers.SkipTracer)2 BasicRequestRootTracer (com.newrelic.agent.tracers.servlet.BasicRequestRootTracer)2 JSONObject (org.json.simple.JSONObject)2