2
0

ElasticsearchHandlerTest.php 7.7 KB

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