Skip to content

Add Ability To Separate Frontend / Adminhtml in New Relic #12935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/code/Magento/NewRelicReporting/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ public function getNewRelicAppName()
return (string)$this->scopeConfig->getValue('newrelicreporting/general/app_name');
}

/**
* Returns configured separate apps value
*
* @return bool
*/
public function isSeparateApps()
{
return (bool)$this->scopeConfig->getValue('newrelicreporting/general/separate_apps');
}

/**
* Returns config setting for overall cron to be enabled
*
Expand Down
13 changes: 13 additions & 0 deletions app/code/Magento/NewRelicReporting/Model/NewRelicWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ public function reportError($exception)
}
}

/**
* Wrapper for 'newrelic_set_appname'
*
* @param string $appName
* @return void
*/
public function setAppName(string $appName)
{
if (extension_loaded('newrelic')) {
newrelic_set_appname($appName);
}
}

/**
* Checks whether newrelic-php5 agent is installed
*
Expand Down
92 changes: 92 additions & 0 deletions app/code/Magento/NewRelicReporting/Plugin/StatePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\NewRelicReporting\Plugin;

use Magento\Framework\App\State;
use Magento\Framework\Exception\LocalizedException;
use Magento\NewRelicReporting\Model\Config;
use Magento\NewRelicReporting\Model\NewRelicWrapper;
use Psr\Log\LoggerInterface;

class StatePlugin
{
/**
* @var Config
*/
private $config;

/**
* @var NewRelicWrapper
*/
private $newRelicWrapper;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param Config $config
* @param NewRelicWrapper $newRelicWrapper
*/
public function __construct(
Config $config,
NewRelicWrapper $newRelicWrapper,
LoggerInterface $logger
) {
$this->config = $config;
$this->newRelicWrapper = $newRelicWrapper;
$this->logger = $logger;
}

/**
* Set separate appname
*
* @param State $subject
* @param null $result
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterSetAreaCode(State $state, $result)
{
if (!$this->shouldSetAppName()) {
return $result;
}

try {
$this->newRelicWrapper->setAppName($this->appName($state));
} catch (LocalizedException $e) {
$this->logger->critical($e);
return $result;
}
}

private function appName(State $state)
{
$code = $state->getAreaCode();
$current = $this->config->getNewRelicAppName();

return $current . ';' . $current . '_' . $code;
}

private function shouldSetAppName()
{
if (!$this->config->isNewRelicEnabled()) {
return false;
}

if (!$this->config->getNewRelicAppName()) {
return false;
}

if (!$this->config->isSeparateApps()) {
return false;
}

return true;
}
}
5 changes: 5 additions & 0 deletions app/code/Magento/NewRelicReporting/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<label>New Relic Application Name</label>
<comment>This is located by navigating to Settings from the New Relic APM website</comment>
</field>
<field id="separate_apps" translate="label comment" type="select" sortOrder="9" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Send Adminhtml and Frontend as Separate Apps</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>In addition to the main app (which includes all PHP execution), separate apps for adminhtml and frontend will be created. Requires New Relic Application Name to be set.</comment>
</field>
</group>
<group id="cron" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Cron</label>
Expand Down
3 changes: 3 additions & 0 deletions app/code/Magento/NewRelicReporting/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<type name="Magento\Framework\App\Http">
<plugin name="framework-http-newrelic" type="Magento\NewRelicReporting\Plugin\HttpPlugin"/>
</type>
<type name="Magento\Framework\App\State">
<plugin name="framework-state-newrelic" type="Magento\NewRelicReporting\Plugin\StatePlugin"/>
</type>
<type name="Magento\Framework\Console\CommandListInterface">
<arguments>
<argument name="commands" xsi:type="array">
Expand Down
2 changes: 2 additions & 0 deletions app/code/Magento/NewRelicReporting/i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ General,General
"This is located by navigating to Settings from the New Relic APM website","This is located by navigating to Settings from the New Relic APM website"
Cron,Cron
"Enable Cron","Enable Cron"
"Send Adminhtml and Frontend as Separate Apps","Send Adminhtml and Frontend as Separate Apps"
"In addition to the main app (which includes all PHP execution), separate apps for adminhtml and frontend will be created. Requires New Relic Application Name to be set.","In addition to the main app (which includes all PHP execution), separate apps for adminhtml and frontend will be created. Requires New Relic Application Name to be set."
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\NewRelicReporting\Plugin;

use Magento\Framework\App\State;
use Magento\NewRelicReporting\Model\NewRelicWrapper;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\Helper\Bootstrap;

class SeparateAppsTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ObjectManager
*/
private $objectManager;

protected function setUp()
{
$this->objectManager = Bootstrap::getObjectManager();
}

/**
* @magentoConfigFixture default/newrelicreporting/general/enable 1
* @magentoConfigFixture default/newrelicreporting/general/app_name beverly_hills
* @magentoConfigFixture default/newrelicreporting/general/separate_apps 1
*/
public function testAppNameIsSetWhenConfiguredCorrectly()
{
$newRelicWrapper = $this->getMockBuilder(NewRelicWrapper::class)
->setMethods(['setAppName'])
->getMock();

$this->objectManager->configure([NewRelicWrapper::class => ['shared' => true]]);
$this->objectManager->addSharedInstance($newRelicWrapper, NewRelicWrapper::class);

$newRelicWrapper->expects($this->once())
->method('setAppName')
->with($this->equalTo('beverly_hills;beverly_hills_90210'));

$state = $this->objectManager->get(State::class);

$state->setAreaCode('90210');
}
}