We use the Monolog\Handler\ElasticsearchHandler to write log entries to Elasticsearch (Bulk API and data stream) with Monolog.
It is not possible for us to use Monolog ElasticsearchHandler with the Elasticsearch PHP client in order to write entries into Elasticsearch by using Bulk API and data stream.
It doesn't matter whether we create the data stream automatically or create it manually beforehand.
When we want to add a log entry ($logger->info()), a parameter array is created in Monolog\Handler\ElasticsearchHandler->bulkSend(), which is not compatible with the Elasticsearch Bulk API and data stream.
In order for the function Elastic\Elasticsearch\Traits\ClientEndpointsTrait->bulk() to create a request that is accepted by Elasticsearch, it needs some changes in Monolog\Handler\ElasticsearchHandler->bulkSend(). We managed this by adjusting bulkSend() as follows:
I'm not sure if that's the correct place and if it has any side effects to other Monolog-Elasticsearch-setups. It also seems to be necessary to make certain adjustments to the Elasticsearch PHP client. More on this is below. Therefore I didn't create a pull-request.
The Elastic\Elasticsearch\Traits\ClientEndpointsTrait->bulk() creates the following POST request, which according to the documentation should be PUT, but it still works:
The Elasticsearch PHP client should also be adapted to create a PUT request for Bulk API with data stream.
I've created a ticket 1389 to address the issue on the Elasticsearch PHP client side.
It would be very desirable if both packages support the Elasticsearch Bulk API with data stream.
System details
Operating System: Ubuntu 20.04.6 LTS or debian:11-slim in docker
Summary of problem or feature request
We use the
Monolog\Handler\ElasticsearchHandler
to write log entries to Elasticsearch (Bulk API and data stream) with Monolog.It is not possible for us to use Monolog ElasticsearchHandler with the Elasticsearch PHP client in order to write entries into Elasticsearch by using Bulk API and data stream.
The versions used are:
Code snippet of problem
Our Monolog setup looks like this:
The Elasticsearch documentation says the following about Bulk API and data stream in it:
To automatically create a data stream, Elasticsearch expects the following request:
In the current configuration, the request from above is sent as
POST
, which leads to an Elasticsearch error.We can get around this by creating the data stream manually using an HttpClient that calls the API with the following request:
After that is is possible to add entries with the following request:
It doesn't matter whether we create the data stream automatically or create it manually beforehand. When we want to add a log entry (
$logger->info()
), a parameter array is created inMonolog\Handler\ElasticsearchHandler->bulkSend()
, which is not compatible with the Elasticsearch Bulk API and data stream. In order for the functionElastic\Elasticsearch\Traits\ClientEndpointsTrait->bulk()
to create a request that is accepted by Elasticsearch, it needs some changes inMonolog\Handler\ElasticsearchHandler->bulkSend()
. We managed this by adjustingbulkSend()
as follows:I'm not sure if that's the correct place and if it has any side effects to other Monolog-Elasticsearch-setups. It also seems to be necessary to make certain adjustments to the Elasticsearch PHP client. More on this is below. Therefore I didn't create a pull-request.
The
Elastic\Elasticsearch\Traits\ClientEndpointsTrait->bulk()
creates the followingPOST
request, which according to the documentation should bePUT
, but it still works:The Elasticsearch PHP client should also be adapted to create a
PUT
request for Bulk API with data stream. I've created a ticket 1389 to address the issue on the Elasticsearch PHP client side.It would be very desirable if both packages support the Elasticsearch Bulk API with data stream.
System details