3232
3333namespace 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}
0 commit comments