ElasticsearchHandlerTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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\ElasticsearchFormatter;
  12. use Monolog\Formatter\NormalizerFormatter;
  13. use Monolog\Test\TestCase;
  14. use Monolog\Level;
  15. use Elasticsearch\Client;
  16. use Elastic\Elasticsearch\Client as Client8;
  17. use Elasticsearch\ClientBuilder;
  18. use Elastic\Elasticsearch\ClientBuilder as ClientBuilder8;
  19. /**
  20. * @group Elasticsearch
  21. */
  22. class ElasticsearchHandlerTest extends TestCase
  23. {
  24. protected Client|Client8 $client;
  25. /**
  26. * @var array Default handler options
  27. */
  28. protected array $options = [
  29. 'index' => 'my_index',
  30. 'type' => 'doc_type',
  31. 'op_type' => 'index',
  32. ];
  33. public function setUp(): void
  34. {
  35. $hosts = ['http://elastic:changeme@127.0.0.1:9200'];
  36. $this->client = $this->getClientBuilder()
  37. ->setHosts($hosts)
  38. ->build();
  39. try {
  40. $this->client->info();
  41. } catch (\Throwable $e) {
  42. $this->markTestSkipped('Could not connect to Elasticsearch on 127.0.0.1:9200');
  43. }
  44. }
  45. public function tearDown(): void
  46. {
  47. parent::tearDown();
  48. unset($this->client);
  49. }
  50. /**
  51. * @covers Monolog\Handler\ElasticsearchHandler::setFormatter
  52. */
  53. public function testSetFormatter()
  54. {
  55. $handler = new ElasticsearchHandler($this->client);
  56. $formatter = new ElasticsearchFormatter('index_new', 'type_new');
  57. $handler->setFormatter($formatter);
  58. $this->assertInstanceOf('Monolog\Formatter\ElasticsearchFormatter', $handler->getFormatter());
  59. $this->assertEquals('index_new', $handler->getFormatter()->getIndex());
  60. $this->assertEquals('type_new', $handler->getFormatter()->getType());
  61. }
  62. /**
  63. * @covers Monolog\Handler\ElasticsearchHandler::setFormatter
  64. */
  65. public function testSetFormatterInvalid()
  66. {
  67. $handler = new ElasticsearchHandler($this->client);
  68. $formatter = new NormalizerFormatter();
  69. $this->expectException(\InvalidArgumentException::class);
  70. $this->expectExceptionMessage('ElasticsearchHandler is only compatible with ElasticsearchFormatter');
  71. $handler->setFormatter($formatter);
  72. }
  73. /**
  74. * @covers Monolog\Handler\ElasticsearchHandler::__construct
  75. * @covers Monolog\Handler\ElasticsearchHandler::getOptions
  76. */
  77. public function testOptions()
  78. {
  79. $expected = [
  80. 'index' => $this->options['index'],
  81. 'type' => $this->options['type'],
  82. 'ignore_error' => false,
  83. 'op_type' => $this->options['op_type'],
  84. ];
  85. if ($this->client instanceof Client8 || $this->client::VERSION[0] === '7') {
  86. $expected['type'] = '_doc';
  87. }
  88. $handler = new ElasticsearchHandler($this->client, $this->options);
  89. $this->assertEquals($expected, $handler->getOptions());
  90. }
  91. /**
  92. * @covers Monolog\Handler\ElasticsearchHandler::bulkSend
  93. * @dataProvider providerTestConnectionErrors
  94. */
  95. public function testConnectionErrors($ignore, $expectedError)
  96. {
  97. $hosts = ['http://127.0.0.1:1'];
  98. $client = $this->getClientBuilder()
  99. ->setHosts($hosts)
  100. ->build();
  101. $handlerOpts = ['ignore_error' => $ignore];
  102. $handler = new ElasticsearchHandler($client, $handlerOpts);
  103. if ($expectedError) {
  104. $this->expectException($expectedError[0]);
  105. $this->expectExceptionMessage($expectedError[1]);
  106. $handler->handle($this->getRecord());
  107. } else {
  108. $this->assertFalse($handler->handle($this->getRecord()));
  109. }
  110. }
  111. public static function providerTestConnectionErrors(): array
  112. {
  113. return [
  114. [false, ['RuntimeException', 'Error sending messages to Elasticsearch']],
  115. [true, false],
  116. ];
  117. }
  118. /**
  119. * Integration test using localhost Elasticsearch server
  120. *
  121. * @covers Monolog\Handler\ElasticsearchHandler::__construct
  122. * @covers Monolog\Handler\ElasticsearchHandler::handleBatch
  123. * @covers Monolog\Handler\ElasticsearchHandler::bulkSend
  124. * @covers Monolog\Handler\ElasticsearchHandler::getDefaultFormatter
  125. */
  126. public function testHandleBatchIntegration()
  127. {
  128. $msg = $this->getRecord(Level::Error, 'log', context: ['foo' => 7, 'bar', 'class' => new \stdClass], datetime: new \DateTimeImmutable("@0"));
  129. $expected = $msg->toArray();
  130. $expected['datetime'] = $msg['datetime']->format(\DateTime::ISO8601);
  131. $expected['context'] = [
  132. 'class' => ["stdClass" => []],
  133. 'foo' => 7,
  134. 0 => 'bar',
  135. ];
  136. $hosts = ['http://elastic:changeme@127.0.0.1:9200'];
  137. $client = $this->getClientBuilder()
  138. ->setHosts($hosts)
  139. ->build();
  140. $handler = new ElasticsearchHandler($client, $this->options);
  141. $handler->handleBatch([$msg]);
  142. // check document id from ES server response
  143. if ($client instanceof Client8) {
  144. $messageBody = $client->getTransport()->getLastResponse()->getBody();
  145. $info = json_decode((string) $messageBody, true);
  146. $this->assertNotNull($info, 'Decoding failed');
  147. $documentId = $this->getCreatedDocIdV8($info);
  148. $this->assertNotEmpty($documentId, 'No elastic document id received');
  149. } else {
  150. $documentId = $this->getCreatedDocId($client->transport->getLastConnection()->getLastRequestInfo());
  151. $this->assertNotEmpty($documentId, 'No elastic document id received');
  152. }
  153. // retrieve document source from ES and validate
  154. $document = $this->getDocSourceFromElastic(
  155. $client,
  156. $this->options['index'],
  157. $this->options['type'],
  158. $documentId
  159. );
  160. $this->assertEquals($expected, $document);
  161. // remove test index from ES
  162. $client->indices()->delete(['index' => $this->options['index']]);
  163. }
  164. /**
  165. * Return last created document id from ES response
  166. *
  167. * @param array $info Elasticsearch last request info
  168. */
  169. protected function getCreatedDocId(array $info): ?string
  170. {
  171. $data = json_decode($info['response']['body'], true);
  172. if (!empty($data['items'][0]['index']['_id'])) {
  173. return $data['items'][0]['index']['_id'];
  174. }
  175. return null;
  176. }
  177. /**
  178. * Return last created document id from ES response
  179. *
  180. * @param array $data Elasticsearch last request info
  181. * @return string|null
  182. */
  183. protected function getCreatedDocIdV8(array $data)
  184. {
  185. if (!empty($data['items'][0]['index']['_id'])) {
  186. return $data['items'][0]['index']['_id'];
  187. }
  188. return null;
  189. }
  190. /**
  191. * Retrieve document by id from Elasticsearch
  192. *
  193. * @return array<mixed>
  194. */
  195. protected function getDocSourceFromElastic(Client|Client8 $client, string $index, string $type, string $documentId): array
  196. {
  197. $params = [
  198. 'index' => $index,
  199. 'id' => $documentId,
  200. ];
  201. if (!$client instanceof Client8 && $client::VERSION[0] !== '7') {
  202. $params['type'] = $type;
  203. }
  204. $data = $client->get($params);
  205. if (!empty($data['_source'])) {
  206. return $data['_source'];
  207. }
  208. return [];
  209. }
  210. /**
  211. * @return ClientBuilder|ClientBuilder8
  212. */
  213. private function getClientBuilder()
  214. {
  215. if (class_exists(ClientBuilder8::class)) {
  216. return ClientBuilder8::create();
  217. }
  218. return ClientBuilder::create();
  219. }
  220. }