ElasticsearchHandler.php 6.5 KB

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