NewRelicHandlerTest.php 5.8 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\Level;
  14. class NewRelicHandlerTest extends TestCase
  15. {
  16. public static $appname;
  17. public static $customParameters;
  18. public static $transactionName;
  19. public function setUp(): void
  20. {
  21. self::$appname = null;
  22. self::$customParameters = [];
  23. self::$transactionName = null;
  24. }
  25. public function testThehandlerThrowsAnExceptionIfTheNRExtensionIsNotLoaded()
  26. {
  27. $handler = new StubNewRelicHandlerWithoutExtension();
  28. $this->expectException(MissingExtensionException::class);
  29. $handler->handle($this->getRecord(Level::Error));
  30. }
  31. public function testThehandlerCanHandleTheRecord()
  32. {
  33. $handler = new StubNewRelicHandler();
  34. $handler->handle($this->getRecord(Level::Error));
  35. }
  36. public function testThehandlerCanAddContextParamsToTheNewRelicTrace()
  37. {
  38. $handler = new StubNewRelicHandler();
  39. $handler->handle($this->getRecord(Level::Error, 'log message', ['a' => 'b']));
  40. $this->assertEquals(['context_a' => 'b'], self::$customParameters);
  41. }
  42. public function testThehandlerCanAddExplodedContextParamsToTheNewRelicTrace()
  43. {
  44. $handler = new StubNewRelicHandler(Level::Error, true, self::$appname, true);
  45. $handler->handle($this->getRecord(
  46. Level::Error,
  47. 'log message',
  48. ['a' => ['key1' => 'value1', 'key2' => 'value2']]
  49. ));
  50. $this->assertEquals(
  51. ['context_a_key1' => 'value1', 'context_a_key2' => 'value2'],
  52. self::$customParameters
  53. );
  54. }
  55. public function testThehandlerCanAddExtraParamsToTheNewRelicTrace()
  56. {
  57. $record = $this->getRecord(Level::Error, 'log message');
  58. $record->extra = ['c' => 'd'];
  59. $handler = new StubNewRelicHandler();
  60. $handler->handle($record);
  61. $this->assertEquals(['extra_c' => 'd'], self::$customParameters);
  62. }
  63. public function testThehandlerCanAddExplodedExtraParamsToTheNewRelicTrace()
  64. {
  65. $record = $this->getRecord(Level::Error, 'log message');
  66. $record->extra = ['c' => ['key1' => 'value1', 'key2' => 'value2']];
  67. $handler = new StubNewRelicHandler(Level::Error, true, self::$appname, true);
  68. $handler->handle($record);
  69. $this->assertEquals(
  70. ['extra_c_key1' => 'value1', 'extra_c_key2' => 'value2'],
  71. self::$customParameters
  72. );
  73. }
  74. public function testThehandlerCanAddExtraContextAndParamsToTheNewRelicTrace()
  75. {
  76. $record = $this->getRecord(Level::Error, 'log message', ['a' => 'b']);
  77. $record->extra = ['c' => 'd'];
  78. $handler = new StubNewRelicHandler();
  79. $handler->handle($record);
  80. $expected = [
  81. 'context_a' => 'b',
  82. 'extra_c' => 'd',
  83. ];
  84. $this->assertEquals($expected, self::$customParameters);
  85. }
  86. public function testThehandlerCanHandleTheRecordsFormattedUsingTheLineFormatter()
  87. {
  88. $handler = new StubNewRelicHandler();
  89. $handler->setFormatter(new LineFormatter());
  90. $handler->handle($this->getRecord(Level::Error));
  91. }
  92. public function testTheAppNameIsNullByDefault()
  93. {
  94. $handler = new StubNewRelicHandler();
  95. $handler->handle($this->getRecord(Level::Error, 'log message'));
  96. $this->assertEquals(null, self::$appname);
  97. }
  98. public function testTheAppNameCanBeInjectedFromtheConstructor()
  99. {
  100. $handler = new StubNewRelicHandler(Level::Debug, false, 'myAppName');
  101. $handler->handle($this->getRecord(Level::Error, 'log message'));
  102. $this->assertEquals('myAppName', self::$appname);
  103. }
  104. public function testTheAppNameCanBeOverriddenFromEachLog()
  105. {
  106. $handler = new StubNewRelicHandler(Level::Debug, false, 'myAppName');
  107. $handler->handle($this->getRecord(Level::Error, 'log message', ['appname' => 'logAppName']));
  108. $this->assertEquals('logAppName', self::$appname);
  109. }
  110. public function testTheTransactionNameIsNullByDefault()
  111. {
  112. $handler = new StubNewRelicHandler();
  113. $handler->handle($this->getRecord(Level::Error, 'log message'));
  114. $this->assertEquals(null, self::$transactionName);
  115. }
  116. public function testTheTransactionNameCanBeInjectedFromTheConstructor()
  117. {
  118. $handler = new StubNewRelicHandler(Level::Debug, false, null, false, 'myTransaction');
  119. $handler->handle($this->getRecord(Level::Error, 'log message'));
  120. $this->assertEquals('myTransaction', self::$transactionName);
  121. }
  122. public function testTheTransactionNameCanBeOverriddenFromEachLog()
  123. {
  124. $handler = new StubNewRelicHandler(Level::Debug, false, null, false, 'myTransaction');
  125. $handler->handle($this->getRecord(Level::Error, 'log message', ['transaction_name' => 'logTransactName']));
  126. $this->assertEquals('logTransactName', self::$transactionName);
  127. }
  128. }
  129. class StubNewRelicHandlerWithoutExtension extends NewRelicHandler
  130. {
  131. protected function isNewRelicEnabled(): bool
  132. {
  133. return false;
  134. }
  135. }
  136. class StubNewRelicHandler extends NewRelicHandler
  137. {
  138. protected function isNewRelicEnabled(): bool
  139. {
  140. return true;
  141. }
  142. }
  143. function newrelic_notice_error()
  144. {
  145. return true;
  146. }
  147. function newrelic_set_appname($appname)
  148. {
  149. return NewRelicHandlerTest::$appname = $appname;
  150. }
  151. function newrelic_name_transaction($transactionName)
  152. {
  153. return NewRelicHandlerTest::$transactionName = $transactionName;
  154. }
  155. function newrelic_add_custom_parameter($key, $value)
  156. {
  157. NewRelicHandlerTest::$customParameters[$key] = $value;
  158. return true;
  159. }