2012-08-23 00:43:49 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Monolog package.
|
|
|
|
*
|
|
|
|
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Monolog\Handler;
|
|
|
|
|
|
|
|
use Monolog\TestCase;
|
|
|
|
use Monolog\Logger;
|
|
|
|
|
|
|
|
class NewRelicHandlerTest extends TestCase
|
2013-07-28 23:19:20 +02:00
|
|
|
{
|
2013-08-26 13:20:13 +04:00
|
|
|
public static $appname;
|
2014-05-13 15:22:12 +10:00
|
|
|
public static $customParameters;
|
2014-12-18 00:16:03 +01:00
|
|
|
public static $transactionName;
|
2013-08-26 13:20:13 +04:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2014-05-13 15:22:12 +10:00
|
|
|
self::$appname = null;
|
|
|
|
self::$customParameters = array();
|
2014-12-18 00:16:03 +01:00
|
|
|
self::$transactionName = null;
|
2013-08-26 13:20:13 +04:00
|
|
|
}
|
|
|
|
|
2013-07-24 10:14:35 +04:00
|
|
|
/**
|
|
|
|
* @expectedException Monolog\Handler\MissingExtensionException
|
|
|
|
*/
|
|
|
|
public function testThehandlerThrowsAnExceptionIfTheNRExtensionIsNotLoaded()
|
|
|
|
{
|
|
|
|
$handler = new StubNewRelicHandlerWithoutExtension();
|
2013-07-28 23:19:20 +02:00
|
|
|
$handler->handle($this->getRecord(Logger::ERROR));
|
2013-07-24 10:14:35 +04:00
|
|
|
}
|
2013-07-28 23:19:20 +02:00
|
|
|
|
2013-07-24 10:14:35 +04:00
|
|
|
public function testThehandlerCanHandleTheRecord()
|
|
|
|
{
|
|
|
|
$handler = new StubNewRelicHandler();
|
2013-07-28 23:19:20 +02:00
|
|
|
$handler->handle($this->getRecord(Logger::ERROR));
|
2013-07-24 10:14:35 +04:00
|
|
|
}
|
2013-07-28 23:19:20 +02:00
|
|
|
|
2014-05-13 15:22:12 +10:00
|
|
|
public function testThehandlerCanAddContextParamsToTheNewRelicTrace()
|
2013-07-24 10:14:35 +04:00
|
|
|
{
|
|
|
|
$handler = new StubNewRelicHandler();
|
2013-07-28 23:19:20 +02:00
|
|
|
$handler->handle($this->getRecord(Logger::ERROR, 'log message', array('a' => 'b')));
|
2014-09-29 11:04:41 -05:00
|
|
|
$this->assertEquals(array('context_a' => 'b'), self::$customParameters);
|
2014-05-13 15:22:12 +10:00
|
|
|
}
|
|
|
|
|
2014-09-30 16:42:07 -05:00
|
|
|
public function testThehandlerCanAddExplodedContextParamsToTheNewRelicTrace()
|
|
|
|
{
|
|
|
|
$handler = new StubNewRelicHandler(Logger::ERROR, true, self::$appname, true);
|
|
|
|
$handler->handle($this->getRecord(
|
|
|
|
Logger::ERROR,
|
|
|
|
'log message',
|
|
|
|
array('a' => array('key1' => 'value1', 'key2' => 'value2'))
|
|
|
|
));
|
|
|
|
$this->assertEquals(
|
|
|
|
array('context_a_key1' => 'value1', 'context_a_key2' => 'value2'),
|
|
|
|
self::$customParameters
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-13 15:22:12 +10:00
|
|
|
public function testThehandlerCanAddExtraParamsToTheNewRelicTrace()
|
|
|
|
{
|
|
|
|
$record = $this->getRecord(Logger::ERROR, 'log message');
|
|
|
|
$record['extra'] = array('c' => 'd');
|
|
|
|
|
|
|
|
$handler = new StubNewRelicHandler();
|
|
|
|
$handler->handle($record);
|
|
|
|
|
2014-09-29 11:04:41 -05:00
|
|
|
$this->assertEquals(array('extra_c' => 'd'), self::$customParameters);
|
2014-05-13 15:22:12 +10:00
|
|
|
}
|
|
|
|
|
2014-09-30 16:42:07 -05:00
|
|
|
public function testThehandlerCanAddExplodedExtraParamsToTheNewRelicTrace()
|
|
|
|
{
|
|
|
|
$record = $this->getRecord(Logger::ERROR, 'log message');
|
|
|
|
$record['extra'] = array('c' => array('key1' => 'value1', 'key2' => 'value2'));
|
|
|
|
|
|
|
|
$handler = new StubNewRelicHandler(Logger::ERROR, true, self::$appname, true);
|
|
|
|
$handler->handle($record);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array('extra_c_key1' => 'value1', 'extra_c_key2' => 'value2'),
|
|
|
|
self::$customParameters
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-13 15:22:12 +10:00
|
|
|
public function testThehandlerCanAddExtraContextAndParamsToTheNewRelicTrace()
|
|
|
|
{
|
|
|
|
$record = $this->getRecord(Logger::ERROR, 'log message', array('a' => 'b'));
|
|
|
|
$record['extra'] = array('c' => 'd');
|
|
|
|
|
|
|
|
$handler = new StubNewRelicHandler();
|
|
|
|
$handler->handle($record);
|
|
|
|
|
|
|
|
$expected = array(
|
2014-09-29 11:04:41 -05:00
|
|
|
'context_a' => 'b',
|
|
|
|
'extra_c' => 'd',
|
2014-05-13 15:22:12 +10:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, self::$customParameters);
|
2013-07-24 10:14:35 +04:00
|
|
|
}
|
2013-08-26 12:04:22 +04:00
|
|
|
|
2013-08-26 13:20:13 +04:00
|
|
|
public function testTheAppNameIsNullByDefault()
|
|
|
|
{
|
|
|
|
$handler = new StubNewRelicHandler();
|
|
|
|
$handler->handle($this->getRecord(Logger::ERROR, 'log message'));
|
|
|
|
|
2014-05-13 15:22:12 +10:00
|
|
|
$this->assertEquals(null, self::$appname);
|
2013-08-26 13:20:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTheAppNameCanBeInjectedFromtheConstructor()
|
|
|
|
{
|
2014-09-30 13:59:45 +01:00
|
|
|
$handler = new StubNewRelicHandler(Logger::DEBUG, false, 'myAppName');
|
2013-08-26 13:20:13 +04:00
|
|
|
$handler->handle($this->getRecord(Logger::ERROR, 'log message'));
|
|
|
|
|
2014-05-13 15:22:12 +10:00
|
|
|
$this->assertEquals('myAppName', self::$appname);
|
2013-08-26 13:20:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTheAppNameCanBeOverriddenFromEachLog()
|
|
|
|
{
|
2014-09-30 13:59:45 +01:00
|
|
|
$handler = new StubNewRelicHandler(Logger::DEBUG, false, 'myAppName');
|
2013-08-26 13:20:13 +04:00
|
|
|
$handler->handle($this->getRecord(Logger::ERROR, 'log message', array('appname' => 'logAppName')));
|
|
|
|
|
2014-05-13 15:22:12 +10:00
|
|
|
$this->assertEquals('logAppName', self::$appname);
|
2013-08-26 13:20:13 +04:00
|
|
|
}
|
2014-12-18 00:16:03 +01:00
|
|
|
|
|
|
|
public function testTheTransactionNameIsNullByDefault()
|
|
|
|
{
|
|
|
|
$handler = new StubNewRelicHandler();
|
|
|
|
$handler->handle($this->getRecord(Logger::ERROR, 'log message'));
|
|
|
|
|
|
|
|
$this->assertEquals(null, self::$transactionName);
|
|
|
|
}
|
|
|
|
|
2015-03-05 01:11:15 +00:00
|
|
|
public function testTheTransactionNameCanBeInjectedFromTheConstructor()
|
2014-12-18 00:16:03 +01:00
|
|
|
{
|
|
|
|
$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);
|
|
|
|
}
|
2013-07-24 10:14:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
class StubNewRelicHandlerWithoutExtension extends NewRelicHandler
|
2012-08-23 00:43:49 +04:00
|
|
|
{
|
2013-07-24 10:14:35 +04:00
|
|
|
protected function isNewRelicEnabled()
|
2012-08-23 00:43:49 +04:00
|
|
|
{
|
2013-07-24 10:14:35 +04:00
|
|
|
return false;
|
2012-08-23 00:43:49 +04:00
|
|
|
}
|
|
|
|
}
|
2013-07-24 10:14:35 +04:00
|
|
|
|
|
|
|
class StubNewRelicHandler extends NewRelicHandler
|
|
|
|
{
|
|
|
|
protected function isNewRelicEnabled()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 00:08:51 +02:00
|
|
|
function newrelic_notice_error()
|
|
|
|
{
|
2013-07-24 10:14:35 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-26 13:20:13 +04:00
|
|
|
function newrelic_set_appname($appname)
|
2013-08-26 12:04:22 +04:00
|
|
|
{
|
2013-08-26 13:20:13 +04:00
|
|
|
return NewRelicHandlerTest::$appname = $appname;
|
2013-08-26 12:04:22 +04:00
|
|
|
}
|
|
|
|
|
2014-12-18 00:16:03 +01:00
|
|
|
function newrelic_name_transaction($transactionName)
|
|
|
|
{
|
|
|
|
return NewRelicHandlerTest::$transactionName = $transactionName;
|
|
|
|
}
|
|
|
|
|
2014-05-13 15:22:12 +10:00
|
|
|
function newrelic_add_custom_parameter($key, $value)
|
2013-07-29 00:08:51 +02:00
|
|
|
{
|
2014-05-13 15:22:12 +10:00
|
|
|
NewRelicHandlerTest::$customParameters[$key] = $value;
|
2014-06-04 18:30:04 +02:00
|
|
|
|
2013-07-24 10:14:35 +04:00
|
|
|
return true;
|
|
|
|
}
|