From d5970c288b595b53371f2d608297087cde2851b1 Mon Sep 17 00:00:00 2001 From: Piotr Siejczuk Date: Sat, 30 Mar 2019 15:40:23 +0100 Subject: [PATCH] #22047: Magento CRON Job Names are missing in NewRelic: Transaction Names FIX --- .../Observer/ProcessCronQueueObserver.php | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php b/app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php index 4bb5dc196f985..e08e46009ce80 100644 --- a/app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php +++ b/app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php @@ -145,6 +145,11 @@ class ProcessCronQueueObserver implements ObserverInterface */ private $statProfiler; + /** + * Key and Value Array of Cron Job Names + */ + protected const LIST_CRON_NAME = []; + /** * @param \Magento\Framework\ObjectManagerInterface $objectManager * @param \Magento\Cron\Model\ScheduleFactory $scheduleFactory @@ -218,6 +223,11 @@ function ($a, $b) { $phpPath = $this->phpExecutableFinder->find() ?: 'php'; + // End previous NewRelic Transaction to track Cron Job Traces + if (extension_loaded('newrelic')) { + newrelic_end_transaction(true); + } + foreach ($jobGroupsRoot as $groupId => $jobsRoot) { if (!$this->isGroupInFilter($groupId)) { continue; @@ -734,6 +744,12 @@ private function processPendingJobs($groupId, $jobsRoot, $currentTime) try { if ($schedule->tryLockJob()) { + if (extension_loaded('newrelic')) { + //Get Job Name after Name Changes + $jobName = $this->changeCronName($schedule->getJobCode()); + newrelic_start_transaction(ini_get('newrelic.appname')); + newrelic_name_transaction($jobName); + } $this->_runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId); } } catch (\Exception $e) { @@ -742,6 +758,10 @@ private function processPendingJobs($groupId, $jobsRoot, $currentTime) if ($schedule->getStatus() === Schedule::STATUS_SUCCESS) { $procesedJobs[$schedule->getJobCode()] = true; } + + if (extension_loaded('newrelic')) { + newrelic_end_transaction(); + } $schedule->save(); } } @@ -765,4 +785,20 @@ private function processError(\Magento\Cron\Model\Schedule $schedule, \Exception $this->logger->info($schedule->getMessages()); } } + + /** + * Get Cron Name for proper CRON Job Traces Tracking + * + * @param string $keyCronName + * + * @return mixed + */ + public static function changeCronName($keyCronName) + { + $cronNameMagento = self::LIST_CRON_NAME; + if (isset($cronNameMagento[$keyCronName])) { + return $cronNameMagento[$keyCronName]; + } + return $keyCronName; + } }