Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the x-elastic-client-meta header #1089

Merged
merged 6 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 26 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,27 @@ protected function getCurlRetryException(array $request, array $response): Elast
return $exception;
}

/**
* Get the x-elastic-client-meta header
*/
private function getElasticMetaHeader(array $connectionParams): string
{
$clientMeta = sprintf(
"es=%s,php=%s,t=%s,a=%d",
Client::VERSION,
phpversion(),
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']);
}
}