Skip to content

Commit dcfae95

Browse files
committed
Merge remote-tracking branch 'flowpl/add_newrelic_name_transaction'
2 parents 257a1c1 + 51ba2d9 commit dcfae95

2 files changed

Lines changed: 81 additions & 4 deletions

File tree

src/Monolog/Handler/NewRelicHandler.php

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class NewRelicHandler extends AbstractProcessingHandler
2727
*/
2828
protected $appName;
2929

30+
/**
31+
* Name of the current transaction
32+
*
33+
* @var string
34+
*/
35+
protected $transactionName;
36+
3037
/**
3138
* Some context and extra data is passed into the handler as arrays of values. Do we send them as is
3239
* (useful if we are using the API), or explode them for display on the NewRelic RPM website?
@@ -39,14 +46,21 @@ class NewRelicHandler extends AbstractProcessingHandler
3946
* {@inheritDoc}
4047
*
4148
* @param string $appName
42-
* @param boolean $implodeArrays
49+
* @param boolean $explodeArrays
50+
* @param string $transactionName
4351
*/
44-
public function __construct($level = Logger::ERROR, $bubble = true, $appName = null, $explodeArrays = false)
45-
{
52+
public function __construct(
53+
$level = Logger::ERROR,
54+
$bubble = true,
55+
$appName = null,
56+
$explodeArrays = false,
57+
$transactionName = null
58+
) {
4659
parent::__construct($level, $bubble);
4760

4861
$this->appName = $appName;
4962
$this->explodeArrays = $explodeArrays;
63+
$this->transactionName = $transactionName;
5064
}
5165

5266
/**
@@ -62,6 +76,11 @@ protected function write(array $record)
6276
$this->setNewRelicAppName($appName);
6377
}
6478

79+
if ($transactionName = $this->getTransactionName($record['context'])) {
80+
$this->setNewRelicTransactionName($transactionName);
81+
unset($record['context']['transaction_name']);
82+
}
83+
6584
if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Exception) {
6685
newrelic_notice_error($record['message'], $record['context']['exception']);
6786
unset($record['context']['exception']);
@@ -102,7 +121,7 @@ protected function isNewRelicEnabled()
102121

103122
/**
104123
* Returns the appname where this log should be sent. Each log can override the default appname, set in this
105-
* handler's constructor, by providing the appname in its context.
124+
* handler's constructor, by providing the appname in it's context.
106125
*
107126
* @param array $context
108127
* @return null|string
@@ -116,6 +135,23 @@ protected function getAppName(array $context)
116135
return $this->appName;
117136
}
118137

138+
/**
139+
* Returns the name of the current transaction. Each log can override the default transaction name, set in this
140+
* handler's constructor, by providing the transaction_name in it's context
141+
*
142+
* @param array $context
143+
*
144+
* @return null|string
145+
*/
146+
protected function getTransactionName(array $context)
147+
{
148+
if (isset($context['transaction_name'])) {
149+
return $context['transaction_name'];
150+
}
151+
152+
return $this->transactionName;
153+
}
154+
119155
/**
120156
* Sets the NewRelic application that should receive this log.
121157
*
@@ -125,4 +161,14 @@ protected function setNewRelicAppName($appName)
125161
{
126162
newrelic_set_appname($appName);
127163
}
164+
165+
/**
166+
* Overwrites the name of the current transaction
167+
*
168+
* @param $transactionName
169+
*/
170+
protected function setNewRelicTransactionName($transactionName)
171+
{
172+
newrelic_name_transaction($transactionName);
173+
}
128174
}

tests/Monolog/Handler/NewRelicHandlerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ class NewRelicHandlerTest extends TestCase
1818
{
1919
public static $appname;
2020
public static $customParameters;
21+
public static $transactionName;
2122

2223
public function setUp()
2324
{
2425
self::$appname = null;
2526
self::$customParameters = array();
27+
self::$transactionName = null;
2628
}
2729

2830
/**
@@ -125,6 +127,30 @@ public function testTheAppNameCanBeOverriddenFromEachLog()
125127

126128
$this->assertEquals('logAppName', self::$appname);
127129
}
130+
131+
public function testTheTransactionNameIsNullByDefault()
132+
{
133+
$handler = new StubNewRelicHandler();
134+
$handler->handle($this->getRecord(Logger::ERROR, 'log message'));
135+
136+
$this->assertEquals(null, self::$transactionName);
137+
}
138+
139+
public function testTheTransactionNameCanBeInjectedFromtheConstructor()
140+
{
141+
$handler = new StubNewRelicHandler(Logger::DEBUG, false, null, false, 'myTransaction');
142+
$handler->handle($this->getRecord(Logger::ERROR, 'log message'));
143+
144+
$this->assertEquals('myTransaction', self::$transactionName);
145+
}
146+
147+
public function testTheTransactionNameCanBeOverriddenFromEachLog()
148+
{
149+
$handler = new StubNewRelicHandler(Logger::DEBUG, false, null, false, 'myTransaction');
150+
$handler->handle($this->getRecord(Logger::ERROR, 'log message', array('transaction_name' => 'logTransactName')));
151+
152+
$this->assertEquals('logTransactName', self::$transactionName);
153+
}
128154
}
129155

130156
class StubNewRelicHandlerWithoutExtension extends NewRelicHandler
@@ -153,6 +179,11 @@ function newrelic_set_appname($appname)
153179
return NewRelicHandlerTest::$appname = $appname;
154180
}
155181

182+
function newrelic_name_transaction($transactionName)
183+
{
184+
return NewRelicHandlerTest::$transactionName = $transactionName;
185+
}
186+
156187
function newrelic_add_custom_parameter($key, $value)
157188
{
158189
NewRelicHandlerTest::$customParameters[$key] = $value;

0 commit comments

Comments
 (0)