Skip to content

Commit 7c0e670

Browse files
committed
Backported #1089
1 parent c4c3bd3 commit 7c0e670

File tree

4 files changed

+169
-4
lines changed

4 files changed

+169
-4
lines changed

src/Elasticsearch/ClientBuilder.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@
2020

2121
use Elasticsearch\Common\Exceptions\InvalidArgumentException;
2222
use Elasticsearch\Common\Exceptions\RuntimeException;
23-
use Elasticsearch\ConnectionPool\AbstractConnectionPool;
24-
use Elasticsearch\ConnectionPool\Selectors\SelectorInterface;
2523
use Elasticsearch\ConnectionPool\StaticNoPingConnectionPool;
26-
use Elasticsearch\Connections\Connection;
2724
use Elasticsearch\Connections\ConnectionFactory;
2825
use Elasticsearch\Connections\ConnectionFactoryInterface;
2926
use Elasticsearch\Namespaces\NamespaceBuilderInterface;
30-
use Elasticsearch\Serializers\SerializerInterface;
3127
use Elasticsearch\ConnectionPool\Selectors;
3228
use Elasticsearch\Serializers\SmartSerializer;
3329
use GuzzleHttp\Ring\Client\CurlHandler;
@@ -100,6 +96,11 @@ class ClientBuilder
10096
/** @var null|bool|string */
10197
private $sslVerification = null;
10298

99+
/**
100+
* @var bool
101+
*/
102+
private $elasticMetaHeader = true;
103+
103104
/**
104105
* @return ClientBuilder
105106
*/
@@ -431,6 +432,16 @@ public function setSSLVerification($value = true)
431432
return $this;
432433
}
433434

435+
/**
436+
* Set or disable the x-elastic-client-meta header
437+
*/
438+
public function setElasticMetaHeader($value = true): ClientBuilder
439+
{
440+
$this->elasticMetaHeader = $value;
441+
442+
return $this;
443+
}
444+
434445
/**
435446
* @return Client
436447
*/
@@ -474,6 +485,8 @@ public function build()
474485
$this->serializer = new $this->serializer;
475486
}
476487

488+
$this->connectionParams['client']['x-elastic-client-meta'] = $this->elasticMetaHeader;
489+
477490
if (is_null($this->connectionFactory)) {
478491
if (is_null($this->connectionParams)) {
479492
$this->connectionParams = [];

src/Elasticsearch/Connections/Connection.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ public function __construct(
145145
phpversion()
146146
)];
147147

148+
// Add x-elastic-client-meta header, if enabled
149+
if (isset($connectionParams['client']['x-elastic-client-meta']) && $connectionParams['client']['x-elastic-client-meta']) {
150+
$this->headers['x-elastic-client-meta'] = [$this->getElasticMetaHeader($connectionParams)];
151+
}
152+
148153
$host = $hostDetails['host'].':'.$hostDetails['port'];
149154
$path = null;
150155
if (isset($hostDetails['path']) === true) {
@@ -762,4 +767,28 @@ private function tryDeserializeError($response, $errorClass)
762767
// <2.0 "i just blew up" nonstructured exception
763768
return new $errorClass($responseBody);
764769
}
770+
771+
/**
772+
* Get the x-elastic-client-meta header
773+
*/
774+
private function getElasticMetaHeader(array $connectionParams): string
775+
{
776+
$phpSemVersion = sprintf("%d.%d.%d", PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION);
777+
// Reduce the size in case of '-snapshot' version (using 'p' as pre-release)
778+
$clientVersion = str_replace('-snapshot', '-p', strtolower(Client::VERSION));
779+
$clientMeta = sprintf(
780+
"es=%s,php=%s,t=%s,a=%d",
781+
$clientVersion,
782+
$phpSemVersion,
783+
$clientVersion,
784+
isset($connectionParams['client']['future']) && $connectionParams['client']['future'] === 'lazy' ? 1 : 0
785+
);
786+
if (function_exists('curl_version')) {
787+
$curlVersion = curl_version();
788+
if (isset($curlVersion['version'])) {
789+
$clientMeta .= sprintf(",cu=%s", $curlVersion['version']); // cu = curl library
790+
}
791+
}
792+
return $clientMeta;
793+
}
765794
}

tests/Elasticsearch/Tests/ClientBuilderTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
namespace Elasticsearch\Tests;
2020

21+
use Elasticsearch\Client;
2122
use Elasticsearch\ClientBuilder;
23+
use Elasticsearch\Common\Exceptions\ElasticsearchException;
2224
use Elasticsearch\Common\Exceptions\InvalidArgumentException;
2325
use PHPUnit\Framework\TestCase;
2426

