Skip to content

Commit ec4ff88

Browse files
committed
Merge branch 'ENGCOM-2974-magento-magento2-15947' of github.com:magento-engcom/magento2ce into 2.3-develop-prs
2 parents 291472b + 58e1c81 commit ec4ff88

File tree

7 files changed

+214
-0
lines changed

7 files changed

+214
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\NewRelicReporting\Model;
77

8+
/**
9+
* NewRelic configuration model
10+
*/
811
class Config
912
{
1013
/**#@+
@@ -161,6 +164,16 @@ public function getNewRelicAppName()
161164
return (string)$this->scopeConfig->getValue('newrelicreporting/general/app_name');
162165
}
163166

167+
/**
168+
* Returns configured separate apps value
169+
*
170+
* @return bool
171+
*/
172+
public function isSeparateApps()
173+
{
174+
return (bool)$this->scopeConfig->getValue('newrelicreporting/general/separate_apps');
175+
}
176+
164177
/**
165178
* Returns config setting for overall cron to be enabled
166179
*

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: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\NewRelicReporting\Plugin;
9+
10+
use Magento\Framework\App\State;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\NewRelicReporting\Model\Config;
13+
use Magento\NewRelicReporting\Model\NewRelicWrapper;
14+
use Psr\Log\LoggerInterface;
15+
16+
/**
17+
* Handles setting which, when enabled, reports frontend and adminhtml as separate apps to New Relic.
18+
*/
19+
class StatePlugin
20+
{
21+
/**
22+
* @var Config
23+
*/
24+
private $config;
25+
26+
/**
27+
* @var NewRelicWrapper
28+
*/
29+
private $newRelicWrapper;
30+
31+
/**
32+
* @var LoggerInterface
33+
*/
34+
private $logger;
35+
36+
/**
37+
* @param Config $config
38+
* @param NewRelicWrapper $newRelicWrapper
39+
* @param LoggerInterface $logger
40+
*/
41+
public function __construct(
42+
Config $config,
43+
NewRelicWrapper $newRelicWrapper,
44+
LoggerInterface $logger
45+
) {
46+
$this->config = $config;
47+
$this->newRelicWrapper = $newRelicWrapper;
48+
$this->logger = $logger;
49+
}
50+
51+
/**
52+
* Set separate appname
53+
*
54+
* @param State $subject
55+
* @param mixed $result
56+
* @return mixed
57+
*/
58+
public function afterSetAreaCode(State $subject, $result)
59+
{
60+
if (!$this->shouldSetAppName()) {
61+
return $result;
62+
}
63+
64+
try {
65+
$this->newRelicWrapper->setAppName($this->appName($subject));
66+
} catch (LocalizedException $e) {
67+
$this->logger->critical($e);
68+
return $result;
69+
}
70+
71+
return $result;
72+
}
73+
74+
/**
75+
* Format appName.
76+
*
77+
* @param State $state
78+
* @return string
79+
* @throws LocalizedException
80+
*/
81+
private function appName(State $state): string
82+
{
83+
$code = $state->getAreaCode();
84+
$current = $this->config->getNewRelicAppName();
85+
86+
return $current . ';' . $current . '_' . $code;
87+
}
88+
89+
/**
90+
* Check if app name should be set.
91+
*
92+
* @return bool
93+
*/
94+
private function shouldSetAppName(): bool
95+
{
96+
return (
97+
$this->config->isSeparateApps() &&
98+
$this->config->getNewRelicAppName() &&
99+
$this->config->isNewRelicEnabled()
100+
);
101+
}
102+
}

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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\NewRelicReporting\Plugin;
9+
10+
use Magento\Framework\App\State;
11+
use Magento\NewRelicReporting\Model\NewRelicWrapper;
12+
use Magento\TestFramework\ObjectManager;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
15+
/**
16+
* Class SeparateAppsTest
17+
*/
18+
class SeparateAppsTest extends \PHPUnit\Framework\TestCase
19+
{
20+
/**
21+
* @var ObjectManager
22+
*/
23+
private $objectManager;
24+
25+
/**
26+
* @inheritdoc
27+
*/
28+
protected function setUp()
29+
{
30+
$this->objectManager = Bootstrap::getObjectManager();
31+
}
32+
33+
/**
34+
* @magentoConfigFixture default/newrelicreporting/general/enable 1
35+
* @magentoConfigFixture default/newrelicreporting/general/app_name beverly_hills
36+
* @magentoConfigFixture default/newrelicreporting/general/separate_apps 1
37+
*/
38+
public function testAppNameIsSetWhenConfiguredCorrectly()
39+
{
40+
$newRelicWrapper = $this->getMockBuilder(NewRelicWrapper::class)
41+
->setMethods(['setAppName'])
42+
->getMock();
43+
44+
$this->objectManager->configure([NewRelicWrapper::class => ['shared' => true]]);
45+
$this->objectManager->addSharedInstance($newRelicWrapper, NewRelicWrapper::class);
46+
47+
$newRelicWrapper->expects($this->once())
48+
->method('setAppName')
49+
->with($this->equalTo('beverly_hills;beverly_hills_90210'));
50+
51+
$state = $this->objectManager->get(State::class);
52+
53+
$state->setAreaCode('90210');
54+
}
55+
56+
/**
57+
* @magentoConfigFixture default/newrelicreporting/general/enable 1
58+
* @magentoConfigFixture default/newrelicreporting/general/app_name beverly_hills
59+
* @magentoConfigFixture default/newrelicreporting/general/separate_apps 0
60+
*/
61+
public function testAppNameIsNotSetWhenDisabled()
62+
{
63+
$newRelicWrapper = $this->getMockBuilder(NewRelicWrapper::class)
64+
->setMethods(['setAppName'])
65+
->getMock();
66+
67+
$this->objectManager->configure([NewRelicWrapper::class => ['shared' => true]]);
68+
$this->objectManager->addSharedInstance($newRelicWrapper, NewRelicWrapper::class);
69+
70+
$newRelicWrapper->expects($this->never())->method('setAppName');
71+
72+
$state = $this->objectManager->get(State::class);
73+
74+
$state->setAreaCode('90210');
75+
}
76+
}

0 commit comments

Comments
 (0)