AgentHeader.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /*
  3. * Copyright 2016 Google LLC
  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\ApiCore;
  33. /**
  34. * Class containing functions used to build the Agent header.
  35. */
  36. class AgentHeader
  37. {
  38. const AGENT_HEADER_KEY = 'x-goog-api-client';
  39. const UNKNOWN_VERSION = '';
  40. /**
  41. * @param array $headerInfo {
  42. * Optional.
  43. *
  44. * @type string $phpVersion the PHP version.
  45. * @type string $libName the name of the client application.
  46. * @type string $libVersion the version of the client application.
  47. * @type string $gapicVersion the code generator version of the GAPIC library.
  48. * @type string $apiCoreVersion the ApiCore version.
  49. * @type string $grpcVersion the gRPC version.
  50. * @type string $restVersion the REST transport version (typically same as the
  51. * ApiCore version).
  52. * @type string $protobufVersion the protobuf version in format 'x.y.z+a' where both 'x.y.z'
  53. * and '+a' are optional, and where 'a' is a single letter representing the
  54. * implementation type of the protobuf runtime. It is recommended to use 'c' for a C
  55. * implementation, and 'n' for the native language implementation (PHP).
  56. * }
  57. * @return array Agent header array
  58. */
  59. public static function buildAgentHeader(array $headerInfo)
  60. {
  61. $metricsHeaders = [];
  62. // The ordering of the headers is important. We use the fact that $metricsHeaders is an
  63. // ordered dict. The desired ordering is:
  64. // - phpVersion (gl-php/)
  65. // - clientName (e.g. gccl/)
  66. // - gapicVersion (gapic/)
  67. // - apiCoreVersion (gax/)
  68. // - grpcVersion (grpc/)
  69. // - restVersion (rest/)
  70. // - protobufVersion (pb/)
  71. $phpVersion = isset($headerInfo['phpVersion'])
  72. ? $headerInfo['phpVersion']
  73. : phpversion();
  74. $metricsHeaders['gl-php'] = $phpVersion;
  75. if (isset($headerInfo['libName'])) {
  76. $clientVersion = isset($headerInfo['libVersion'])
  77. ? $headerInfo['libVersion']
  78. : self::UNKNOWN_VERSION;
  79. $metricsHeaders[$headerInfo['libName']] = $clientVersion;
  80. }
  81. $codeGenVersion = isset($headerInfo['gapicVersion'])
  82. ? $headerInfo['gapicVersion']
  83. : self::UNKNOWN_VERSION;
  84. $metricsHeaders['gapic'] = $codeGenVersion;
  85. $apiCoreVersion = isset($headerInfo['apiCoreVersion'])
  86. ? $headerInfo['apiCoreVersion']
  87. : Version::getApiCoreVersion();
  88. $metricsHeaders['gax'] = $apiCoreVersion;
  89. // Context on library type identification (between gRPC+REST and REST-only):
  90. // This uses the gRPC extension's version if 'grpcVersion' is not set, so we
  91. // cannot use the presence of 'grpcVersion' to determine whether or not a library
  92. // is gRPC+REST or REST-only. However, we cannot use the extension's presence
  93. // either, since some clients may have the extension installed but opt to use a
  94. // REST-only library (e.g. GCE).
  95. // TODO: Should we stop sending empty gRPC headers?
  96. $grpcVersion = isset($headerInfo['grpcVersion'])
  97. ? $headerInfo['grpcVersion']
  98. : phpversion('grpc');
  99. $metricsHeaders['grpc'] = $grpcVersion;
  100. $restVersion = isset($headerInfo['restVersion'])
  101. ? $headerInfo['restVersion']
  102. : $apiCoreVersion;
  103. $metricsHeaders['rest'] = $restVersion;
  104. // The native version is not set by default because it is complex and costly to retrieve.
  105. // Users can override this default behavior if needed.
  106. $protobufVersion = isset($headerInfo['protobufVersion'])
  107. ? $headerInfo['protobufVersion']
  108. : (phpversion('protobuf') ? phpversion('protobuf') . '+c' : '+n');
  109. $metricsHeaders['pb'] = $protobufVersion;
  110. $metricsList = [];
  111. foreach ($metricsHeaders as $key => $value) {
  112. $metricsList[] = $key . "/" . $value;
  113. }
  114. return [self::AGENT_HEADER_KEY => [implode(" ", $metricsList)]];
  115. }
  116. /**
  117. * Reads the gapic version string from a VERSION file. In order to determine the file
  118. * location, this method follows this procedure:
  119. * - accepts a class name $callingClass
  120. * - identifies the file defining that class
  121. * - searches up the directory structure for the 'src' directory
  122. * - looks in the directory above 'src' for a file named VERSION
  123. *
  124. * @param string $callingClass
  125. * @return string the gapic version
  126. * @throws \ReflectionException
  127. */
  128. public static function readGapicVersionFromFile(string $callingClass)
  129. {
  130. $callingClassFile = (new \ReflectionClass($callingClass))->getFileName();
  131. $versionFile = substr(
  132. $callingClassFile,
  133. 0,
  134. strrpos($callingClassFile, DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR)
  135. ) . DIRECTORY_SEPARATOR . 'VERSION';
  136. return Version::readVersionFile($versionFile);
  137. }
  138. }