-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Cron Jobs names to New Relic transactions
- Loading branch information
1 parent
17183cf
commit 5819ded
Showing
7 changed files
with
400 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
app/code/Magento/NewRelicReporting/Plugin/StatPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\NewRelicReporting\Plugin; | ||
|
||
use Magento\Framework\Profiler\Driver\Standard\Stat; | ||
use Magento\NewRelicReporting\Model\Config; | ||
use Magento\NewRelicReporting\Model\NewRelicWrapper; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Class StatPlugin handles single Cron Jobs transaction names | ||
*/ | ||
class StatPlugin | ||
{ | ||
public const TIMER_NAME_CRON_PREFIX = 'job'; | ||
|
||
/** | ||
* @var Config | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @var NewRelicWrapper | ||
*/ | ||
private $newRelicWrapper; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @param Config $config | ||
* @param NewRelicWrapper $newRelicWrapper | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct( | ||
Config $config, | ||
NewRelicWrapper $newRelicWrapper, | ||
LoggerInterface $logger | ||
) { | ||
$this->config = $config; | ||
$this->newRelicWrapper = $newRelicWrapper; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Before running original profiler, register NewRelic transaction | ||
* | ||
* @param Stat $schedule | ||
* @param array $args | ||
* @return array | ||
* @see \Magento\Cron\Observer\ProcessCronQueueObserver::startProfiling | ||
* | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function beforeStart(Stat $schedule, ...$args): array | ||
{ | ||
$timerName = current($args); | ||
|
||
if ($this->isCronJob($timerName)) { | ||
$this->newRelicWrapper->setTransactionName( | ||
sprintf('Cron %s', $timerName) | ||
); | ||
} | ||
|
||
return $args; | ||
} | ||
|
||
/** | ||
* Before stopping original profiler, close NewRelic transaction | ||
* | ||
* @param Stat $schedule | ||
* @param array $args | ||
* @return array | ||
* | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function beforeStop(Stat $schedule, ...$args): array | ||
{ | ||
$timerName = current($args); | ||
|
||
if ($this->isCronJob($timerName)) { | ||
$this->newRelicWrapper->endTransaction(); | ||
} | ||
|
||
return $args; | ||
} | ||
|
||
/** | ||
* Determines whether provided name is Cron Job | ||
* | ||
* @param string $timerName | ||
* @return bool | ||
*/ | ||
private function isCronJob(string $timerName): bool | ||
{ | ||
return 0 === strpos($timerName, static::TIMER_NAME_CRON_PREFIX); | ||
} | ||
} |
Oops, something went wrong.