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

[gax-php] feat: add REST headers #322

Merged
merged 8 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/AgentHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public static function buildAgentHeader($headerInfo)
// - gapicVersion (gapic/)
// - apiCoreVersion (gax/)
// - grpcVersion (grpc/)
// - restVersion (rest/)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to add this to the $headerInfo phpdoc for the method!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


$phpVersion = isset($headerInfo['phpVersion'])
? $headerInfo['phpVersion']
Expand All @@ -87,11 +88,23 @@ public static function buildAgentHeader($headerInfo)
: Version::getApiCoreVersion();
$metricsHeaders['gax'] = $apiCoreVersion;

// Context on library type identification (between gRPC+REST and REST-only):
// This uses the gRPC extension's version if 'grpcVersion' is not set, so we
// cannot use the presence of 'grpcVersion' to determine whether or not a library
// is gRPC+REST or REST-only. However, we cannot use the extension's presence
// either, since some clients may have the extension installed but opt to use a
// REST-only library (e.g. GCE).
// TODO: Should we stop sending empty gRPC headers?
miraleung marked this conversation as resolved.
Show resolved Hide resolved
$grpcVersion = isset($headerInfo['grpcVersion'])
? $headerInfo['grpcVersion']
: phpversion('grpc');
$metricsHeaders['grpc'] = $grpcVersion;

$restVersion = isset($headerInfo['restVersion'])
? $headerInfo['restVersion']
: $apiCoreVersion;
$metricsHeaders['rest'] = $restVersion;

$metricsList = [];
foreach ($metricsHeaders as $key => $value) {
$metricsList[] = $key . "/" . $value;
Expand Down
15 changes: 15 additions & 0 deletions src/GapicClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,21 @@ private function setClientOptions(array $options)
$clientConfig,
$options['disableRetries']
);

// Edge case: If the client has the gRPC extension installed, but is
// a REST-only library, then the grpcVersion header should not be set.
// Note: The case where defaultTransport() returns 'rest' is implemented
// in the microgenerator, not in gax-php.
if ($this->defaultTransport() === 'rest') {
// TODO: If/when we support gRPC-only libraries, do not unset the grpcVersion header.
if (isset($options['grpcVersion'])) {
unset($options['grpcVersion']);
}
if (!isset($options['restVersion'])) {
$options['restVersion'] = Version::getApiCoreVersion();
}
}

$this->agentHeader = AgentHeader::buildAgentHeader(
$this->pluckArray([
'libName',
Expand Down
48 changes: 44 additions & 4 deletions tests/Tests/Unit/AgentHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function testWithoutInput()
'gl-php/' . phpversion() .
' gapic/' .
' gax/' . Version::getApiCoreVersion() .
' grpc/' . phpversion('grpc')
' grpc/' . phpversion('grpc') .
' rest/' . Version::getApiCoreVersion()
]];

$header = AgentHeader::buildAgentHeader([]);
Expand All @@ -53,7 +54,7 @@ public function testWithoutInput()
public function testWithInput()
{
$expectedHeader = [AgentHeader::AGENT_HEADER_KEY => [
'gl-php/4.4.4 gccl/1.1.1 gapic/2.2.2 gax/3.3.3 grpc/5.5.5'
'gl-php/4.4.4 gccl/1.1.1 gapic/2.2.2 gax/3.3.3 grpc/5.5.5 rest/3.3.3'
]];

$header = AgentHeader::buildAgentHeader([
Expand All @@ -73,7 +74,8 @@ public function testWithoutVersionInput()
$expectedHeader = [AgentHeader::AGENT_HEADER_KEY => [
'gl-php/' . phpversion() .
' gccl/ gapic/ gax/' . Version::getApiCoreVersion() .
' grpc/' . phpversion('grpc')
' grpc/' . phpversion('grpc') .
' rest/' . Version::getApiCoreVersion()
]];

$header = AgentHeader::buildAgentHeader([
Expand All @@ -88,7 +90,8 @@ public function testWithNullVersionInput()
$expectedHeader = [AgentHeader::AGENT_HEADER_KEY => [
'gl-php/' . phpversion() .
' gccl/ gapic/ gax/' . Version::getApiCoreVersion() .
' grpc/' . phpversion('grpc')
' grpc/' . phpversion('grpc') .
' rest/' . Version::getApiCoreVersion()
]];

$header = AgentHeader::buildAgentHeader([
Expand All @@ -115,4 +118,41 @@ public function testGetGapicVersionWithNoAvailableVersion()
{
$this->assertEquals('', AgentHeader::readGapicVersionFromFile(__CLASS__));
}

public function testWithGrpcAndRest()
{
$expectedHeader = [AgentHeader::AGENT_HEADER_KEY => [
'gl-php/' . phpversion() .
' gapic/' .
' gax/3.3.3' .
' grpc/4.4.4' .
' rest/5.5.5'
]];

$header = AgentHeader::buildAgentHeader([
'apiCoreVersion' => '3.3.3',
'grpcVersion' => '4.4.4',
'restVersion' => '5.5.5',
]);

$this->assertSame($expectedHeader, $header);
}

public function testWithRestAndGaxFallback()
{
$expectedHeader = [AgentHeader::AGENT_HEADER_KEY => [
'gl-php/' . phpversion() .
' gapic/' .
' gax/3.3.3' .
' grpc/' . phpversion('grpc') .
' rest/3.3.3'
]];

$header = AgentHeader::buildAgentHeader([
'apiCoreVersion' => '3.3.3',
'restVersion' => null,
]);

$this->assertSame($expectedHeader, $header);
}
}
2 changes: 1 addition & 1 deletion tests/Tests/Unit/GapicClientTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testHeadersOverwriteBehavior()
'new-header' => ['this-should-be-used']
];
$expectedHeaders = [
'x-goog-api-client' => ['gl-php/5.5.0 gccl/0.0.0 gapic/0.9.0 gax/1.0.0 grpc/1.0.1'],
'x-goog-api-client' => ['gl-php/5.5.0 gccl/0.0.0 gapic/0.9.0 gax/1.0.0 grpc/1.0.1 rest/1.0.0'],
'new-header' => ['this-should-be-used'],
];
$transport = $this->getMock(TransportInterface::class);
Expand Down