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 4 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($appName)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to define variable type to "string" there, we're not supporting php 5.6 anymore

{
if (extension_loaded('newrelic')) {
newrelic_set_appname($appName);
}
}

/**
* Checks whether newrelic-php5 agent is installed
*
Expand Down
83 changes: 83 additions & 0 deletions app/code/Magento/NewRelicReporting/Plugin/StatePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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;

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

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

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

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

Choose a reason for hiding this comment

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

Probably we should have return $result here

}

try {
$this->newRelicWrapper->setAppName($this->appName($state));
} catch (LocalizedException $e) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Why should we ignore such exception?
  2. Probably we should have return $result here

}
}

private function appName(State $state)
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be better to rename it to "setAppName"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a getter. Would you prefer getAppName?

{
$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."