Search in sources :

Example 1 with TransactionCounts

use of com.newrelic.agent.transaction.TransactionCounts in project newrelic-java-agent by newrelic.

the class Transaction method finishTransaction.

private void finishTransaction() {
    try {
        synchronized (lock) {
            // this may have the side-effect of ignoring the transaction
            freezeTransactionName();
            if (ignore) {
                Agent.LOG.log(Level.FINER, "Transaction {0} was cancelled: ignored. This is not an error condition.", this);
                ServiceFactory.getTransactionService().transactionCancelled(this);
                return;
            }
            if (finishedChildren.isEmpty()) {
                Agent.LOG.log(Level.FINER, "Transaction {0} was cancelled: no activities. This is not an error condition.", this);
                ServiceFactory.getTransactionService().transactionCancelled(this);
                return;
            }
            // this needs to go before dispatcher.transactionFinished so that all of
            // the time metrics are correct
            TransactionStats transactionStats = transactionFinishedActivityMerging();
            transactionTime.markTransactionAsDone();
            recordFinalGCTime(transactionStats);
            handleTokenTimeout(transactionStats);
            String txName = priorityTransactionName.getName();
            // parse headers in dispatcher request before we get rid of request/response objects
            getInboundHeaderState();
            // this may trigger this dispatcher to record extra metrics like apdex & HttpDispatcher
            dispatcher.transactionFinished(txName, transactionStats);
            if (Agent.LOG.isFinerEnabled()) {
                String requestURI = dispatcher == null ? "No Dispatcher Defined" : dispatcher.getUri();
                Agent.LOG.log(Level.FINER, "Transaction {0} for request: {1} finished {2}ms {3}", txName, requestURI, transactionTime.getResponseTimeInMilliseconds(), this);
            }
            if (!ServiceFactory.getServiceManager().isStarted()) {
                Agent.LOG.log(Level.INFO, "Transaction {0} tried to finish but ServiceManager not started", this);
                return;
            }
            // Some parts of the code below are only required if this transaction's TT is selected
            // for sending upstream. Unfortunately we don't know at this point in the harvest whether
            // this transaction's trace will be selected, and there's no obvious way to know that can
            // be built with maintainable code. There was a previous effort at this, but the obvious
            // happened: it sat broken in the code (i.e. it did some checks, but always returned true)
            // for 18 months before we noticed and ripped it out. Please don't repeat this mistake.
            TransactionTracerConfig ttConfig = getTransactionTracerConfig();
            TransactionCounts rootCounts = getTransactionCounts();
            if (rootCounts.isOverTracerSegmentLimit()) {
                getIntrinsicAttributes().put(AttributeNames.SEGMENT_CLAMP, rootCounts.getSegmentCount());
                // Record supportability metric to track when a segment clamp occurs
                transactionStats.getUnscopedStats().getStats(MetricNames.SUPPORTABILITY_TRANSACTION_SEGMENT_CLAMP).recordDataPoint(rootCounts.getSegmentCount());
            }
            if (rootCounts.isOverTransactionSize()) {
                getIntrinsicAttributes().put(AttributeNames.SIZE_LIMIT_PARAMETER_NAME, "The transaction size limit was reached");
            }
            int count = rootCounts.getStackTraceCount();
            if (count >= ttConfig.getMaxStackTraces()) {
                getIntrinsicAttributes().put(AttributeNames.STACK_TRACE_CLAMP, count);
            }
            if (rootCounts.isOverTokenLimit()) {
                getIntrinsicAttributes().put(AttributeNames.TOKEN_CLAMP, rootCounts.getTokenCount());
            }
            count = rootCounts.getExplainPlanCount();
            if (count >= ttConfig.getMaxExplainPlans()) {
                getIntrinsicAttributes().put(AttributeNames.EXPLAIN_PLAN_CLAMP, count);
            }
            DistributedTracingConfig distributedTracingConfig = getAgentConfig().getDistributedTracingConfig();
            if (!distributedTracingConfig.isEnabled()) {
                if (getInboundHeaderState().isTrustedCatRequest()) {
                    String id = getInboundHeaderState().getClientCrossProcessId();
                    getIntrinsicAttributes().put(AttributeNames.CLIENT_CROSS_PROCESS_ID_PARAMETER_NAME, id);
                }
                String referrerGuid = getInboundHeaderState().getReferrerGuid();
                if (referrerGuid != null) {
                    getIntrinsicAttributes().put(AttributeNames.REFERRING_TRANSACTION_TRACE_ID_PARAMETER_NAME, referrerGuid);
                }
                String tripId = getCrossProcessTransactionState().getTripId();
                if (tripId != null) {
                    getIntrinsicAttributes().put(AttributeNames.TRIP_ID_PARAMETER_NAME, tripId);
                    int pathHash = getCrossProcessTransactionState().generatePathHash();
                    getIntrinsicAttributes().put(AttributeNames.PATH_HASH_PARAMETER_NAME, ServiceUtils.intToHexString(pathHash));
                }
            }
            if (isSynthetic()) {
                Agent.LOG.log(Level.FINEST, "Completing Synthetics transaction for monitor {0}", getInboundHeaderState().getSyntheticsMonitorId());
                getIntrinsicAttributes().put(AttributeNames.SYNTHETICS_RESOURCE_ID, this.getInboundHeaderState().getSyntheticsResourceId());
                getIntrinsicAttributes().put(AttributeNames.SYNTHETICS_MONITOR_ID, this.getInboundHeaderState().getSyntheticsMonitorId());
                getIntrinsicAttributes().put(AttributeNames.SYNTHETICS_JOB_ID, this.getInboundHeaderState().getSyntheticsJobId());
            }
            if (timeoutCause != null && timeoutCause.cause != null) {
                getIntrinsicAttributes().put(AttributeNames.TIMEOUT_CAUSE, timeoutCause.cause);
            }
            String displayHost = getAgentConfig().getValue("process_host.display_name", null);
            if (displayHost != null) {
                getAgentAttributes().put(AttributeNames.DISPLAY_HOST, displayHost);
            }
            String instanceName = ServiceFactory.getEnvironmentService().getEnvironment().getAgentIdentity().getInstanceName();
            if (instanceName != null) {
                getAgentAttributes().put(AttributeNames.INSTANCE_NAME, instanceName);
            }
            // Only add jvm.thread_name if transaction  was not timed out and
            // if it's *NOT* a multi-threaded transaction
            TimedSet<TokenImpl> tokenCache = activeTokensCache.get();
            if ((tokenCache == null || tokenCache.timedOutCount() == 0) && finishedChildren.size() == 1) {
                if (!ServiceFactory.getThreadService().isAgentThreadId(Thread.currentThread().getId())) {
                    getAgentAttributes().put(AttributeNames.THREAD_NAME, Thread.currentThread().getName());
                }
            }
            getIntrinsicAttributes().put(AttributeNames.PRIORITY, getPriority());
            TransactionData transactionData = new TransactionData(this, rootCounts.getTransactionSize());
            ServiceFactory.getTransactionService().transactionFinished(transactionData, transactionStats);
        }
    } catch (Throwable th) {
        Agent.LOG.log(Level.WARNING, th, "Transaction {0} was not reported because of an internal error.", this);
        ServiceFactory.getTransactionService().transactionCancelled(this);
    }
}
Also used : DistributedTracingConfig(com.newrelic.agent.config.DistributedTracingConfig) TransactionStats(com.newrelic.agent.stats.TransactionStats) TransactionThrowable(com.newrelic.agent.transaction.TransactionThrowable) TransactionCounts(com.newrelic.agent.transaction.TransactionCounts) TransactionTracerConfig(com.newrelic.agent.config.TransactionTracerConfig)

