Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ElasticSearch v8 support #1662

Merged
merged 6 commits into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ jobs:
- name: "Checkout"
uses: "actions/checkout@v2"

# required for elasticsearch
- name: Configure sysctl limits
run: |
sudo swapoff -a
sudo sysctl -w vm.swappiness=1
sudo sysctl -w fs.file-max=262144
sudo sysctl -w vm.max_map_count=262144

- name: Runs Elasticsearch
timeout-minutes: 1
continue-on-error: true
uses: elastic/elastic-github-actions/elasticsearch@master
with:
stack-version: 7.17.3

- name: Run CouchDB
timeout-minutes: 1
continue-on-error: true
uses: "cobot/couchdb-action@master"
with:
couchdb version: '3.2.2'

- name: Run MongoDB
uses: supercharge/mongodb-github-action@1.7.0
with:
mongodb-version: 5.0

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
Expand Down Expand Up @@ -60,6 +87,7 @@ jobs:

- name: "Install latest dependencies"
run: |
composer require --no-update --no-interaction --dev elasticsearch/elasticsearch:^7
composer update ${{ env.COMPOSER_FLAGS }}

- name: "Run tests"
Expand All @@ -72,3 +100,16 @@ jobs:
composer require --no-update psr/log:^3
composer update -W ${{ env.COMPOSER_FLAGS }}
composer exec phpunit -- --verbose

- name: Runs Elasticsearch
timeout-minutes: 1
uses: elastic/elastic-github-actions/elasticsearch@master
with:
stack-version: 8.2.0

- name: "Run ElasticSearch tests with elasticsearch 8"
run: |
composer remove --no-update --dev ruflin/elastica
composer require --no-update --no-interaction --dev elasticsearch/elasticsearch:^8
composer update ${{ env.COMPOSER_FLAGS }}
composer exec phpunit -- --filter Elasticsearch --verbose
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"require-dev": {
"aws/aws-sdk-php": "^2.4.9 || ^3.0",
"doctrine/couchdb": "~1.0@dev",
"elasticsearch/elasticsearch": "^7",
Seldaek marked this conversation as resolved.
Show resolved Hide resolved
"elasticsearch/elasticsearch": "^7 || ^8",
"graylog2/gelf-php": "^1.4.2",
"mongodb/mongodb": "^1.8",
"php-amqplib/php-amqplib": "~2.4 || ^3",
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ parameters:
- '#::popProcessor\(\) should return callable#'
- '#Parameter \#1 \$ of callable \(callable\(Monolog\\Handler\\Record\): Monolog\\Handler\\Record\)#'
- '#is incompatible with native type array.#'

# legacy elasticsearch namespace failures
- '# Elasticsearch\\#'
47 changes: 39 additions & 8 deletions src/Monolog/Handler/ElasticsearchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Monolog\Handler;

use Elastic\Elasticsearch\Response\Elasticsearch;
use Throwable;
use RuntimeException;
use Monolog\Logger;
Expand All @@ -19,6 +20,8 @@
use InvalidArgumentException;
use Elasticsearch\Common\Exceptions\RuntimeException as ElasticsearchRuntimeException;
use Elasticsearch\Client;
use Elastic\Elasticsearch\Exception\InvalidArgumentException as ElasticInvalidArgumentException;
use Elastic\Elasticsearch\Client as Client8;

/**
* Elasticsearch handler
Expand All @@ -44,7 +47,7 @@
class ElasticsearchHandler extends AbstractProcessingHandler
{
/**
* @var Client
* @var Client|Client8
*/
protected $client;

Expand All @@ -54,11 +57,20 @@ class ElasticsearchHandler extends AbstractProcessingHandler
protected $options = [];

/**
* @param Client $client Elasticsearch Client object
* @param mixed[] $options Handler configuration
* @var bool
*/
public function __construct(Client $client, array $options = [], $level = Logger::DEBUG, bool $bubble = true)
private $needsType;

/**
* @param Client|Client8 $client Elasticsearch Client object
* @param mixed[] $options Handler configuration
*/
public function __construct($client, array $options = [], $level = Logger::DEBUG, bool $bubble = true)
{
if (!$client instanceof Client && !$client instanceof Client8) {
throw new \TypeError('Elasticsearch\Client or Elastic\Elasticsearch\Client instance required');
}

parent::__construct($level, $bubble);
$this->client = $client;
$this->options = array_merge(
Expand All @@ -69,6 +81,14 @@ public function __construct(Client $client, array $options = [], $level = Logger
],
$options
);

if ($client instanceof Client8 || $client::VERSION[0] === '7') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is this comparison false? Clients before version 7 are not supported

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well only those versions are used in the tests but we don't enforce it when used within a project so I thought it safer to check and then used the same check in tests for consistency even tho there it's useless.

$this->needsType = false;
// force the type to _doc for ES8/ES7
$this->options['type'] = '_doc';
} else {
$this->needsType = true;
}
}

