MetricsTrait.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /*
  3. * Copyright 2024 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Google\Auth;
  18. /**
  19. * Trait containing helper methods required for enabling
  20. * observability metrics in the library.
  21. *
  22. * @internal
  23. */
  24. trait MetricsTrait
  25. {
  26. /**
  27. * @var string The version of the auth library php.
  28. */
  29. private static $version;
  30. /**
  31. * @var string The header key for the observability metrics.
  32. */
  33. protected static $metricMetadataKey = 'x-goog-api-client';
  34. /**
  35. * @param string $credType [Optional] The credential type.
  36. * Empty value will not add any credential type to the header.
  37. * Should be one of `'sa'`, `'jwt'`, `'imp'`, `'mds'`, `'u'`.
  38. * @param string $authRequestType [Optional] The auth request type.
  39. * Empty value will not add any auth request type to the header.
  40. * Should be one of `'at'`, `'it'`, `'mds'`.
  41. * @return string The header value for the observability metrics.
  42. */
  43. protected static function getMetricsHeader(
  44. $credType = '',
  45. $authRequestType = ''
  46. ): string {
  47. $value = sprintf(
  48. 'gl-php/%s auth/%s',
  49. PHP_VERSION,
  50. self::getVersion()
  51. );
  52. if (!empty($authRequestType)) {
  53. $value .= ' auth-request-type/' . $authRequestType;
  54. }
  55. if (!empty($credType)) {
  56. $value .= ' cred-type/' . $credType;
  57. }
  58. return $value;
  59. }
  60. /**
  61. * @param array<mixed> $metadata The metadata to update and return.
  62. * @return array<mixed> The updated metadata.
  63. */
  64. protected function applyServiceApiUsageMetrics($metadata)
  65. {
  66. if ($credType = $this->getCredType()) {
  67. // Add service api usage observability metrics info into metadata
  68. // We expect upstream libries to have the metadata key populated already
  69. $value = 'cred-type/' . $credType;
  70. if (!isset($metadata[self::$metricMetadataKey])) {
  71. // This case will happen only when someone invokes the updateMetadata
  72. // method on the credentials fetcher themselves.
  73. $metadata[self::$metricMetadataKey] = [$value];
  74. } elseif (is_array($metadata[self::$metricMetadataKey])) {
  75. $metadata[self::$metricMetadataKey][0] .= ' ' . $value;
  76. } else {
  77. $metadata[self::$metricMetadataKey] .= ' ' . $value;
  78. }
  79. }
  80. return $metadata;
  81. }
  82. /**
  83. * @param array<mixed> $metadata The metadata to update and return.
  84. * @param string $authRequestType The auth request type. Possible values are
  85. * `'at'`, `'it'`, `'mds'`.
  86. * @return array<mixed> The updated metadata.
  87. */
  88. protected function applyTokenEndpointMetrics($metadata, $authRequestType)
  89. {
  90. $metricsHeader = self::getMetricsHeader($this->getCredType(), $authRequestType);
  91. if (!isset($metadata[self::$metricMetadataKey])) {
  92. $metadata[self::$metricMetadataKey] = $metricsHeader;
  93. }
  94. return $metadata;
  95. }
  96. protected static function getVersion(): string
  97. {
  98. if (is_null(self::$version)) {
  99. $versionFilePath = __DIR__ . '/../VERSION';
  100. self::$version = trim((string) file_get_contents($versionFilePath));
  101. }
  102. return self::$version;
  103. }
  104. protected function getCredType(): string
  105. {
  106. return '';
  107. }
  108. }