Skip to content

Commit 68e516c

Browse files
ENGCOM-1860: Add Ability To Separate Frontend / Adminhtml in New Relic #12935
- Merge Pull Request #12935 from mpchadwick/magento2:feature/new-relic-separate-areas - Merged commits: 1. 95b2c36 2. 48066b2 3. 7ceace8 4. 0b68496 5. e186221 6. dc48306 7. bef36de 8. 8141f17 9. 24d7d4a
2 parents 1b52d8c + 24d7d4a commit 68e516c

File tree

7 files changed

+172
-0
lines changed

7 files changed

+172
-0
lines changed

app/code/Magento/NewRelicReporting/Model/Config.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ public function getNewRelicAppName()
161161
return (string)$this->scopeConfig->getValue('newrelicreporting/general/app_name');
162162
}
163163

164+
/**
165+
* Returns configured separate apps value
166+
*
167+
* @return bool
168+
*/
169+
public function isSeparateApps()
170+
{
171+
return (bool)$this->scopeConfig->getValue('newrelicreporting/general/separate_apps');
172+
}
173+
164174
/**
165175
* Returns config setting for overall cron to be enabled
166176
*

app/code/Magento/NewRelicReporting/Model/NewRelicWrapper.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ public function reportError($exception)
4141
}
4242
}
4343

44+
/**
45+
* Wrapper for 'newrelic_set_appname'
46+
*
47+
* @param string $appName
48+
* @return void
49+
*/
50+
public function setAppName(string $appName)
51+
{
52+
if (extension_loaded('newrelic')) {
53+
newrelic_set_appname($appName);
54+
}
55+
}
56+
4457
/**
4558
* Checks whether newrelic-php5 agent is installed
4659
*
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\NewRelicReporting\Plugin;
7+
8+
use Magento\Framework\App\State;
9+
use Magento\Framework\Exception\LocalizedException;
10+
use Magento\NewRelicReporting\Model\Config;
11+
use Magento\NewRelicReporting\Model\NewRelicWrapper;
12+
use Psr\Log\LoggerInterface;
13+
14+
class StatePlugin
15+
{
16+
/**
17+
* @var Config
18+
*/
19+
private $config;
20+
21+
/**
22+
* @var NewRelicWrapper
23+
*/
24+
private $newRelicWrapper;
25+
26+
/**
27+
* @var LoggerInterface
28+
*/
29+
private $logger;
30+
31+
/**
32+
* @param Config $config
33+
* @param NewRelicWrapper $newRelicWrapper
34+
*/
35+
public function __construct(
36+
Config $config,
37+
NewRelicWrapper $newRelicWrapper,
38+
LoggerInterface $logger
39+
) {
40+
$this->config = $config;
41+
$this->newRelicWrapper = $newRelicWrapper;
42+
$this->logger = $logger;
43+
}
44+
45+
/**
46+
* Set separate appname
47+
*
48+
* @param State $subject
49+
* @param null $result
50+
* @return void
51+
*
52+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
53+
*/
54+
public function afterSetAreaCode(State $state, $result)
55+
{
56+
if (!$this->shouldSetAppName()) {
57+
return $result;
58+
}
59+
60+
try {
61+
$this->newRelicWrapper->setAppName($this->appName($state));
62+
} catch (LocalizedException $e) {
63+
$this->logger->critical($e);
64+
return $result;
65+
}
66+
}
67+
68+
private function appName(State $state)
69+
{
70+
$code = $state->getAreaCode();
71+
$current = $this->config->getNewRelicAppName();
72+
73+
return $current . ';' . $current . '_' . $code;
74+
}
75+
76+
private function shouldSetAppName()
77+
{
78+
if (!$this->config->isNewRelicEnabled()) {
79+
return false;
80+
}
81+
82+
if (!$this->config->getNewRelicAppName()) {
83+
return false;
84+
}
85+
86+
if (!$this->config->isSeparateApps()) {
87+
return false;
88+
}
89+
90+
return true;
91+
}
92+
}

app/code/Magento/NewRelicReporting/etc/adminhtml/system.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
<label>New Relic Application Name</label>
4747
<comment>This is located by navigating to Settings from the New Relic APM website</comment>
4848
</field>
49+
<field id="separate_apps" translate="label comment" type="select" sortOrder="9" showInDefault="1" showInWebsite="1" showInStore="1">
50+
<label>Send Adminhtml and Frontend as Separate Apps</label>
51+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
52+
<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>
53+
</field>
4954
</group>
5055
<group id="cron" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
5156
<label>Cron</label>

app/code/Magento/NewRelicReporting/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
<type name="Magento\Framework\App\Http">
3131
<plugin name="framework-http-newrelic" type="Magento\NewRelicReporting\Plugin\HttpPlugin"/>
3232
</type>
33+
<type name="Magento\Framework\App\State">
34+
<plugin name="framework-state-newrelic" type="Magento\NewRelicReporting\Plugin\StatePlugin"/>
35+
</type>
3336
<type name="Magento\Framework\Console\CommandListInterface">
3437
<arguments>
3538
<argument name="commands" xsi:type="array">

app/code/Magento/NewRelicReporting/i18n/en_US.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ General,General
2121
"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"
2222
Cron,Cron
2323
"Enable Cron","Enable Cron"
24+
"Send Adminhtml and Frontend as Separate Apps","Send Adminhtml and Frontend as Separate Apps"
25+
"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."
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\NewRelicReporting\Plugin;
7+
8+
use Magento\Framework\App\State;
9+
use Magento\NewRelicReporting\Model\NewRelicWrapper;
10+
use Magento\TestFramework\ObjectManager;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
13+
class SeparateAppsTest extends \PHPUnit\Framework\TestCase
14+
{
15+
/**
16+
* @var ObjectManager
17+
*/
18+
private $objectManager;
19+
20+
protected function setUp()
21+
{
22+
$this->objectManager = Bootstrap::getObjectManager();
23+
}
24+
25+
/**
26+
* @magentoConfigFixture default/newrelicreporting/general/enable 1
27+
* @magentoConfigFixture default/newrelicreporting/general/app_name beverly_hills
28+
* @magentoConfigFixture default/newrelicreporting/general/separate_apps 1
29+
*/
30+
public function testAppNameIsSetWhenConfiguredCorrectly()
31+
{
32+
$newRelicWrapper = $this->getMockBuilder(NewRelicWrapper::class)
33+
->setMethods(['setAppName'])
34+
->getMock();
35+
36+
$this->objectManager->configure([NewRelicWrapper::class => ['shared' => true]]);
37+
$this->objectManager->addSharedInstance($newRelicWrapper, NewRelicWrapper::class);
38+
39+
$newRelicWrapper->expects($this->once())
40+
->method('setAppName')
41+
->with($this->equalTo('beverly_hills;beverly_hills_90210'));
42+
43+
$state = $this->objectManager->get(State::class);
44+
45+
$state->setAreaCode('90210');
46+
}
47+
}

0 commit comments

Comments
 (0)