Skip to content
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
13 changes: 13 additions & 0 deletions app/code/Magento/NewRelicReporting/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\NewRelicReporting\Model;

/**
* NewRelic configuration model
*/
class Config
{
/**#@+
Expand Down Expand Up @@ -161,6 +164,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
102 changes: 102 additions & 0 deletions app/code/Magento/NewRelicReporting/Plugin/StatePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you use strict type, would be great to use return types as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


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;

/**
* Handles setting which, when enabled, reports frontend and adminhtml as separate apps to New Relic.
*/
class StatePlugin
{
/**
* @var Config
*/
private $config;

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

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

/**
* @param Config $config
* @param NewRelicWrapper $newRelicWrapper
Copy link
Member

@imadphp imadphp Jun 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add $logger param

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. will Update PR

* @param LoggerInterface $logger
*/
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 mixed $result
* @return mixed
*/
public function afterSetAreaCode(State $subject, $result)
{
if (!$this->shouldSetAppName()) {
return $result;
}

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

return $result;
}

/**
* Format appName.
*
* @param State $state
* @return string
* @throws LocalizedException
*/
private function appName(State $state): string
{
$code = $state->getAreaCode();
$current = $this->config->getNewRelicAppName();

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

/**
* Check if app name should be set.
*
* @return bool
*/
private function shouldSetAppName(): bool
{
return (
$this->config->isSeparateApps() &&
$this->config->getNewRelicAppName() &&
$this->config->isNewRelicEnabled()
);
}
}
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,76 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

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
*/
class SeparateAppsTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ObjectManager
*/
private $objectManager;

/**
* @inheritdoc
*/
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()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't check case if adjusting is disabled and exception

{
$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');
}

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

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

$newRelicWrapper->expects($this->never())->method('setAppName');

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

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