AgentHeader.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. $metricsHeaders['gl-php'] = $headerInfo['phpVersion'] ?? phpversion();
  72. if (isset($headerInfo['libName'])) {
  73. $metricsHeaders[$headerInfo['libName']] =
  74. $headerInfo['libVersion'] ?? self::UNKNOWN_VERSION;
  75. }
  76. $apiCoreVersion = $headerInfo['apiCoreVersion'] ?? Version::getApiCoreVersion();
  77. $metricsHeaders['gapic'] = $headerInfo['gapicVersion'] ?? self::UNKNOWN_VERSION;
  78. $metricsHeaders['gax'] = $apiCoreVersion;
  79. // Context on library type identification (between gRPC+REST and REST-only):
  80. // This uses the gRPC extension's version if 'grpcVersion' is not set, so we
  81. // cannot use the presence of 'grpcVersion' to determine whether or not a library
  82. // is gRPC+REST or REST-only. However, we cannot use the extension's presence
  83. // either, since some clients may have the extension installed but opt to use a
  84. // REST-only library (e.g. GCE).
  85. // TODO: Should we stop sending empty gRPC headers?
  86. $metricsHeaders['grpc'] = $headerInfo['grpcVersion'] ?? phpversion('grpc');
  87. $metricsHeaders['rest'] = $headerInfo['restVersion'] ?? $apiCoreVersion;
  88. // The native version is not set by default because it is complex and costly to retrieve.
  89. // Users can override this default behavior if needed.
  90. $metricsHeaders['pb'] = $headerInfo['protobufVersion']
  91. ?? (phpversion('protobuf') ? phpversion('protobuf') . '+c' : '+n');
  92. $metricsList = [];
  93. foreach ($metricsHeaders as $key => $value) {
  94. $metricsList[] = $key . "/" . $value;
  95. }
  96. return [self::AGENT_HEADER_KEY => [implode(" ", $metricsList)]];
  97. }
  98. /**
  99. * Reads the gapic version string from a VERSION file. In order to determine the file
  100. * location, this method follows this procedure:
  101. * - accepts a class name $callingClass
  102. * - identifies the file defining that class
  103. * - searches up the directory structure for the 'src' directory
  104. * - looks in the directory above 'src' for a file named VERSION
  105. *
  106. * @param string $callingClass
  107. * @return string the gapic version
  108. * @throws \ReflectionException
  109. */
  110. public static function readGapicVersionFromFile(string $callingClass)
  111. {
  112. $callingClassFile = (new \ReflectionClass($callingClass))->getFileName();
  113. $versionFile = substr(
  114. $callingClassFile,
  115. 0,
  116. strrpos($callingClassFile, DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR)
  117. ) . DIRECTORY_SEPARATOR . 'VERSION';
  118. return Version::readVersionFile($versionFile);
  119. }
  120. }