Skip to content

Commit b0bbf6d

Browse files
Update agent header (#60)
* Update agent header, add tests * Add grpc version * Rename clientName/Version to libName/Version
1 parent 69acebc commit b0bbf6d

File tree

3 files changed

+165
-41
lines changed

3 files changed

+165
-41
lines changed

src/AgentHeaderDescriptor.php

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232

3333
namespace Google\GAX;
3434

35-
use InvalidArgumentException;
36-
3735
/**
3836
* Encapsulates the custom GAPIC header information.
3937
*/
@@ -43,44 +41,74 @@ class AgentHeaderDescriptor
4341
// TODO(michaelbausor): include bumping this version in a streamlined
4442
// release process. Issue: https://github.com/googleapis/gax-php/issues/48
4543
const GAX_VERSION = '0.7.0';
44+
const UNKNOWN_VERSION = '';
4645

47-
private $clientName;
48-
private $clientVersion;
49-
private $codeGenName;
50-
private $codeGenVersion;
51-
private $gaxVersion;
52-
private $phpVersion;
46+
private $metricsHeaders;
5347

5448
/**
5549
* @param array $headerInfo {
56-
* Required.
50+
* Optional.
5751
*
58-
* @type string $clientName the name of the client application.
59-
* @type string $clientVersion the version of the client application.
60-
* @type string $codeGenName the code generator name of the client library.
61-
* @type string $codeGenVersion the code generator version of the client library.
62-
* @type string $gaxVersion the GAX version.
6352
* @type string $phpVersion the PHP version.
53+
* @type string $libName the name of the client application.
54+
* @type string $libVersion the version of the client application.
55+
* @type string $gapicVersion the code generator version of the GAPIC library.
56+
* @type string $gaxVersion the GAX version.
57+
* @type string $grpcVersion the gRPC version.
6458
* }
6559
*/
6660
public function __construct($headerInfo)
6761
{
68-
$this->clientName = $headerInfo['clientName'];
69-
$this->clientVersion = $headerInfo['clientVersion'];
70-
$this->codeGenName = $headerInfo['codeGenName'];
71-
$this->codeGenVersion = $headerInfo['codeGenVersion'];
72-
$this->gaxVersion = $headerInfo['gaxVersion'];
73-
$this->phpVersion = $headerInfo['phpVersion'];
62+
$metricsHeaders = [];
63+
64+
// The ordering of the headers is important. We use the fact that $metricsHeaders is an
65+
// ordered dict. The desired ordering is:
66+
// - phpVersion (gl-php/)
67+
// - clientName (e.g. gccl/)
68+
// - gapicVersion (gapic/)
69+
// - gaxVersion (gax/)
70+
// - grpcVersion (grpc/)
71+
72+
$phpVersion = isset($headerInfo['phpVersion'])
73+
? $headerInfo['phpVersion']
74+
: phpversion();
75+
$metricsHeaders['gl-php'] = $phpVersion;
76+
77+
if (isset($headerInfo['libName'])) {
78+
$clientVersion = isset($headerInfo['libVersion'])
79+
? $headerInfo['libVersion']
80+
: AgentHeaderDescriptor::UNKNOWN_VERSION;
81+
$metricsHeaders[$headerInfo['libName']] = $clientVersion;
82+
}
83+
84+
$codeGenVersion = isset($headerInfo['gapicVersion'])
85+
? $headerInfo['gapicVersion']
86+
: AgentHeaderDescriptor::UNKNOWN_VERSION;
87+
$metricsHeaders['gapic'] = $codeGenVersion;
88+
89+
$gaxVersion = isset($headerInfo['gaxVersion'])
90+
? $headerInfo['gaxVersion']
91+
: AgentHeaderDescriptor::GAX_VERSION;
92+
$metricsHeaders['gax'] = $gaxVersion;
93+
94+
$grpcVersion = isset($headerInfo['grpcVersion'])
95+
? $headerInfo['grpcVersion']
96+
: phpversion('grpc');
97+
$metricsHeaders['grpc'] = $grpcVersion;
98+
99+
$this->metricsHeaders = $metricsHeaders;
74100
}
75101

76102
/**
77103
* Returns an associative array that contains GAPIC header metadata.
78104
*/
79105
public function getHeader()
80106
{
81-
return [self::AGENT_HEADER_KEY => ["$this->clientName/$this->clientVersion ".
82-
"$this->codeGenName/$this->codeGenVersion gax/$this->gaxVersion ".
83-
"php/$this->phpVersion"]];
107+
$metricsList = [];
108+
foreach ($this->metricsHeaders as $key => $value) {
109+
$metricsList[] = $key . "/" . $value;
110+
}
111+
return [self::AGENT_HEADER_KEY => [implode(" ", $metricsList)]];
84112
}
85113

86114
/**
@@ -90,17 +118,4 @@ public static function getGaxVersion()
90118
{
91119
return self::GAX_VERSION;
92120
}
93-
94-
private static function validate($descriptor)
95-
{
96-
$requiredFields = ['clientName', 'clientVersion', 'codeGenName',
97-
'codeGenVersion', 'gaxVersion', 'phpVersion'];
98-
foreach ($requiredFields as $field) {
99-
if (empty($descriptor[$field])) {
100-
throw new InvalidArgumentException(
101-
"$field is required for AgentHeaderDescriptor"
102-
);
103-
}
104-
}
105-
}
106121
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/*
3+
* Copyright 2016, Google Inc.
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are
8+
* met:
9+
*
10+
* * Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* * Redistributions in binary form must reproduce the above
13+
* copyright notice, this list of conditions and the following disclaimer
14+
* in the documentation and/or other materials provided with the
15+
* distribution.
16+
* * Neither the name of Google Inc. nor the names of its
17+
* contributors may be used to endorse or promote products derived from
18+
* this software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
namespace Google\GAX\UnitTests;
33+
34+
use Google\GAX\AgentHeaderDescriptor;
35+
use PHPUnit_Framework_TestCase;
36+
37+
class AgentHeaderDescriptorTest extends PHPUnit_Framework_TestCase
38+
{
39+
public function testWithoutInput()
40+
{
41+
$expectedHeader = [AgentHeaderDescriptor::AGENT_HEADER_KEY => [
42+
'gl-php/' . phpversion() .
43+
' gapic/' .
44+
' gax/' . AgentHeaderDescriptor::GAX_VERSION .
45+
' grpc/' . phpversion('grpc')
46+
]];
47+
48+
$agentHeaderDescriptor = new AgentHeaderDescriptor([]);
49+
$header = $agentHeaderDescriptor->getHeader();
50+
51+
$this->assertSame($expectedHeader, $header);
52+
}
53+
54+
public function testWithInput()
55+
{
56+
$expectedHeader = [AgentHeaderDescriptor::AGENT_HEADER_KEY => [
57+
'gl-php/4.4.4 gccl/1.1.1 gapic/2.2.2 gax/3.3.3 grpc/5.5.5'
58+
]];
59+
60+
$agentHeaderDescriptor = new AgentHeaderDescriptor([
61+
'libName' => 'gccl',
62+
'libVersion' => '1.1.1',
63+
'gapicVersion' => '2.2.2',
64+
'gaxVersion' => '3.3.3',
65+
'phpVersion' => '4.4.4',
66+
'grpcVersion' => '5.5.5',
67+
]);
68+
$header = $agentHeaderDescriptor->getHeader();
69+
70+
$this->assertSame($expectedHeader, $header);
71+
}
72+
73+
public function testWithoutVersionInput()
74+
{
75+
$expectedHeader = [AgentHeaderDescriptor::AGENT_HEADER_KEY => [
76+
'gl-php/' . phpversion() .
77+
' gccl/ gapic/ gax/' . AgentHeaderDescriptor::GAX_VERSION .
78+
' grpc/' . phpversion('grpc')
79+
]];
80+
81+
$agentHeaderDescriptor = new AgentHeaderDescriptor([
82+
'libName' => 'gccl',
83+
]);
84+
$header = $agentHeaderDescriptor->getHeader();
85+
86+
$this->assertSame($expectedHeader, $header);
87+
}
88+
89+
public function testWithNullVersionInput()
90+
{
91+
$expectedHeader = [AgentHeaderDescriptor::AGENT_HEADER_KEY => [
92+
'gl-php/' . phpversion() .
93+
' gccl/ gapic/ gax/' . AgentHeaderDescriptor::GAX_VERSION .
94+
' grpc/' . phpversion('grpc')
95+
]];
96+
97+
$agentHeaderDescriptor = new AgentHeaderDescriptor([
98+
'libName' => 'gccl',
99+
'libVersion' => null,
100+
'gapicVersion' => null,
101+
'gaxVersion' => null,
102+
'phpVersion' => null,
103+
'grpcVersion' => null,
104+
]);
105+
$header = $agentHeaderDescriptor->getHeader();
106+
107+
$this->assertSame($expectedHeader, $header);
108+
}
109+
}

tests/ApiCallableTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,12 @@ public function testCustomHeader()
474474
{
475475
$stub = MockStub::create(new MockPageStreamingResponse());
476476
$headerDescriptor = new AgentHeaderDescriptor([
477-
'clientName' => 'testClient',
478-
'clientVersion' => '0.0.0',
479-
'codeGenName' => 'testCodeGen',
480-
'codeGenVersion' => '0.9.0',
477+
'libName' => 'gccl',
478+
'libVersion' => '0.0.0',
479+
'gapicVersion' => '0.9.0',
481480
'gaxVersion' => '1.0.0',
482481
'phpVersion' => '5.5.0',
482+
'grpcVersion' => '1.0.1'
483483
]);
484484
$apiCall = ApiCallable::createApiCall(
485485
$stub,
@@ -491,7 +491,7 @@ public function testCustomHeader()
491491
$actualCalls = $stub->getReceivedCalls();
492492
$this->assertEquals(1, count($actualCalls));
493493
$expectedMetadata = [
494-
'x-goog-api-client' => ['testClient/0.0.0 testCodeGen/0.9.0 gax/1.0.0 php/5.5.0']
494+
'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']
495495
];
496496
$this->assertEquals($expectedMetadata, $actualCalls[0]->getMetadata());
497497
}

0 commit comments

Comments
 (0)