Skip to content

Commit c8084ec

Browse files
authored
Add the x-elastic-client-meta header (#1089)
* Added the x-elastic-client-meta header * Removed @ExpectedException usage in PHPUnit * Removed prestissimo plugin for composer in github action * Added .phpunit.result.cache in .gitignore * Add the t transport parameter in telemetry client header * Fixed semver format for PHP version in client telemetry header
1 parent 755ffd0 commit c8084ec

File tree

6 files changed

+165
-4
lines changed

6 files changed

+165
-4
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ jobs:
2121
uses: shivammathur/setup-php@v2
2222
with:
2323
php-version: ${{ matrix.php-version }}
24-
tools: prestissimo
2524
env:
2625
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2726

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ util/output/
3131

3232
# PHPUnit
3333
/phpunit.xml
34+
.phpunit.result.cache
3435

3536
# Code coverage
3637
build

src/Elasticsearch/ClientBuilder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ class ClientBuilder
134134
*/
135135
private $sslVerification = null;
136136

137+
/**
138+
* @var bool
139+
*/
140+
private $elasticMetaHeader = true;
141+
137142
/**
138143
* @var bool
139144
*/
@@ -480,6 +485,16 @@ public function setSSLVerification($value = true): ClientBuilder
480485
return $this;
481486
}
482487

488+
/**
489+
* Set or disable the x-elastic-client-meta header
490+
*/
491+
public function setElasticMetaHeader($value = true): ClientBuilder
492+
{
493+
$this->elasticMetaHeader = $value;
494+
495+
return $this;
496+
}
497+
483498
/**
484499
* Include the port in Host header
485500
*
@@ -532,6 +547,7 @@ public function build(): Client
532547
$this->serializer = new $this->serializer;
533548
}
534549

550+
$this->connectionParams['client']['x-elastic-client-meta']= $this->elasticMetaHeader;
535551
$this->connectionParams['client']['port_in_header'] = $this->includePortInHostHeader;
536552

537553
if (is_null($this->connectionFactory)) {

src/Elasticsearch/Connections/Connection.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ public function __construct(
169169
phpversion()
170170
)];
171171

172+
// Add x-elastic-client-meta header, if enabled
173+
if (isset($connectionParams['client']['x-elastic-client-meta']) && $connectionParams['client']['x-elastic-client-meta']) {
174+
$this->headers['x-elastic-client-meta'] = [$this->getElasticMetaHeader($connectionParams)];
175+
}
176+
172177
$host = $hostDetails['host'];
173178
$path = null;
174179
if (isset($hostDetails['path']) === true) {
@@ -574,6 +579,28 @@ protected function getCurlRetryException(array $request, array $response): Elast
574579
return $exception;
575580
}
576581

582+
/**
583+
* Get the x-elastic-client-meta header
584+
*/
585+
private function getElasticMetaHeader(array $connectionParams): string
586+
{
587+
$phpSemVersion = sprintf("%d.%d.%d", PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION);
588+
$clientMeta = sprintf(
589+
"es=%s,php=%s,t=%s,a=%d",
590+
Client::VERSION,
591+
$phpSemVersion,
592+
Client::VERSION,
593+
isset($connectionParams['client']['future']) && $connectionParams['client']['future'] === 'lazy' ? 1 : 0
594+
);
595+
if (function_exists('curl_version')) {
596+
$curlVersion = curl_version();
597+
if (isset($curlVersion['version'])) {
598+
$clientMeta .= sprintf(",c=%s", $curlVersion['version']);
599+
}
600+
}
601+
return $clientMeta;
602+
}
603+
577604
/**
578605
* Get the OS version using php_uname if available
579606
* otherwise it returns an empty string

tests/Elasticsearch/Tests/ClientBuilderTest.php

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,6 @@ public function testFromConfigQuiteTrueWithUnknownKey()
227227
);
228228
}
229229

230-
/**
231-
* @expectedException Elasticsearch\Common\Exceptions\RuntimeException
232-
*/
233230
public function testFromConfigQuiteFalseWithUnknownKey()
234231
{
235232
$this->expectException(RuntimeException::class);
@@ -241,4 +238,62 @@ public function testFromConfigQuiteFalseWithUnknownKey()
241238
false
242239
);
243240
}
241+
242+
public function testElasticClientMetaHeaderIsSentByDefault()
243+
{
244+
$client = ClientBuilder::create()
245+
->build();
246+
$this->assertInstanceOf(Client::class, $client);
247+
248+
try {
249+
$result = $client->info();
250+
} catch (ElasticsearchException $e) {
251+
$request = $client->transport->getLastConnection()->getLastRequestInfo();
252+
$this->assertTrue(isset($request['request']['headers']['x-elastic-client-meta']));
253+
$this->assertEquals(
254+
1,
255+
preg_match(
256+
'/^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*$/',
257+
$request['request']['headers']['x-elastic-client-meta'][0]
258+
)
259+
);
260+
}
261+
}
262+
263+
public function testElasticClientMetaHeaderIsSentWhenEnabled()
264+
{
265+
$client = ClientBuilder::create()
266+
->setElasticMetaHeader(true)
267+
->build();
268+
$this->assertInstanceOf(Client::class, $client);
269+
270+
try {
271+
$result = $client->info();
272+
} catch (ElasticsearchException $e) {
273+
$request = $client->transport->getLastConnection()->getLastRequestInfo();
274+
$this->assertTrue(isset($request['request']['headers']['x-elastic-client-meta']));
275+
$this->assertEquals(
276+
1,
277+
preg_match(
278+
'/^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*$/',
279+
$request['request']['headers']['x-elastic-client-meta'][0]
280+
)
281+
);
282+
}
283+
}
284+
285+
public function testElasticClientMetaHeaderIsNotSentWhenDisabled()
286+
{
287+
$client = ClientBuilder::create()
288+
->setElasticMetaHeader(false)
289+
->build();
290+
$this->assertInstanceOf(Client::class, $client);
291+
292+
try {
293+
$result = $client->info();
294+
} catch (ElasticsearchException $e) {
295+
$request = $client->transport->getLastConnection()->getLastRequestInfo();
296+
$this->assertFalse(isset($request['request']['headers']['x-elastic-client-meta']));
297+
}
298+
}
244299
}