@@ -40,4 +42,62 @@ public function testClientBuilderThrowsExceptionForIncorrectTracerClass()
4042

4143
ClientBuilder::create()->setTracer(new \Elasticsearch\Tests\ClientBuilder\DummyLogger());
4244
}
45+
46+
public function testElasticClientMetaHeaderIsSentByDefault()
47+
{
48+
$client = ClientBuilder::create()
49+
->build();
50+
$this->assertInstanceOf(Client::class, $client);
51+
52+
try {
53+
$result = $client->info();
54+
} catch (ElasticsearchException $e) {
55+
$request = $client->transport->getLastConnection()->getLastRequestInfo();
56+
$this->assertTrue(isset($request['request']['headers']['x-elastic-client-meta']));
57+
$this->assertEquals(
58+
1,
59+
preg_match(
60+
'/^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*$/',
61+
$request['request']['headers']['x-elastic-client-meta'][0]
62+
)
63+
);
64+
}
65+
}
66+
67+
public function testElasticClientMetaHeaderIsSentWhenEnabled()
68+
{
69+
$client = ClientBuilder::create()
70+
->setElasticMetaHeader(true)
71+
->build();
72+
$this->assertInstanceOf(Client::class, $client);
73+
74+
try {
75+
$result = $client->info();
76+
} catch (ElasticsearchException $e) {
77+
$request = $client->transport->getLastConnection()->getLastRequestInfo();
78+
$this->assertTrue(isset($request['request']['headers']['x-elastic-client-meta']));
79+
$this->assertEquals(
80+
1,
81+
preg_match(
82+
'/^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*$/',
83+
$request['request']['headers']['x-elastic-client-meta'][0]
84+
)
85+
);
86+
}
87+
}
88+
89+
public function testElasticClientMetaHeaderIsNotSentWhenDisabled()
90+
{
91+
$client = ClientBuilder::create()
92+
->setElasticMetaHeader(false)
93+
->build();
94+
$this->assertInstanceOf(Client::class, $client);
95+
96+
try {
97+
$result = $client->info();
98+
} catch (ElasticsearchException $e) {
99+
$request = $client->transport->getLastConnection()->getLastRequestInfo();
100+
$this->assertFalse(isset($request['request']['headers']['x-elastic-client-meta']));
101+
}
102+
}
43103
}

tests/Elasticsearch/Tests/Connections/ConnectionTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,67 @@ public function testHeaderClientParamIsResetAfterSent()
153153
$headersAfter = $connection->getHeaders();
154154
$this->assertEquals($headersBefore, $headersAfter);
155155
}
156+
157+
/**
158+
* Test if the x-elastic-client-meta header is sent if $params['client']['x-elastic-client-meta'] is true
159+
*/
160+
public function testElasticMetaClientHeaderIsSentWhenParameterIsTrue()
161+
{
162+
$params = [
163+
'client' => [
164+
'x-elastic-client-meta'=> true
165+
]
166+
];
167+
$host = [
168+
'host' => 'localhost'
169+
];
170+
171+
$connection = new Connection(
172+
ClientBuilder::defaultHandler(),
173+
$host,
174+
$params,
175+
$this->serializer,
176+
$this->logger,
177+
$this->trace
178+
);
179+
$result = $connection->performRequest('GET', '/');
180+
$request = $connection->getLastRequestInfo()['request'];
181+
182+
$this->assertArrayHasKey('x-elastic-client-meta', $request['headers']);
183+
$this->assertEquals(
184+
1,
185+
preg_match(
186+
'/^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*$/',
187+
$request['headers']['x-elastic-client-meta'][0]
188+
)
189+
);
190+
}
191+
192+
/**
193+
* Test if the x-elastic-client-meta header is sent if $params['client']['x-elastic-client-meta'] is true
194+
*/
195+
public function testElasticMetaClientHeaderIsNotSentWhenParameterIsFalse()
196+
{
197+
$params = [
198+
'client' => [
199+
'x-elastic-client-meta'=> false
200+
]
201+
];
202+
$host = [
203+
'host' => 'localhost'
204+
];
205+
206+
$connection = new Connection(
207+
ClientBuilder::defaultHandler(),
208+
$host,
209+
$params,
210+
$this->serializer,
211+
$this->logger,
212+
$this->trace
213+
);
214+
$result = $connection->performRequest('GET', '/');
215+
$request = $connection->getLastRequestInfo()['request'];
216+
217+
$this->assertArrayNotHasKey('x-elastic-client-meta', $request['headers']);
218+
}
156219
}

0 commit comments

Comments
 (0)