1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-04 12:17:35 +02:00

added support for newrelic_name_transaction() to NewRelicHandler

This commit is contained in:
Florian Plattner
2014-12-18 00:16:03 +01:00
parent 4119844a43
commit e6e2457c55
2 changed files with 69 additions and 2 deletions

View File

@@ -27,6 +27,13 @@ class NewRelicHandler extends AbstractProcessingHandler
*/ */
protected $appName; protected $appName;
/**
* Name of the current transaction
*
* @var string
*/
protected $transactionName;
/** /**
* Some context and extra data is passed into the handler as arrays of values. Do we send them as is * Some context and extra data is passed into the handler as arrays of values. Do we send them as is
* (useful if we are using the API), or explode them for display on the NewRelic RPM website? * (useful if we are using the API), or explode them for display on the NewRelic RPM website?
@@ -41,12 +48,18 @@ class NewRelicHandler extends AbstractProcessingHandler
* @param string $appName * @param string $appName
* @param boolean $implodeArrays * @param boolean $implodeArrays
*/ */
public function __construct($level = Logger::ERROR, $bubble = true, $appName = null, $explodeArrays = false) public function __construct(
{ $level = Logger::ERROR,
$bubble = true,
$appName = null,
$explodeArrays = false,
$transactionName = null
) {
parent::__construct($level, $bubble); parent::__construct($level, $bubble);
$this->appName = $appName; $this->appName = $appName;
$this->explodeArrays = $explodeArrays; $this->explodeArrays = $explodeArrays;
$this->transactionName = $transactionName;
} }
/** /**
@@ -62,6 +75,10 @@ class NewRelicHandler extends AbstractProcessingHandler
$this->setNewRelicAppName($appName); $this->setNewRelicAppName($appName);
} }
if ($transactionName = $this->getTransactionName($record['context'])) {
$this->setNewRelicTransactionName($transactionName);
}
if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Exception) { if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Exception) {
newrelic_notice_error($record['message'], $record['context']['exception']); newrelic_notice_error($record['message'], $record['context']['exception']);
unset($record['context']['exception']); unset($record['context']['exception']);
@@ -69,6 +86,11 @@ class NewRelicHandler extends AbstractProcessingHandler
newrelic_notice_error($record['message']); newrelic_notice_error($record['message']);
} }
if (isset($record['context']['transaction_name'])) {
newrelic_name_transaction($record['context']['transaction_name']);
unset($record['context']['transaction_name']);
}
foreach ($record['context'] as $key => $parameter) { foreach ($record['context'] as $key => $parameter) {
if (is_array($parameter) && $this->explodeArrays) { if (is_array($parameter) && $this->explodeArrays) {
foreach ($parameter as $paramKey => $paramValue) { foreach ($parameter as $paramKey => $paramValue) {
@@ -116,6 +138,15 @@ class NewRelicHandler extends AbstractProcessingHandler
return $this->appName; return $this->appName;
} }
protected function getTransactionName(array $context)
{
if (isset($context['transaction_name'])) {
return $context['transaction_name'];
}
return $this->transactionName;
}
/** /**
* Sets the NewRelic application that should receive this log. * Sets the NewRelic application that should receive this log.
* *
@@ -125,4 +156,9 @@ class NewRelicHandler extends AbstractProcessingHandler
{ {
newrelic_set_appname($appName); newrelic_set_appname($appName);
} }
protected function setNewRelicTransactionName($transactionName)
{
newrelic_name_transaction($transactionName);
}
} }

View File

@@ -18,11 +18,13 @@ class NewRelicHandlerTest extends TestCase
{ {
public static $appname; public static $appname;
public static $customParameters; public static $customParameters;
public static $transactionName;
public function setUp() public function setUp()
{ {
self::$appname = null; self::$appname = null;
self::$customParameters = array(); self::$customParameters = array();
self::$transactionName = null;
} }
/** /**
@@ -125,6 +127,30 @@ class NewRelicHandlerTest extends TestCase
$this->assertEquals('logAppName', self::$appname); $this->assertEquals('logAppName', self::$appname);
} }
public function testTheTransactionNameIsNullByDefault()
{
$handler = new StubNewRelicHandler();
$handler->handle($this->getRecord(Logger::ERROR, 'log message'));
$this->assertEquals(null, self::$transactionName);
}
public function testTheTransactionNameCanBeInjectedFromtheConstructor()
{
$handler = new StubNewRelicHandler(Logger::DEBUG, false, null, false, 'myTransaction');
$handler->handle($this->getRecord(Logger::ERROR, 'log message'));
$this->assertEquals('myTransaction', self::$transactionName);
}
public function testTheTransactionNameCanBeOverriddenFromEachLog()
{
$handler = new StubNewRelicHandler(Logger::DEBUG, false, null, false, 'myTransaction');
$handler->handle($this->getRecord(Logger::ERROR, 'log message', array('transaction_name' => 'logTransactName')));
$this->assertEquals('logTransactName', self::$transactionName);
}
} }
class StubNewRelicHandlerWithoutExtension extends NewRelicHandler class StubNewRelicHandlerWithoutExtension extends NewRelicHandler
@@ -153,6 +179,11 @@ function newrelic_set_appname($appname)
return NewRelicHandlerTest::$appname = $appname; return NewRelicHandlerTest::$appname = $appname;
} }
function newrelic_name_transaction($transactionName)
{
return NewRelicHandlerTest::$transactionName = $transactionName;
}
function newrelic_add_custom_parameter($key, $value) function newrelic_add_custom_parameter($key, $value)
{ {
NewRelicHandlerTest::$customParameters[$key] = $value; NewRelicHandlerTest::$customParameters[$key] = $value;