Example 2 with TransactionCounts

use of com.newrelic.agent.transaction.TransactionCounts in project newrelic-java-agent by newrelic.

the class TransactionStateImplTest method tracerGeneration_segmentClampInPlace_afterRoot.

@Test
public void tracerGeneration_segmentClampInPlace_afterRoot() throws Exception {
    Tracer rootTracer = createRootTracer();
    Transaction tx = Transaction.getTransaction();
    TransactionCounts transactionCounts = tx.getTransactionCounts();
    TracerFactory tracerFactory = Mockito.mock(TracerFactory.class, new Returns(rootTracer));
    tx.getTransactionState().getTracer(tx, tracerFactory, null, null, (Object[]) null);
    assertEquals(rootTracer, tx.getTransactionActivity().getRootTracer());
    transactionCounts.addTracers(3001);
    ClassMethodSignature sig = new ClassMethodSignature("com.test.Dude", "dude1", "()V");
    Tracer tracer = tx.getTransactionState().getTracer(tx, null, sig, null, simpleFlags);
    assertNotEquals(UltraLightTracer.class, tracer.getClass());
    assertEquals("Java/com.test.Dude/dude1", tracer.getTransactionSegmentName());
    Tracer sqlTracer = tx.getTransactionState().getSqlTracer(tx, null, sig, null, simpleFlags);
    assertEquals(UltraLightTracer.class, sqlTracer.getClass());
    assertEquals("Clamped/com.test.Dude/dude1", sqlTracer.getTransactionSegmentName());
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) UltraLightTracer(com.newrelic.agent.tracers.UltraLightTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) TransactionCounts(com.newrelic.agent.transaction.TransactionCounts) TracerFactory(com.newrelic.agent.tracers.TracerFactory) Test(org.junit.Test)