/**
Expand Down Expand Up @@ -133,16 +153,19 @@ protected function bulkSend(array $records): void

foreach ($records as $record) {
$params['body'][] = [
'index' => [
'index' => $this->needsType ? [
'_index' => $record['_index'],
'_type' => $record['_type'],
] : [
'_index' => $record['_index'],
],
];
unset($record['_index'], $record['_type']);

$params['body'][] = $record;
}

/** @var Elasticsearch */
$responses = $this->client->bulk($params);

if ($responses['errors'] === true) {
Expand All @@ -160,16 +183,20 @@ protected function bulkSend(array $records): void
*
* Only the first error is converted into an exception.
*
* @param mixed[] $responses returned by $this->client->bulk()
* @param mixed[]|Elasticsearch $responses returned by $this->client->bulk()
*/
protected function createExceptionFromResponses(array $responses): ElasticsearchRuntimeException
protected function createExceptionFromResponses($responses): Throwable
{
foreach ($responses['items'] ?? [] as $item) {
if (isset($item['index']['error'])) {
return $this->createExceptionFromError($item['index']['error']);
}
}

if (class_exists(ElasticInvalidArgumentException::class)) {
return new ElasticInvalidArgumentException('Elasticsearch failed to index one or more records.');
}

return new ElasticsearchRuntimeException('Elasticsearch failed to index one or more records.');
}

Expand All @@ -178,10 +205,14 @@ protected function createExceptionFromResponses(array $responses): Elasticsearch
*
* @param mixed[] $error
*/
protected function createExceptionFromError(array $error): ElasticsearchRuntimeException
protected function createExceptionFromError(array $error): Throwable
{
$previous = isset($error['caused_by']) ? $this->createExceptionFromError($error['caused_by']) : null;

if (class_exists(ElasticInvalidArgumentException::class)) {
return new ElasticInvalidArgumentException($error['type'] . ': ' . $error['reason'], 0, $previous);
}

return new ElasticsearchRuntimeException($error['type'] . ': ' . $error['reason'], 0, $previous);
}
}
2 changes: 1 addition & 1 deletion tests/Monolog/Handler/AmqpHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function testHandleAmqpExt()

public function testHandlePhpAmqpLib()
{
if (!class_exists('PhpAmqpLib\Connection\AMQPConnection')) {
if (!class_exists('PhpAmqpLib\Channel\AMQPChannel')) {
$this->markTestSkipped("php-amqplib not installed");
}

Expand Down
55 changes: 1 addition & 54 deletions tests/Monolog/Handler/ElasticaHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,60 +155,6 @@ public function providerTestConnectionErrors()
];
}

/**
* Integration test using localhost Elastic Search server version <7
*
* @covers Monolog\Handler\ElasticaHandler::__construct
* @covers Monolog\Handler\ElasticaHandler::handleBatch
* @covers Monolog\Handler\ElasticaHandler::bulkSend
* @covers Monolog\Handler\ElasticaHandler::getDefaultFormatter
*/
public function testHandleIntegration()
{
$msg = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['foo' => 7, 'bar', 'class' => new \stdClass],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'log',
];

$expected = $msg;
$expected['datetime'] = $msg['datetime']->format(\DateTime::ISO8601);
$expected['context'] = [
'class' => '[object] (stdClass: {})',
'foo' => 7,
0 => 'bar',
];

$client = new Client();
$handler = new ElasticaHandler($client, $this->options);

try {
$handler->handleBatch([$msg]);
} catch (\RuntimeException $e) {
$this->markTestSkipped("Cannot connect to Elastic Search server on localhost");
}

// check document id from ES server response
$documentId = $this->getCreatedDocId($client->getLastResponse());
$this->assertNotEmpty($documentId, 'No elastic document id received');

// retrieve document source from ES and validate
$document = $this->getDocSourceFromElastic(
$client,
$this->options['index'],
$this->options['type'],
$documentId
);
$this->assertEquals($expected, $document);

// remove test index from ES
$client->request("/{$this->options['index']}", Request::DELETE);
}

/**
* Integration test using localhost Elastic Search server version 7+
*
Expand Down Expand Up @@ -274,6 +220,7 @@ protected function getCreatedDocId(Response $response)
if (!empty($data['items'][0]['create']['_id'])) {
return $data['items'][0]['create']['_id'];
}
var_dump('Unexpected response: ', $data);
}

/**
Expand Down
Loading