Skip to content

Commit

Permalink
Add the x-elastic-client-meta header (#1089)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ezimuel authored Dec 16, 2020
1 parent 755ffd0 commit c8084ec
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 4 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
tools: prestissimo
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ util/output/

# PHPUnit
/phpunit.xml
.phpunit.result.cache

# Code coverage
build
Expand Down
16 changes: 16 additions & 0 deletions src/Elasticsearch/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ class ClientBuilder
*/
private $sslVerification = null;

/**
* @var bool
*/
private $elasticMetaHeader = true;

/**
* @var bool
*/
Expand Down Expand Up @@ -480,6 +485,16 @@ public function setSSLVerification($value = true): ClientBuilder
return $this;
}

/**
* Set or disable the x-elastic-client-meta header
*/
public function setElasticMetaHeader($value = true): ClientBuilder
{
$this->elasticMetaHeader = $value;

return $this;
}

/**
* Include the port in Host header
*
Expand Down Expand Up @@ -532,6 +547,7 @@ public function build(): Client
$this->serializer = new $this->serializer;
}

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

if (is_null($this->connectionFactory)) {
Expand Down
27 changes: 27 additions & 0 deletions src/Elasticsearch/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ public function __construct(
phpversion()
)];

// Add x-elastic-client-meta header, if enabled
if (isset($connectionParams['client']['x-elastic-client-meta']) && $connectionParams['client']['x-elastic-client-meta']) {
$this->headers['x-elastic-client-meta'] = [$this->getElasticMetaHeader($connectionParams)];
}

$host = $hostDetails['host'];
$path = null;
if (isset($hostDetails['path']) === true) {
Expand Down Expand Up @@ -574,6 +579,28 @@ protected function getCurlRetryException(array $request, array $response): Elast
return $exception;
}

/**
* Get the x-elastic-client-meta header
*/
private function getElasticMetaHeader(array $connectionParams): string
{
$phpSemVersion = sprintf("%d.%d.%d", PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION);
$clientMeta = sprintf(
"es=%s,php=%s,t=%s,a=%d",
Client::VERSION,
$phpSemVersion,
Client::VERSION,
isset($connectionParams['client']['future']) && $connectionParams['client']['future'] === 'lazy' ? 1 : 0
);
if (function_exists('curl_version')) {
$curlVersion = curl_version();
if (isset($curlVersion['version'])) {
$clientMeta .= sprintf(",c=%s", $curlVersion['version']);
}
}
return $clientMeta;
}

/**
* Get the OS version using php_uname if available
* otherwise it returns an empty string
Expand Down
61 changes: 58 additions & 3 deletions tests/Elasticsearch/Tests/ClientBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,6 @@ public function testFromConfigQuiteTrueWithUnknownKey()
);
}

/**
* @expectedException Elasticsearch\Common\Exceptions\RuntimeException
*/
public function testFromConfigQuiteFalseWithUnknownKey()
{
$this->expectException(RuntimeException::class);
Expand All @@ -241,4 +238,62 @@ public function testFromConfigQuiteFalseWithUnknownKey()
false
);
}

public function testElasticClientMetaHeaderIsSentByDefault()
{
$client = ClientBuilder::create()
->build();
$this->assertInstanceOf(Client::class, $client);

try {
$result = $client->info();
} catch (ElasticsearchException $e) {
$request = $client->transport->getLastConnection()->getLastRequestInfo();
$this->assertTrue(isset($request['request']['headers']['x-elastic-client-meta']));
$this->assertEquals(
1,
preg_match(
'/^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*$/',
$request['request']['headers']['x-elastic-client-meta'][0]
)
);
}
}

public function testElasticClientMetaHeaderIsSentWhenEnabled()
{
$client = ClientBuilder::create()
->setElasticMetaHeader(true)
->build();
$this->assertInstanceOf(Client::class, $client);

try {
$result = $client->info();
} catch (ElasticsearchException $e) {
$request = $client->transport->getLastConnection()->getLastRequestInfo();
$this->assertTrue(isset($request['request']['headers']['x-elastic-client-meta']));
$this->assertEquals(
1,
preg_match(
'/^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*$/',
$request['request']['headers']['x-elastic-client-meta'][0]
)
);
}
}

public function testElasticClientMetaHeaderIsNotSentWhenDisabled()
{
$client = ClientBuilder::create()
->setElasticMetaHeader(false)
->build();
$this->assertInstanceOf(Client::class, $client);

try {
$result = $client->info();
} catch (ElasticsearchException $e) {
$request = $client->transport->getLastConnection()->getLastRequestInfo();
$this->assertFalse(isset($request['request']['headers']['x-elastic-client-meta']));
}
}
}
63 changes: 63 additions & 0 deletions tests/Elasticsearch/Tests/Connections/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,67 @@ public function testHeaderClientParamIsResetAfterSent()
$headersAfter = $connection->getHeaders();
$this->assertEquals($headersBefore, $headersAfter);
}

/**
* Test if the x-elastic-client-meta header is sent if $params['client']['x-elastic-client-meta'] is true
*/
public function testElasticMetaClientHeaderIsSentWhenParameterIsTrue()
{
$params = [
'client' => [
'x-elastic-client-meta'=> true
]
];
$host = [
'host' => 'localhost'
];

$connection = new Connection(
ClientBuilder::defaultHandler(),
$host,
$params,
$this->serializer,
$this->logger,
$this->trace
);
$result = $connection->performRequest('GET', '/');
$request = $connection->getLastRequestInfo()['request'];

$this->assertArrayHasKey('x-elastic-client-meta', $request['headers']);
$this->assertEquals(
1,
preg_match(
'/^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*$/',
$request['headers']['x-elastic-client-meta'][0]
)
);
}

/**
* Test if the x-elastic-client-meta header is sent if $params['client']['x-elastic-client-meta'] is true
*/
public function testElasticMetaClientHeaderIsNotSentWhenParameterIsFalse()
{
$params = [
'client' => [
'x-elastic-client-meta'=> false
]
];
$host = [
'host' => 'localhost'
];

$connection = new Connection(
ClientBuilder::defaultHandler(),
$host,
$params,
$this->serializer,
$this->logger,
$this->trace
);
$result = $connection->performRequest('GET', '/');
$request = $connection->getLastRequestInfo()['request'];

$this->assertArrayNotHasKey('x-elastic-client-meta', $request['headers']);
}
}

0 comments on commit c8084ec

Please sign in to comment.