metrics.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright 2023 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """ We use x-goog-api-client header to report metrics. This module provides
  15. the constants and helper methods to construct x-goog-api-client header.
  16. """
  17. import platform
  18. from google.auth import version
  19. API_CLIENT_HEADER = "x-goog-api-client"
  20. # BYOID Specific consts
  21. BYOID_HEADER_SECTION = "google-byoid-sdk"
  22. # Auth request type
  23. REQUEST_TYPE_ACCESS_TOKEN = "auth-request-type/at"
  24. REQUEST_TYPE_ID_TOKEN = "auth-request-type/it"
  25. REQUEST_TYPE_MDS_PING = "auth-request-type/mds"
  26. REQUEST_TYPE_REAUTH_START = "auth-request-type/re-start"
  27. REQUEST_TYPE_REAUTH_CONTINUE = "auth-request-type/re-cont"
  28. # Credential type
  29. CRED_TYPE_USER = "cred-type/u"
  30. CRED_TYPE_SA_ASSERTION = "cred-type/sa"
  31. CRED_TYPE_SA_JWT = "cred-type/jwt"
  32. CRED_TYPE_SA_MDS = "cred-type/mds"
  33. CRED_TYPE_SA_IMPERSONATE = "cred-type/imp"
  34. # Versions
  35. def python_and_auth_lib_version():
  36. return "gl-python/{} auth/{}".format(platform.python_version(), version.__version__)
  37. # Token request metric header values
  38. # x-goog-api-client header value for access token request via metadata server.
  39. # Example: "gl-python/3.7 auth/1.1 auth-request-type/at cred-type/mds"
  40. def token_request_access_token_mds():
  41. return "{} {} {}".format(
  42. python_and_auth_lib_version(), REQUEST_TYPE_ACCESS_TOKEN, CRED_TYPE_SA_MDS
  43. )
  44. # x-goog-api-client header value for ID token request via metadata server.
  45. # Example: "gl-python/3.7 auth/1.1 auth-request-type/it cred-type/mds"
  46. def token_request_id_token_mds():
  47. return "{} {} {}".format(
  48. python_and_auth_lib_version(), REQUEST_TYPE_ID_TOKEN, CRED_TYPE_SA_MDS
  49. )
  50. # x-goog-api-client header value for impersonated credentials access token request.
  51. # Example: "gl-python/3.7 auth/1.1 auth-request-type/at cred-type/imp"
  52. def token_request_access_token_impersonate():
  53. return "{} {} {}".format(
  54. python_and_auth_lib_version(),
  55. REQUEST_TYPE_ACCESS_TOKEN,
  56. CRED_TYPE_SA_IMPERSONATE,
  57. )
  58. # x-goog-api-client header value for impersonated credentials ID token request.
  59. # Example: "gl-python/3.7 auth/1.1 auth-request-type/it cred-type/imp"
  60. def token_request_id_token_impersonate():
  61. return "{} {} {}".format(
  62. python_and_auth_lib_version(), REQUEST_TYPE_ID_TOKEN, CRED_TYPE_SA_IMPERSONATE
  63. )
  64. # x-goog-api-client header value for service account credentials access token
  65. # request (assertion flow).
  66. # Example: "gl-python/3.7 auth/1.1 auth-request-type/at cred-type/sa"
  67. def token_request_access_token_sa_assertion():
  68. return "{} {} {}".format(
  69. python_and_auth_lib_version(), REQUEST_TYPE_ACCESS_TOKEN, CRED_TYPE_SA_ASSERTION
  70. )
  71. # x-goog-api-client header value for service account credentials ID token
  72. # request (assertion flow).
  73. # Example: "gl-python/3.7 auth/1.1 auth-request-type/it cred-type/sa"
  74. def token_request_id_token_sa_assertion():
  75. return "{} {} {}".format(
  76. python_and_auth_lib_version(), REQUEST_TYPE_ID_TOKEN, CRED_TYPE_SA_ASSERTION
  77. )
  78. # x-goog-api-client header value for user credentials token request.
  79. # Example: "gl-python/3.7 auth/1.1 cred-type/u"
  80. def token_request_user():
  81. return "{} {}".format(python_and_auth_lib_version(), CRED_TYPE_USER)
  82. # Miscellenous metrics
  83. # x-goog-api-client header value for metadata server ping.
  84. # Example: "gl-python/3.7 auth/1.1 auth-request-type/mds"
  85. def mds_ping():
  86. return "{} {}".format(python_and_auth_lib_version(), REQUEST_TYPE_MDS_PING)
  87. # x-goog-api-client header value for reauth start endpoint calls.
  88. # Example: "gl-python/3.7 auth/1.1 auth-request-type/re-start"
  89. def reauth_start():
  90. return "{} {}".format(python_and_auth_lib_version(), REQUEST_TYPE_REAUTH_START)
  91. # x-goog-api-client header value for reauth continue endpoint calls.
  92. # Example: "gl-python/3.7 auth/1.1 cred-type/re-cont"
  93. def reauth_continue():
  94. return "{} {}".format(python_and_auth_lib_version(), REQUEST_TYPE_REAUTH_CONTINUE)
  95. # x-goog-api-client header value for BYOID calls to the Security Token Service exchange token endpoint.
  96. # Example: "gl-python/3.7 auth/1.1 google-byoid-sdk source/aws sa-impersonation/true sa-impersonation/true"
  97. def byoid_metrics_header(metrics_options):
  98. header = "{} {}".format(python_and_auth_lib_version(), BYOID_HEADER_SECTION)
  99. for key, value in metrics_options.items():
  100. header = "{} {}/{}".format(header, key, value)
  101. return header
  102. def add_metric_header(headers, metric_header_value):
  103. """Add x-goog-api-client header with the given value.
  104. Args:
  105. headers (Mapping[str, str]): The headers to which we will add the
  106. metric header.
  107. metric_header_value (Optional[str]): If value is None, do nothing;
  108. if headers already has a x-goog-api-client header, append the value
  109. to the existing header; otherwise add a new x-goog-api-client
  110. header with the given value.
  111. """
  112. if not metric_header_value:
  113. return
  114. if API_CLIENT_HEADER not in headers:
  115. headers[API_CLIENT_HEADER] = metric_header_value
  116. else:
  117. headers[API_CLIENT_HEADER] += " " + metric_header_value