2
0

NewRelicHandlerTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Formatter\LineFormatter;
  12. use Monolog\Test\TestCase;
  13. use Monolog\Logger;
  14. class NewRelicHandlerTest extends TestCase
  15. {
  16. public static $appname;
  17. public static $customParameters;
  18. public static $transactionName;
  19. public function setUp()
  20. {
  21. self::$appname = null;
  22. self::$customParameters = [];
  23. self::$transactionName = null;
  24. }
  25. /**
  26. * @expectedException Monolog\Handler\MissingExtensionException
  27. */
  28. public function testThehandlerThrowsAnExceptionIfTheNRExtensionIsNotLoaded()
  29. {
  30. $handler = new StubNewRelicHandlerWithoutExtension();
  31. $handler->handle($this->getRecord(Logger::ERROR));
  32. }
  33. public function testThehandlerCanHandleTheRecord()
  34. {
  35. $handler = new StubNewRelicHandler();
  36. $handler->handle($this->getRecord(Logger::ERROR));
  37. }
  38. public function testThehandlerCanAddContextParamsToTheNewRelicTrace()
  39. {
  40. $handler = new StubNewRelicHandler();
  41. $handler->handle($this->getRecord(Logger::ERROR, 'log message', ['a' => 'b']));
  42. $this->assertEquals(['context_a' => 'b'], self::$customParameters);
  43. }
  44. public function testThehandlerCanAddExplodedContextParamsToTheNewRelicTrace()
  45. {
  46. $handler = new StubNewRelicHandler(Logger::ERROR, true, self::$appname, true);
  47. $handler->handle($this->getRecord(
  48. Logger::ERROR,
  49. 'log message',
  50. ['a' => ['key1' => 'value1', 'key2' => 'value2']]
  51. ));
  52. $this->assertEquals(
  53. ['context_a_key1' => 'value1', 'context_a_key2' => 'value2'],
  54. self::$customParameters
  55. );
  56. }
  57. public function testThehandlerCanAddExtraParamsToTheNewRelicTrace()
  58. {
  59. $record = $this->getRecord(Logger::ERROR, 'log message');
  60. $record['extra'] = ['c' => 'd'];
  61. $handler = new StubNewRelicHandler();
  62. $handler->handle($record);
  63. $this->assertEquals(['extra_c' => 'd'], self::$customParameters);
  64. }
  65. public function testThehandlerCanAddExplodedExtraParamsToTheNewRelicTrace()
  66. {
  67. $record = $this->getRecord(Logger::ERROR, 'log message');
  68. $record['extra'] = ['c' => ['key1' => 'value1', 'key2' => 'value2']];
  69. $handler = new StubNewRelicHandler(Logger::ERROR, true, self::$appname, true);
  70. $handler->handle($record);
  71. $this->assertEquals(
  72. ['extra_c_key1' => 'value1', 'extra_c_key2' => 'value2'],
  73. self::$customParameters
  74. );
  75. }
  76. public function testThehandlerCanAddExtraContextAndParamsToTheNewRelicTrace()
  77. {
  78. $record = $this->getRecord(Logger::ERROR, 'log message', ['a' => 'b']);
  79. $record['extra'] = ['c' => 'd'];
  80. $handler = new StubNewRelicHandler();
  81. $handler->handle($record);
  82. $expected = [
  83. 'context_a' => 'b',
  84. 'extra_c' => 'd',
  85. ];
  86. $this->assertEquals($expected, self::$customParameters);
  87. }
  88. public function testThehandlerCanHandleTheRecordsFormattedUsingTheLineFormatter()
  89. {
  90. $handler = new StubNewRelicHandler();
  91. $handler->setFormatter(new LineFormatter());
  92. $handler->handle($this->getRecord(Logger::ERROR));
  93. }
  94. public function testTheAppNameIsNullByDefault()
  95. {
  96. $handler = new StubNewRelicHandler();
  97. $handler->handle($this->getRecord(Logger::ERROR, 'log message'));
  98. $this->assertEquals(null, self::$appname);
  99. }
  100. public function testTheAppNameCanBeInjectedFromtheConstructor()
  101. {
  102. $handler = new StubNewRelicHandler(Logger::DEBUG, false, 'myAppName');
  103. $handler->handle($this->getRecord(Logger::ERROR, 'log message'));
  104. $this->assertEquals('myAppName', self::$appname);
  105. }
  106. public function testTheAppNameCanBeOverriddenFromEachLog()
  107. {
  108. $handler = new StubNewRelicHandler(Logger::DEBUG, false, 'myAppName');
  109. $handler->handle($this->getRecord(Logger::ERROR, 'log message', ['appname' => 'logAppName']));
  110. $this->assertEquals('logAppName', self::$appname);
  111. }
  112. public function testTheTransactionNameIsNullByDefault()
  113. {
  114. $handler = new StubNewRelicHandler();
  115. $handler->handle($this->getRecord(Logger::ERROR, 'log message'));
  116. $this->assertEquals(null, self::$transactionName);
  117. }
  118. public function testTheTransactionNameCanBeInjectedFromTheConstructor()
  119. {
  120. $handler = new StubNewRelicHandler(Logger::DEBUG, false, null, false, 'myTransaction');
  121. $handler->handle($this->getRecord(Logger::ERROR, 'log message'));
  122. $this->assertEquals('myTransaction', self::$transactionName);
  123. }
  124. public function testTheTransactionNameCanBeOverriddenFromEachLog()
  125. {
  126. $handler = new StubNewRelicHandler(Logger::DEBUG, false, null, false, 'myTransaction');
  127. $handler->handle($this->getRecord(Logger::ERROR, 'log message', ['transaction_name' => 'logTransactName']));
  128. $this->assertEquals('logTransactName', self::$transactionName);
  129. }
  130. }
  131. class StubNewRelicHandlerWithoutExtension extends NewRelicHandler
  132. {
  133. protected function isNewRelicEnabled(): bool
  134. {
  135. return false;
  136. }
  137. }
  138. class StubNewRelicHandler extends NewRelicHandler
  139. {
  140. protected function isNewRelicEnabled(): bool
  141. {
  142. return true;
  143. }
  144. }
  145. function newrelic_notice_error()
  146. {
  147. return true;
  148. }
  149. function newrelic_set_appname($appname)
  150. {
  151. return NewRelicHandlerTest::$appname = $appname;
  152. }
  153. function newrelic_name_transaction($transactionName)
  154. {
  155. return NewRelicHandlerTest::$transactionName = $transactionName;
  156. }
  157. function newrelic_add_custom_parameter($key, $value)
  158. {
  159. NewRelicHandlerTest::$customParameters[$key] = $value;
  160. return true;
  161. }