Search in sources :

Example 1 with WrappedTry

use of com.nr.agent.instrumentation.scala.WrappedTry in project newrelic-java-agent by newrelic.

the class CallbackRunnable_Instrumentation method run.

@Trace(excludeFromTransactionTrace = true)
public void run() {
    Segment segment = null;
    WrappedTry wrapped = null;
    boolean remove = false;
    if (value instanceof WrappedTry) {
        wrapped = (WrappedTry) value;
        // If we are here and there is no activeToken in progress we are the first one so we set this boolean in
        // order to correctly remove the "activeToken" from the thread local after the original run() method executes
        remove = AgentBridge.activeToken.get() == null;
        AgentBridge.activeToken.set(wrapped.tokenAndRefCount);
        if (scalaFuturesAsSegments && remove) {
            Transaction tx = AgentBridge.getAgent().getTransaction(false);
            if (tx != null) {
                segment = tx.startSegment("Scala", "Callback");
                segment.setMetricName("Scala", "Callback", ScalaUtils.nameScalaFunction(onComplete.getClass().getName()));
            }
        }
        value = wrapped.original;
    }
    try {
        Weaver.callOriginal();
    } finally {
        if (wrapped != null) {
            if (segment != null) {
                segment.end();
            }
            if (remove) {
                AgentBridge.activeToken.remove();
            }
            if (wrapped.tokenAndRefCount.refCount.decrementAndGet() == 0) {
                wrapped.tokenAndRefCount.token.expire();
                wrapped.tokenAndRefCount.token = null;
            }
        }
    }
}
Also used : WrappedTry(com.nr.agent.instrumentation.scala.WrappedTry) Transaction(com.newrelic.agent.bridge.Transaction) Segment(com.newrelic.api.agent.Segment) Trace(com.newrelic.api.agent.Trace)

Aggregations

Transaction (com.newrelic.agent.bridge.Transaction)1 Segment (com.newrelic.api.agent.Segment)1 Trace (com.newrelic.api.agent.Trace)1 WrappedTry (com.nr.agent.instrumentation.scala.WrappedTry)1