Example 3 with TransactionCounts

use of com.newrelic.agent.transaction.TransactionCounts in project newrelic-java-agent by newrelic.

the class CrossProcessStateCatApiTest method setUpTransaction.

private void setUpTransaction(Transaction tx, TransactionActivity txa, Object lock, Dispatcher dispatcher, CrossProcessConfig config, String guid) {
    when(txa.getTransaction()).thenReturn(tx);
    when(tx.getLock()).thenReturn(lock);
    when(tx.getDispatcher()).thenReturn(dispatcher);
    when(tx.getCrossProcessConfig()).thenReturn(config);
    DistributedTracePayloadImpl distributedTracePayload = DistributedTracePayloadImpl.createDistributedTracePayload("", "", "", 0f);
    when(tx.createDistributedTracePayload(guid)).thenReturn(distributedTracePayload);
    TransactionStats transactionStats = Mockito.mock(TransactionStats.class);
    SimpleStatsEngine stats = Mockito.mock(SimpleStatsEngine.class);
    when(stats.getOrCreateResponseTimeStats(anyString())).thenReturn(Mockito.mock(ResponseTimeStats.class));
    when(transactionStats.getUnscopedStats()).thenReturn(stats);
    when(txa.getTransactionStats()).thenReturn(transactionStats);
    when(tx.getTransactionActivity()).thenReturn(txa);
    InboundHeaders headers = Mockito.mock(InboundHeaders.class);
    InboundHeaderState inboundHeaderState = new InboundHeaderState(tx, headers);
    when(tx.getInboundHeaderState()).thenReturn(inboundHeaderState);
    PriorityTransactionName priorityTransactionName = PriorityTransactionName.create("Something/Or/other", "category", TransactionNamePriority.FRAMEWORK);
    when(tx.getPriorityTransactionName()).thenReturn(priorityTransactionName);
    TransactionCounts txnCounts = Mockito.mock(TransactionCounts.class);
    when(txnCounts.isOverTracerSegmentLimit()).thenReturn(false);
    when(tx.getTransactionCounts()).thenReturn(txnCounts);
}
Also used : ResponseTimeStats(com.newrelic.agent.stats.ResponseTimeStats) TransactionStats(com.newrelic.agent.stats.TransactionStats) PriorityTransactionName(com.newrelic.agent.transaction.PriorityTransactionName) InboundHeaders(com.newrelic.api.agent.InboundHeaders) SimpleStatsEngine(com.newrelic.agent.stats.SimpleStatsEngine) TransactionCounts(com.newrelic.agent.transaction.TransactionCounts) DistributedTracePayloadImpl(com.newrelic.agent.tracing.DistributedTracePayloadImpl)