tests/Elasticsearch/Tests/Connections/ConnectionTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,67 @@ public function testHeaderClientParamIsResetAfterSent()
363363
$headersAfter = $connection->getHeaders();
364364
$this->assertEquals($headersBefore, $headersAfter);
365365
}
366+
367+
/**
368+
* Test if the x-elastic-client-meta header is sent if $params['client']['x-elastic-client-meta'] is true
369+
*/
370+
public function testElasticMetaClientHeaderIsSentWhenParameterIsTrue()
371+
{
372+
$params = [
373+
'client' => [
374+
'x-elastic-client-meta'=> true
375+
]
376+
];
377+
$host = [
378+
'host' => 'localhost'
379+
];
380+
381+
$connection = new Connection(
382+
ClientBuilder::defaultHandler(),
383+
$host,
384+
$params,
385+
$this->serializer,
386+
$this->logger,
387+
$this->trace
388+
);
389+
$result = $connection->performRequest('GET', '/');
390+
$request = $connection->getLastRequestInfo()['request'];
391+
392+
$this->assertArrayHasKey('x-elastic-client-meta', $request['headers']);
393+
$this->assertEquals(
394+
1,
395+
preg_match(
396+
'/^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*$/',
397+
$request['headers']['x-elastic-client-meta'][0]
398+
)
399+
);
400+
}
401+
402+
/**
403+
* Test if the x-elastic-client-meta header is sent if $params['client']['x-elastic-client-meta'] is true
404+
*/
405+
public function testElasticMetaClientHeaderIsNotSentWhenParameterIsFalse()
406+
{
407+
$params = [
408+
'client' => [
409+
'x-elastic-client-meta'=> false
410+
]
411+
];
412+
$host = [
413+
'host' => 'localhost'
414+
];
415+
416+
$connection = new Connection(
417+
ClientBuilder::defaultHandler(),
418+
$host,
419+
$params,
420+
$this->serializer,
421+
$this->logger,
422+
$this->trace
423+
);
424+
$result = $connection->performRequest('GET', '/');
425+
$request = $connection->getLastRequestInfo()['request'];
426+
427+
$this->assertArrayNotHasKey('x-elastic-client-meta', $request['headers']);
428+
}
366429
}

0 commit comments

Comments
 (0)