Example #1
0
        public void OnTransactionFinalized_CallsAgentHealthReporter()
        {
            var startTime = DateTime.Now;
            var segments  = new Segment[]
            {
                GetFinishedSegment(startTime, startTime.AddSeconds(0), TimeSpan.FromSeconds(3)),
                GetFinishedSegment(startTime, startTime.AddSeconds(1), TimeSpan.FromSeconds(1)),
                GetFinishedSegment(startTime, startTime.AddSeconds(2), TimeSpan.FromSeconds(1)),
            };
            var transaction       = BuildTestTransaction(segments, startTime);
            var mockedTransaction = Mock.Create <IInternalTransaction>();

            Mock.Arrange(() => mockedTransaction.Finish()).Returns(true);
            Mock.Arrange(() => mockedTransaction.ConvertToImmutableTransaction()).Returns(transaction);

            var transactionMetricName = new TransactionMetricName("c", "d");

            Mock.Arrange(() => _transactionMetricNameMaker.GetTransactionMetricName(Arg.IsAny <ITransactionName>())).Returns(transactionMetricName);

            EventBus <TransactionFinalizedEvent> .Publish(new TransactionFinalizedEvent(mockedTransaction));

            Mock.Assert(() => _agentHealthReporter.ReportTransactionGarbageCollected(transactionMetricName, Arg.IsAny <string>(), Arg.IsAny <string>()));
        }
        private void OnTransactionFinalized(TransactionFinalizedEvent eventData)
        {
            var internalTransaction = eventData.Transaction;

            // When a transaction gets finalized it means it never ended cleanly, so we should try to estimate when it ended based on its last finished segment
            var immutableTransaction = internalTransaction.ConvertToImmutableTransaction();
            var lastStartedSegment   = TryGetLastStartedSegment(immutableTransaction);
            var lastFinishedSegment  = TryGetLastFinishedSegment(immutableTransaction);
            var estimatedDuration    = GetEstimatedTransactionDuration(internalTransaction, lastStartedSegment, lastFinishedSegment);
            var finishedTransaction  = false;

            try
            {
                internalTransaction.ForceChangeDuration(estimatedDuration);

                // Then we should mark the transaction as cleanly finished so it won't get finalized again
                finishedTransaction = Finish(internalTransaction);

                if (finishedTransaction)
                {
                    // Then we send it off to be transformed as with normal transactions
                    _transactionTransformer.Transform(internalTransaction);
                }
            }
            finally
            {
                if (finishedTransaction)
                {
                    // Finally, we announce the event to our agent health reporter
                    var transactionMetricName   = _transactionMetricNameMaker.GetTransactionMetricName(immutableTransaction.TransactionName);
                    var lastFinishedSegmentName = lastFinishedSegment != null
                        ? lastFinishedSegment.GetTransactionTraceName()
                        : "<unknown>";

                    var lastStartedSegmentName = lastStartedSegment != null
                        ? lastStartedSegment.GetTransactionTraceName()
                        : "<unknown>";

                    _agentHealthReporter.ReportTransactionGarbageCollected(transactionMetricName, lastStartedSegmentName, lastFinishedSegmentName);
                }
            }
        }