ElasticsearchHandler.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 Throwable;
  12. use RuntimeException;
  13. use Monolog\Logger;
  14. use Monolog\Formatter\FormatterInterface;
  15. use Monolog\Formatter\ElasticsearchFormatter;
  16. use InvalidArgumentException;
  17. use Elasticsearch\Common\Exceptions\RuntimeException as ElasticsearchRuntimeException;
  18. use Elasticsearch\Client;
  19. /**
  20. * Elasticsearch handler
  21. *
  22. * @link https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html
  23. *
  24. * Simple usage example:
  25. *
  26. * $client = \Elasticsearch\ClientBuilder::create()
  27. * ->setHosts($hosts)
  28. * ->build();
  29. *
  30. * $options = array(
  31. * 'index' => 'elastic_index_name',
  32. * 'type' => 'elastic_doc_type',
  33. * );
  34. * $handler = new ElasticsearchHandler($client, $options);
  35. * $log = new Logger('application');
  36. * $log->pushHandler($handler);
  37. *
  38. * @author Avtandil Kikabidze <akalongman@gmail.com>
  39. */
  40. class ElasticsearchHandler extends AbstractProcessingHandler
  41. {
  42. /**
  43. * @var Client
  44. */
  45. protected $client;
  46. /**
  47. * @var mixed[] Handler config options
  48. */
  49. protected $options = [];
  50. /**
  51. * @param Client $client Elasticsearch Client object
  52. * @param mixed[] $options Handler configuration
  53. */
  54. public function __construct(Client $client, array $options = [], $level = Logger::DEBUG, bool $bubble = true)
  55. {
  56. parent::__construct($level, $bubble);
  57. $this->client = $client;
  58. $this->options = array_merge(
  59. [
  60. 'index' => 'monolog', // Elastic index name
  61. 'type' => '_doc', // Elastic document type
  62. 'ignore_error' => false, // Suppress Elasticsearch exceptions
  63. ],
  64. $options
  65. );
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. protected function write(array $record): void
  71. {
  72. $this->bulkSend([$record['formatted']]);
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. public function setFormatter(FormatterInterface $formatter): HandlerInterface
  78. {
  79. if ($formatter instanceof ElasticsearchFormatter) {
  80. return parent::setFormatter($formatter);
  81. }
  82. throw new InvalidArgumentException('ElasticsearchHandler is only compatible with ElasticsearchFormatter');
  83. }
  84. /**
  85. * Getter options
  86. *
  87. * @return mixed[]
  88. */
  89. public function getOptions(): array
  90. {
  91. return $this->options;
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. protected function getDefaultFormatter(): FormatterInterface
  97. {
  98. return new ElasticsearchFormatter($this->options['index'], $this->options['type']);
  99. }
  100. /**
  101. * {@inheritDoc}
  102. */
  103. public function handleBatch(array $records): void
  104. {
  105. $documents = $this->getFormatter()->formatBatch($records);
  106. $this->bulkSend($documents);
  107. }
  108. /**
  109. * Use Elasticsearch bulk API to send list of documents
  110. *
  111. * @param array[] $records Records + _index/_type keys
  112. * @throws \RuntimeException
  113. */
  114. protected function bulkSend(array $records): void
  115. {
  116. try {
  117. $params = [
  118. 'body' => [],
  119. ];
  120. foreach ($records as $record) {
  121. $params['body'][] = [
  122. 'index' => [
  123. '_index' => $record['_index'],
  124. '_type' => $record['_type'],
  125. ],
  126. ];
  127. unset($record['_index'], $record['_type']);
  128. $params['body'][] = $record;
  129. }
  130. $responses = $this->client->bulk($params);
  131. if ($responses['errors'] === true) {
  132. throw $this->createExceptionFromResponses($responses);
  133. }
  134. } catch (Throwable $e) {
  135. if (! $this->options['ignore_error']) {
  136. throw new RuntimeException('Error sending messages to Elasticsearch', 0, $e);
  137. }
  138. }
  139. }
  140. /**
  141. * Creates elasticsearch exception from responses array
  142. *
  143. * Only the first error is converted into an exception.
  144. *
  145. * @param mixed[] $responses returned by $this->client->bulk()
  146. */
  147. protected function createExceptionFromResponses(array $responses): ElasticsearchRuntimeException
  148. {
  149. foreach ($responses['items'] ?? [] as $item) {
  150. if (isset($item['index']['error'])) {
  151. return $this->createExceptionFromError($item['index']['error']);
  152. }
  153. }
  154. return new ElasticsearchRuntimeException('Elasticsearch failed to index one or more records.');
  155. }
  156. /**
  157. * Creates elasticsearch exception from error array
  158. *
  159. * @param mixed[] $error
  160. */
  161. protected function createExceptionFromError(array $error): ElasticsearchRuntimeException
  162. {
  163. $previous = isset($error['caused_by']) ? $this->createExceptionFromError($error['caused_by']) : null;
  164. return new ElasticsearchRuntimeException($error['type'] . ': ' . $error['reason'], 0, $previous);
  165. }
  166. }