NewRelicHandler.php 6.1 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\Logger;
  12. use Monolog\Utils;
  13. use Monolog\Formatter\NormalizerFormatter;
  14. use Monolog\Formatter\FormatterInterface;
  15. /**
  16. * Class to record a log on a NewRelic application.
  17. * Enabling New Relic High Security mode may prevent capture of useful information.
  18. *
  19. * This handler requires a NormalizerFormatter to function and expects an array in $record['formatted']
  20. *
  21. * @see https://docs.newrelic.com/docs/agents/php-agent
  22. * @see https://docs.newrelic.com/docs/accounts-partnerships/accounts/security/high-security
  23. */
  24. class NewRelicHandler extends AbstractProcessingHandler
  25. {
  26. /**
  27. * Name of the New Relic application that will receive logs from this handler.
  28. *
  29. * @var ?string
  30. */
  31. protected $appName;
  32. /**
  33. * Name of the current transaction
  34. *
  35. * @var ?string
  36. */
  37. protected $transactionName;
  38. /**
  39. * Some context and extra data is passed into the handler as arrays of values. Do we send them as is
  40. * (useful if we are using the API), or explode them for display on the NewRelic RPM website?
  41. *
  42. * @var bool
  43. */
  44. protected $explodeArrays;
  45. /**
  46. * {@inheritDoc}
  47. *
  48. * @param string|null $appName
  49. * @param bool $explodeArrays
  50. * @param string|null $transactionName
  51. */
  52. public function __construct(
  53. $level = Logger::ERROR,
  54. bool $bubble = true,
  55. ?string $appName = null,
  56. bool $explodeArrays = false,
  57. ?string $transactionName = null
  58. ) {
  59. parent::__construct($level, $bubble);
  60. $this->appName = $appName;
  61. $this->explodeArrays = $explodeArrays;
  62. $this->transactionName = $transactionName;
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. protected function write(array $record): void
  68. {
  69. if (!$this->isNewRelicEnabled()) {
  70. throw new MissingExtensionException('The newrelic PHP extension is required to use the NewRelicHandler');
  71. }
  72. if ($appName = $this->getAppName($record['context'])) {
  73. $this->setNewRelicAppName($appName);
  74. }
  75. if ($transactionName = $this->getTransactionName($record['context'])) {
  76. $this->setNewRelicTransactionName($transactionName);
  77. unset($record['formatted']['context']['transaction_name']);
  78. }
  79. if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Throwable) {
  80. newrelic_notice_error($record['message'], $record['context']['exception']);
  81. unset($record['formatted']['context']['exception']);
  82. } else {
  83. newrelic_notice_error($record['message']);
  84. }
  85. if (isset($record['formatted']['context']) && is_array($record['formatted']['context'])) {
  86. foreach ($record['formatted']['context'] as $key => $parameter) {
  87. if (is_array($parameter) && $this->explodeArrays) {
  88. foreach ($parameter as $paramKey => $paramValue) {
  89. $this->setNewRelicParameter('context_' . $key . '_' . $paramKey, $paramValue);
  90. }
  91. } else {
  92. $this->setNewRelicParameter('context_' . $key, $parameter);
  93. }
  94. }
  95. }
  96. if (isset($record['formatted']['extra']) && is_array($record['formatted']['extra'])) {
  97. foreach ($record['formatted']['extra'] as $key => $parameter) {
  98. if (is_array($parameter) && $this->explodeArrays) {
  99. foreach ($parameter as $paramKey => $paramValue) {
  100. $this->setNewRelicParameter('extra_' . $key . '_' . $paramKey, $paramValue);
  101. }
  102. } else {
  103. $this->setNewRelicParameter('extra_' . $key, $parameter);
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * Checks whether the NewRelic extension is enabled in the system.
  110. *
  111. * @return bool
  112. */
  113. protected function isNewRelicEnabled(): bool
  114. {
  115. return extension_loaded('newrelic');
  116. }
  117. /**
  118. * Returns the appname where this log should be sent. Each log can override the default appname, set in this
  119. * handler's constructor, by providing the appname in it's context.
  120. *
  121. * @param mixed[] $context
  122. */
  123. protected function getAppName(array $context): ?string
  124. {
  125. if (isset($context['appname'])) {
  126. return $context['appname'];
  127. }
  128. return $this->appName;
  129. }
  130. /**
  131. * Returns the name of the current transaction. Each log can override the default transaction name, set in this
  132. * handler's constructor, by providing the transaction_name in it's context
  133. *
  134. * @param mixed[] $context
  135. */
  136. protected function getTransactionName(array $context): ?string
  137. {
  138. if (isset($context['transaction_name'])) {
  139. return $context['transaction_name'];
  140. }
  141. return $this->transactionName;
  142. }
  143. /**
  144. * Sets the NewRelic application that should receive this log.
  145. */
  146. protected function setNewRelicAppName(string $appName): void
  147. {
  148. newrelic_set_appname($appName);
  149. }
  150. /**
  151. * Overwrites the name of the current transaction
  152. */
  153. protected function setNewRelicTransactionName(string $transactionName): void
  154. {
  155. newrelic_name_transaction($transactionName);
  156. }
  157. /**
  158. * @param string $key
  159. * @param mixed $value
  160. */
  161. protected function setNewRelicParameter(string $key, $value): void
  162. {
  163. if (null === $value || is_scalar($value)) {
  164. newrelic_add_custom_parameter($key, $value);
  165. } else {
  166. newrelic_add_custom_parameter($key, Utils::jsonEncode($value, null, true));
  167. }
  168. }
  169. /**
  170. * {@inheritDoc}
  171. */
  172. protected function getDefaultFormatter(): FormatterInterface
  173. {
  174. return new NormalizerFormatter();
  175. }
  176. }