source: vendor/google/auth/src/MetricsTrait.php@ e3d4e0a

Last change on this file since e3d4e0a was e3d4e0a, checked in by Vlado 222039 <vlado.popovski@…>, 7 days ago

Upload project files

  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[e3d4e0a]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
18namespace Google\Auth;
19
20/**
21 * Trait containing helper methods required for enabling
22 * observability metrics in the library.
23 *
24 * @internal
25 */
26trait MetricsTrait
27{
28 /**
29 * @var string The version of the auth library php.
30 */
31 private static $version;
32
33 /**
34 * @var string The header key for the observability metrics.
35 */
36 protected static $metricMetadataKey = 'x-goog-api-client';
37
38 /**
39 * @param string $credType [Optional] The credential type.
40 * Empty value will not add any credential type to the header.
41 * Should be one of `'sa'`, `'jwt'`, `'imp'`, `'mds'`, `'u'`.
42 * @param string $authRequestType [Optional] The auth request type.
43 * Empty value will not add any auth request type to the header.
44 * Should be one of `'at'`, `'it'`, `'mds'`.
45 * @return string The header value for the observability metrics.
46 */
47 protected static function getMetricsHeader(
48 $credType = '',
49 $authRequestType = ''
50 ): string {
51 $value = sprintf(
52 'gl-php/%s auth/%s',
53 PHP_VERSION,
54 self::getVersion()
55 );
56
57 if (!empty($authRequestType)) {
58 $value .= ' auth-request-type/' . $authRequestType;
59 }
60
61 if (!empty($credType)) {
62 $value .= ' cred-type/' . $credType;
63 }
64
65 return $value;
66 }
67
68 /**
69 * @param array<mixed> $metadata The metadata to update and return.
70 * @return array<mixed> The updated metadata.
71 */
72 protected function applyServiceApiUsageMetrics($metadata)
73 {
74 if ($credType = $this->getCredType()) {
75 // Add service api usage observability metrics info into metadata
76 // We expect upstream libries to have the metadata key populated already
77 $value = 'cred-type/' . $credType;
78 if (!isset($metadata[self::$metricMetadataKey])) {
79 // This case will happen only when someone invokes the updateMetadata
80 // method on the credentials fetcher themselves.
81 $metadata[self::$metricMetadataKey] = [$value];
82 } elseif (is_array($metadata[self::$metricMetadataKey])) {
83 $metadata[self::$metricMetadataKey][0] .= ' ' . $value;
84 } else {
85 $metadata[self::$metricMetadataKey] .= ' ' . $value;
86 }
87 }
88
89 return $metadata;
90 }
91
92 /**
93 * @param array<mixed> $metadata The metadata to update and return.
94 * @param string $authRequestType The auth request type. Possible values are
95 * `'at'`, `'it'`, `'mds'`.
96 * @return array<mixed> The updated metadata.
97 */
98 protected function applyTokenEndpointMetrics($metadata, $authRequestType)
99 {
100 $metricsHeader = self::getMetricsHeader($this->getCredType(), $authRequestType);
101 if (!isset($metadata[self::$metricMetadataKey])) {
102 $metadata[self::$metricMetadataKey] = $metricsHeader;
103 }
104 return $metadata;
105 }
106
107 protected static function getVersion(): string
108 {
109 if (is_null(self::$version)) {
110 $versionFilePath = __DIR__ . '/../VERSION';
111 self::$version = trim((string) file_get_contents($versionFilePath));
112 }
113 return self::$version;
114 }
115
116 protected function getCredType(): string
117 {
118 return '';
119 }
120}
Note: See TracBrowser for help on using the repository browser.