Skip to content

Commit b316710

Browse files
committed
Backport #1075, #1065 and #952 + type fixes for PHP 8
1 parent b875a90 commit b316710

File tree

7 files changed

+115
-17
lines changed

7 files changed

+115
-17
lines changed

docs/per-request-configuration.asciidoc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,28 @@ Array
214214
)
215215
----
216216

217+
=== Request Identification
218+
219+
You can enrich your requests against Elasticsearch with an identifier string, that allows you to discover this identifier
220+
in https://www.elastic.co/guide/en/elasticsearch/reference/6.8/logging.html#deprecation-logging[deprecation logs], to support you with
221+
https://www.elastic.co/guide/en/elasticsearch/reference/6.8/index-modules-slowlog.html[identifying search slow log origin]
222+
or to help with https://www.elastic.co/guide/en/elasticsearch/reference/6.8/tasks.html#_identifying_running_tasks[identifying running tasks].
223+
224+
225+
[source,php]
226+
----
227+
$client = ClientBuilder::create()->build();
228+
$params = [
229+
'index' => 'test',
230+
'id' => 1,
231+
'client' => [
232+
'opaqueId' => '[email protected]_user1234', <1>
233+
]
234+
];
235+
$response = $client->get($params);
236+
----
237+
<1> This will populate the `X-Opaque-Id` header with the value `[email protected]_user1234`
238+
217239
=== Curl Timeouts
218240

219241
It is possible to configure per-request curl timeouts via the `timeout` and `connect_timeout` parameters. These

src/Elasticsearch/ClientBuilder.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
use GuzzleHttp\Ring\Client\Middleware;
3636
use Psr\Log\LoggerInterface;
3737
use Psr\Log\NullLogger;
38+
use ReflectionClass;
39+
40+
use function filter_var;
3841

3942
/**
4043
* Class ClientBuilder
@@ -512,7 +515,11 @@ public function build()
512515

513516
$this->endpoint = function ($class) use ($serializer) {
514517
$fullPath = '\\Elasticsearch\\Endpoints\\' . $class;
515-
if ($class === 'Bulk' || $class === 'Msearch' || $class === 'MsearchTemplate' || $class === 'MPercolate') {
518+
519+
$reflection = new ReflectionClass($fullPath);
520+
$constructor = $reflection->getConstructor();
521+
522+
if ($constructor && $constructor->getParameters()) {
516523
return new $fullPath($serializer);
517524
} else {
518525
return new $fullPath();
@@ -576,7 +583,7 @@ private function buildTransport()
576583
}
577584

578585
if (is_null($this->transport)) {
579-
$this->transport = new Transport($this->retries, $this->sniffOnStart, $this->connectionPool, $this->logger);
586+
$this->transport = new Transport($this->retries, $this->connectionPool, $this->logger, $this->sniffOnStart);
580587
}
581588
}
582589

src/Elasticsearch/Connections/Connection.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,17 @@ public function performRequest($method, $uri, $params = null, $body = null, $opt
175175
$body = $this->serializer->serialize($body);
176176
}
177177

178+
$headers = $this->headers;
179+
if (isset($options['client']['headers']) && is_array($options['client']['headers'])) {
180+
$headers = array_merge($this->headers, $options['client']['headers']);
181+
}
182+
178183
$request = [
179184
'http_method' => $method,
180185
'scheme' => $this->transportSchema,
181186
'uri' => $this->getURI($uri, $params),
182187
'body' => $body,
183-
'headers' => array_merge([
184-
'Host' => [$this->host]
185-
], $this->headers)
188+
'headers' => array_merge(['Host' => [$this->host]], $headers)
186189
];
187190

188191
$request = array_replace_recursive($request, $this->connectionParams, $options);
@@ -333,16 +336,20 @@ private function wrapHandler(callable $handler)
333336
*
334337
* @return string
335338
*/
336-
private function getURI(string $uri, ?array $params)
339+
private function getURI(string $uri, ?array $params): string
337340
{
338341
if (isset($params) === true && !empty($params)) {
339-
array_walk($params, function (&$value, &$key) {
340-
if ($value === true) {
341-
$value = 'true';
342-
} elseif ($value === false) {
343-
$value = 'false';
344-
}
345-
});
342+
$params = array_map(
343+
function ($value) {
344+
if ($value === true) {
345+
return 'true';
346+
} elseif ($value === false) {
347+
return 'false';
348+
}
349+
return $value;
350+
},
351+
$params
352+
);
346353

347354
$uri .= '?' . http_build_query($params);
348355
}
@@ -351,7 +358,7 @@ private function getURI(string $uri, ?array $params)
351358
$uri = $this->path . $uri;
352359
}
353360