Example 4 with TransactionCounts

use of com.newrelic.agent.transaction.TransactionCounts in project newrelic-java-agent by newrelic.

the class TransactionStateImplTest method tracerGeneration_segmentClampInPlace.

@Test
public void tracerGeneration_segmentClampInPlace() throws Exception {
    Tracer rootTracer = createRootTracer();
    Transaction tx = Transaction.getTransaction();
    TransactionCounts transactionCounts = tx.getTransactionCounts();
    transactionCounts.addTracers(3001);
    TracerFactory tracerFactory = Mockito.mock(TracerFactory.class, new Returns(rootTracer));
    ClassMethodSignature sig = new ClassMethodSignature("com.test.Dude", "dude1", "()V");
    tx.getTransactionState().getTracer(tx, tracerFactory, sig, null, (Object[]) null);
    Assert.assertNotNull(tx.getTransactionActivity().getRootTracer());
    Tracer tracer = tx.getTransactionState().getTracer(tx, null, sig, null, simpleFlags);
    assertNotEquals(UltraLightTracer.class, tracer.getClass());
    assertEquals("Java/com.test.Dude/dude1", tracer.getTransactionSegmentName());
    Tracer sqlTracer = tx.getTransactionState().getSqlTracer(tx, null, sig, null, simpleFlags);
    assertEquals(UltraLightTracer.class, sqlTracer.getClass());
    assertEquals("Clamped/com.test.Dude/dude1", sqlTracer.getTransactionSegmentName());
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) UltraLightTracer(com.newrelic.agent.tracers.UltraLightTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) TransactionCounts(com.newrelic.agent.transaction.TransactionCounts) TracerFactory(com.newrelic.agent.tracers.TracerFactory) Test(org.junit.Test)

Example 5 with TransactionCounts

use of com.newrelic.agent.transaction.TransactionCounts in project newrelic-java-agent by newrelic.

the class SegmentTest method getNumTracers.

private static int getNumTracers(Transaction tx) {
    TransactionCounts counts = tx.getTransactionCounts();
    TransactionData data = new TransactionData(tx, counts.getTransactionSize());
    // this method can fixed by remove the +1.
    return data.getTracers().size() + 1;
}
Also used : TransactionCounts(com.newrelic.agent.transaction.TransactionCounts)

Aggregations

TransactionCounts (com.newrelic.agent.transaction.TransactionCounts)5 TransactionStats (com.newrelic.agent.stats.TransactionStats)2 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)2 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)2 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)2 Tracer (com.newrelic.agent.tracers.Tracer)2 TracerFactory (com.newrelic.agent.tracers.TracerFactory)2 UltraLightTracer (com.newrelic.agent.tracers.UltraLightTracer)2 Test (org.junit.Test)2 Returns (org.mockito.internal.stubbing.answers.Returns)2 DistributedTracingConfig (com.newrelic.agent.config.DistributedTracingConfig)1 TransactionTracerConfig (com.newrelic.agent.config.TransactionTracerConfig)1 ResponseTimeStats (com.newrelic.agent.stats.ResponseTimeStats)1 SimpleStatsEngine (com.newrelic.agent.stats.SimpleStatsEngine)1 DistributedTracePayloadImpl (com.newrelic.agent.tracing.DistributedTracePayloadImpl)1 PriorityTransactionName (com.newrelic.agent.transaction.PriorityTransactionName)1 TransactionThrowable (com.newrelic.agent.transaction.TransactionThrowable)1 InboundHeaders (com.newrelic.api.agent.InboundHeaders)1