NewRelicHandlerTest.php 5.8 KB

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