Search in sources :

Example 6 with Transaction

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

the class CallbackRunnable_Instrumentation method value_$eq.

/**
 * Override the setter for "value" so we can replace it with our wrapped version
 */
public void value_$eq(Try<T> original) {
    Weaver.callOriginal();
    AgentBridge.TokenAndRefCount tokenAndRefCount = AgentBridge.activeToken.get();
    if (tokenAndRefCount == null) {
        Transaction tx = AgentBridge.getAgent().getTransaction(false);
        if (tx != null) {
            AgentBridge.TokenAndRefCount tokenAndRef = new AgentBridge.TokenAndRefCount(tx.getToken(), AgentBridge.getAgent().getTracedMethod(), new AtomicInteger(1));
            value = new WrappedTry<>(value, tokenAndRef);
        }
    } else {
        value = new WrappedTry<>(value, tokenAndRefCount);
        tokenAndRefCount.refCount.incrementAndGet();
    }
}
Also used : Transaction(com.newrelic.agent.bridge.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AgentBridge(com.newrelic.agent.bridge.AgentBridge)

Example 7 with Transaction

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

the class FutureInnerClass_Instrumentation method apply.

public <T> scala.concurrent.Future<T> apply(Function0<T> body, final ExecutionContext executor) {
    AgentBridge.TokenAndRefCount tokenAndRefCount = AgentBridge.activeToken.get();
    if (tokenAndRefCount == null) {
        Transaction tx = AgentBridge.getAgent().getTransaction(false);
        if (tx != null && (!body.getClass().getName().startsWith("akka."))) {
            AgentBridge.TokenAndRefCount tokenAndRef = new AgentBridge.TokenAndRefCount(tx.getToken(), AgentBridge.getAgent().getTracedMethod(), new AtomicInteger(1));
            body = new WrappedFunction0(body, tokenAndRef);
        }
    } else {
        body = new WrappedFunction0(body, tokenAndRefCount);
        tokenAndRefCount.refCount.incrementAndGet();
    }
    scala.concurrent.Future<T> value = Weaver.callOriginal();
    return value;
}
Also used : Transaction(com.newrelic.agent.bridge.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AgentBridge(com.newrelic.agent.bridge.AgentBridge) WrappedFunction0(com.nr.agent.instrumentation.scala.WrappedFunction0)

Example 8 with Transaction

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

the class Transformation_Instrumentation method run.

@Trace(excludeFromTransactionTrace = true)
public void run() {
    Segment segment = null;
    boolean remove = false;
    if (tokenAndRefCount != null) {
        // 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(tokenAndRefCount);
        // getTransaction implicitly makes Transaction available on Thread to runnable
        Transaction tx = AgentBridge.getAgent().getTransaction(false);
        if (scalaFuturesAsSegments && remove) {
            if (tx != null) {
                segment = tx.startSegment("Scala", "Callback");
                segment.setMetricName("Scala", "Callback", ScalaUtils.nameScalaFunction(_arg.getClass().getName()));
            }
        }
    }
    try {
        Weaver.callOriginal();
    } finally {
        if (tokenAndRefCount != null) {
            if (segment != null) {
                segment.end();
            }
            if (remove) {
                AgentBridge.activeToken.remove();
            }
            if (tokenAndRefCount.refCount.decrementAndGet() == 0) {
                tokenAndRefCount.token.expire();
                tokenAndRefCount.token = null;
            }
        }
    }
}
Also used : Transaction(com.newrelic.agent.bridge.Transaction) Segment(com.newrelic.api.agent.Segment) Trace(com.newrelic.api.agent.Trace)

Example 9 with Transaction

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

the class AgentImpl method getTracedMethod.

/**
 * If in a transaction, then getTransaction().getTracedMethod() returns the same thing as this method. If outside a
 * transaction then this method returns a noop. Note: The getTransaction().getTracedMethod() will return null.
 */
@Override
public TracedMethod getTracedMethod() {
    com.newrelic.agent.bridge.Transaction transaction = getTransaction(false);
    if (NoOpTransaction.INSTANCE.equals(transaction)) {
        return NoOpTracedMethod.INSTANCE;
    }
    com.newrelic.agent.Transaction txn = com.newrelic.agent.Transaction.getTransaction(false);
    // TransactionApiImpl to ensure the same behavior.
    if (txn == null) {
        return NoOpTracedMethod.INSTANCE;
    }
    TransactionActivity txa = txn.getTransactionActivity();
    if (txa == null) {
        return NoOpTracedMethod.INSTANCE;
    }
    Tracer tracer = txa.getLastTracer();
    return (tracer == null) ? NoOpTracedMethod.INSTANCE : tracer;
}
Also used : Tracer(com.newrelic.agent.tracers.Tracer) Transaction(com.newrelic.agent.bridge.Transaction)

Example 10 with Transaction

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

the class Cell_Instrumentation method sendMessage.

@Trace
public void sendMessage(Envelope_Instrumentation envelope) {
    String receiver = props().actorClass().getName();
    String messageClassName = envelope.message().getClass().getName();
    Transaction transaction = AgentBridge.getAgent().getTransaction(false);
    if (transaction != null && transaction.isStarted() && !AkkaUtil.isHeartBeatMessage(messageClassName) && !AkkaUtil.isLogger(receiver) && !AkkaUtil.isAkkaStream(messageClassName)) {
        String sender = AkkaUtil.getActor(envelope.sender());
        AkkaUtil.recordTellMetric(receiver, sender, messageClassName);
        if (envelope.token != null) {
            // Akka may migrate envelopes to another ActorCell.
            // See UnstartedCell in RepointableActorRef.scala.
            // We expire and replace the existing token just to be on the safe side.
            envelope.token.expire();
            // this prevents the warning even though it's immediately reassigned
            envelope.token = null;
        }
        envelope.token = transaction.getToken();
    }
    Weaver.callOriginal();
}
Also used : Transaction(com.newrelic.agent.bridge.Transaction) Trace(com.newrelic.api.agent.Trace)

Aggregations

Transaction (com.newrelic.agent.bridge.Transaction)40 Trace (com.newrelic.api.agent.Trace)18 TracedMethod (com.newrelic.agent.bridge.TracedMethod)7 AgentBridge (com.newrelic.agent.bridge.AgentBridge)4 WeaveIntoAllMethods (com.newrelic.api.agent.weaver.WeaveIntoAllMethods)4 WeaveWithAnnotation (com.newrelic.api.agent.weaver.WeaveWithAnnotation)4 URI (java.net.URI)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Segment (com.newrelic.api.agent.Segment)3 Label (org.objectweb.asm.Label)3 Method (org.objectweb.asm.commons.Method)3 TracedMethod (com.newrelic.api.agent.TracedMethod)2 MuleHttpConnectorRequest (com.nr.agent.instrumentation.mule3.MuleHttpConnectorRequest)2 OutboundWrapper (com.nr.instrumentation.jersey.client.OutboundWrapper)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 GET (javax.ws.rs.GET)2 HEAD (javax.ws.rs.HEAD)2 PUT (javax.ws.rs.PUT)2 Test (org.junit.Test)2