Skip to content

Commit b71666d

Browse files
committed
ACP2E-3187: Metric in NR might be misleading for background transactions- Follow up of ACP2E-3067
1 parent 11e7d89 commit b71666d

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\NewRelicReporting\Model;
77

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

1012
/**
@@ -16,6 +18,26 @@ class NewRelicWrapper
1618
{
1719
private const NEWRELIC_APPNAME = 'newrelic.appname';
1820

21+
/**
22+
* @var Config
23+
*/
24+
private $config;
25+
26+
/**
27+
* @var State
28+
*/
29+
private $state;
30+
31+
/**
32+
* @param ?Config $config
33+
* @param ?State $state
34+
*/
35+
public function __construct(?Config $config = null, ?State $state = null)
36+
{
37+
$this->config = $config ?? ObjectManager::getInstance()->get(Config::class);
38+
$this->state = $state ?? ObjectManager::getInstance()->get(State::class);
39+
}
40+
1941
/**
2042
* Wrapper for 'newrelic_add_custom_parameter' function
2143
*
@@ -79,7 +101,8 @@ public function setTransactionName(string $transactionName): void
79101
public function startBackgroundTransaction()
80102
{
81103
if ($this->isExtensionInstalled()) {
82-
newrelic_start_transaction(ini_get(self::NEWRELIC_APPNAME));
104+
$name = $this->getCurrentAppName();
105+
newrelic_start_transaction($name);
83106
newrelic_background_job();
84107
}
85108
}
@@ -106,4 +129,21 @@ public function isExtensionInstalled()
106129
{
107130
return extension_loaded('newrelic');
108131
}
132+
133+
/**
134+
* Get current App name for NR transactions
135+
*
136+
* @return string
137+
*/
138+
public function getCurrentAppName()
139+
{
140+
if ($this->config->isSeparateApps() &&
141+
$this->config->getNewRelicAppName() &&
142+
$this->config->isNewRelicEnabled()) {
143+
$code = $this->state->getAreaCode();
144+
$current = $this->config->getNewRelicAppName();
145+
return $current . ';' . $current . '_' . $code;
146+
}
147+
return ini_get(self::NEWRELIC_APPNAME);
148+
}
109149
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Magento\NewRelicReporting\Test\Unit\Model;
4+
5+
use Magento\NewRelicReporting\Model\NewRelicWrapper;
6+
use Magento\NewRelicReporting\Model\Config;
7+
use Magento\Framework\App\State;
8+
use PHPUnit\Framework\TestCase;
9+
use PHPUnit\Framework\MockObject\MockObject;
10+
11+
class NewRelicWrapperTest extends TestCase
12+
{
13+
/**
14+
* @var Config|MockObject
15+
*/
16+
private $config;
17+
18+
/**
19+
* @var State|MockObject
20+
*/
21+
private $state;
22+
23+
/**
24+
* @var NewRelicWrapper
25+
*/
26+
private $newRelicWrapper;
27+
28+
protected function setUp(): void
29+
{
30+
$this->config = $this->createMock(Config::class);
31+
$this->state = $this->createMock(State::class);
32+
$this->newRelicWrapper = new NewRelicWrapper($this->config, $this->state);
33+
}
34+
35+
public function testGetCurrentAppName()
36+
{
37+
$this->config->expects($this->once())
38+
->method('isSeparateApps')
39+
->willReturn(true);
40+
$this->config->expects($this->atLeastOnce())
41+
->method('getNewRelicAppName')
42+
->willReturn('Magento');
43+
$this->config->expects($this->once())
44+
->method('isNewRelicEnabled')
45+
->willReturn(true);
46+
$this->state->expects($this->once())
47+
->method('getAreaCode')
48+
->willReturn('cron');
49+
$this->assertEquals('Magento;Magento_cron', $this->newRelicWrapper->getCurrentAppName());
50+
}
51+
}

0 commit comments

Comments
 (0)