354-
return $uri;
361+
return $uri ?? '';
355362
}
356363

357364
/**

src/Elasticsearch/Connections/ConnectionInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,6 @@ public function getLastRequestInfo();
112112
*/
113113
// @codingStandardsIgnoreStart
114114
// "Arguments with default values must be at the end of the argument list" - cannot change the interface
115-
public function performRequest($method, $uri, $params = null, $body = null, $options = [], Transport $transport);
115+
public function performRequest(string $method, string $uri, ?array $params = [], $body = null, array $options = [], Transport $transport = null);
116116
// @codingStandardsIgnoreEnd
117117
}

src/Elasticsearch/Transport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Transport
6161
*/
6262
// @codingStandardsIgnoreStart
6363
// "Arguments with default values must be at the end of the argument list" - cannot change the interface
64-
public function __construct($retries, $sniffOnStart = false, AbstractConnectionPool $connectionPool, LoggerInterface $log)
64+
public function __construct(int $retries, AbstractConnectionPool $connectionPool, LoggerInterface $log, bool $sniffOnStart = false)
6565
{
6666
// @codingStandardsIgnoreEnd
6767

tests/Elasticsearch/Tests/Connections/ConnectionTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Elasticsearch\ClientBuilder;
2020
use Elasticsearch\Connections\Connection;
2121
use Elasticsearch\Serializers\SerializerInterface;
22+
use Elasticsearch\Serializers\SmartSerializer;
2223
use Psr\Log\LoggerInterface;
2324

2425
class ConnectionTest extends \PHPUnit\Framework\TestCase
@@ -91,4 +92,65 @@ public function testUserAgentHeaderIsSent()
9192
$this->assertArrayHasKey('User-Agent', $request['headers']);
9293
$this->assertStringContainsString('elasticsearch-php/'. Client::VERSION, $request['headers']['User-Agent'][0]);
9394
}
95+
96+
/**
97+
* @depends testGetHeadersContainUserAgent
98+
*
99+
* @covers \Connection::performRequest
100+
* @covers \Connection::getURI
101+
*/
102+
public function testParametersAreSent()
103+
{
104+
$connectionParams = [];
105+
$host = [
106+
'host' => 'localhost'
107+
];
108+
$requestParams = [
109+
'foo' => true,
110+
'baz' => false,
111+
'bar' => 'baz'
112+
];
113+
114+
$connection = new Connection(
115+
ClientBuilder::defaultHandler(),
116+
$host,
117+
$connectionParams,
118+
$this->serializer,
119+
$this->logger,
120+
$this->trace
121+
);
122+
$result = $connection->performRequest('GET', '/', $requestParams);
123+
$request = $connection->getLastRequestInfo()['request'];
124+
125+
$this->assertEquals('/?foo=true&baz=false&bar=baz', $request['uri']);
126+
}
127+
128+
public function testHeaderClientParamIsResetAfterSent()
129+
{
130+
$host = [
131+
'host' => 'localhost'
132+
];
133+
134+
$connection = new Connection(
135+
ClientBuilder::defaultHandler(),
136+
$host,
137+
[],
138+
new SmartSerializer(),
139+
$this->logger,
140+
$this->trace
141+
);
142+
143+
$options = [
144+
'client' => [
145+
'headers' => [
146+
'Foo' => [ 'Bar' ]
147+
]
148+
]
149+
];
150+
151+
$headersBefore = $connection->getHeaders();
152+
$result = $connection->performRequest('GET', '/', null, null, $options);
153+
$headersAfter = $connection->getHeaders();
154+
$this->assertEquals($headersBefore, $headersAfter);
155+
}
94156
}

util/template/test/warnings

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
$this->assertTrue(isset($headers['Warning']));
44
$expectedHeaders = :expected;
55
foreach ($expectedHeaders as $expected) {
6-
$this->assertStringContainsString($expected, $headers['Warning'][0]);
6+
$this->assertStringContainsString($expected, implode('', $headers['Warning']));
77
}

0 commit comments

Comments
 (0)