Skip to content

Commit 56211fb

Browse files
committed
Merge remote-tracking branch 'act4/ACP2E-3187' into PR_13_OCT_2024
2 parents b6951e8 + e4dddbc commit 56211fb

File tree

2 files changed

+99
-3
lines changed

2 files changed

+99
-3
lines changed

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

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\NewRelicReporting\Model;
77

8+
use Magento\Framework\App\State;
9+
use Magento\Framework\App\ObjectManager;
810
use Throwable;
911

1012
/**
@@ -17,6 +19,26 @@ class NewRelicWrapper
1719
private const NEWRELIC_APPNAME = 'newrelic.appname';
1820
private const NEWRELIC_AUTO_INSTRUMENT = 'newrelic.browser_monitoring.auto_instrument';
1921

22+
/**
23+
* @var Config
24+
*/
25+
private $config;
26+
27+
/**
28+
* @var State
29+
*/
30+
private $state;
31+
32+
/**
33+
* @param ?Config $config
34+
* @param ?State $state
35+
*/
36+
public function __construct(?Config $config = null, ?State $state = null)
37+
{
38+
$this->config = $config ?? ObjectManager::getInstance()->get(Config::class);
39+
$this->state = $state ?? ObjectManager::getInstance()->get(State::class);
40+
}
41+
2042
/**
2143
* Wrapper for 'newrelic_add_custom_parameter' function
2244
*
@@ -80,7 +102,8 @@ public function setTransactionName(string $transactionName): void
80102
public function startBackgroundTransaction()
81103
{
82104
if ($this->isExtensionInstalled()) {
83-
newrelic_start_transaction(ini_get(self::NEWRELIC_APPNAME));
105+
$name = $this->getCurrentAppName();
106+
newrelic_start_transaction($name);
84107
newrelic_background_job();
85108
}
86109
}
@@ -161,4 +184,21 @@ public function getBrowserTimingFooter(bool $includeTags = true): ?string
161184

162185
return newrelic_get_browser_timing_footer($includeTags);
163186
}
187+
188+
/**
189+
* Get current App name for NR transactions
190+
*
191+
* @return string
192+
*/
193+
public function getCurrentAppName()
194+
{
195+
if ($this->config->isSeparateApps() &&
196+
$this->config->getNewRelicAppName() &&
197+
$this->config->isNewRelicEnabled()) {
198+
$code = $this->state->getAreaCode();
199+
$current = $this->config->getNewRelicAppName();
200+
return $current . ';' . $current . '_' . $code;
201+
}
202+
return ini_get(self::NEWRELIC_APPNAME);
203+
}
164204
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\NewRelicReporting\Test\Unit\Model;
9+
10+
use Magento\NewRelicReporting\Model\NewRelicWrapper;
11+
use Magento\NewRelicReporting\Model\Config;
12+
use Magento\Framework\App\State;
13+
use PHPUnit\Framework\TestCase;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
16+
class NewRelicWrapperTest extends TestCase
17+
{
18+
/**
19+
* @var Config|MockObject
20+
*/
21+
private $config;
22+
23+
/**
24+
* @var State|MockObject
25+
*/
26+
private $state;
27+
28+
/**
29+
* @var NewRelicWrapper
30+
*/
31+
private $newRelicWrapper;
32+
33+
protected function setUp(): void
34+
{
35+
$this->config = $this->createMock(Config::class);
36+
$this->state = $this->createMock(State::class);
37+
$this->newRelicWrapper = new NewRelicWrapper($this->config, $this->state);
38+
}
39+
40+
public function testGetCurrentAppName()
41+
{
42+
$this->config->expects($this->once())
43+
->method('isSeparateApps')
44+
->willReturn(true);
45+
$this->config->expects($this->atLeastOnce())
46+
->method('getNewRelicAppName')
47+
->willReturn('Magento');
48+
$this->config->expects($this->once())
49+
->method('isNewRelicEnabled')
50+
->willReturn(true);
51+
$this->state->expects($this->once())
52+
->method('getAreaCode')
53+
->willReturn('cron');
54+
$this->assertEquals('Magento;Magento_cron', $this->newRelicWrapper->getCurrentAppName());
55+
}
56+
}

0 commit comments

Comments
 (0)