metrics_utils.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #!/usr/bin/env python
  2. # Copyright (c) 2018 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. from __future__ import print_function
  6. import re
  7. import scm
  8. import subprocess2
  9. import sys
  10. import urlparse
  11. from third_party import colorama
  12. # Current version of metrics recording.
  13. # When we add new metrics, the version number will be increased, we display the
  14. # user what has changed, and ask the user to agree again.
  15. CURRENT_VERSION = 1
  16. APP_URL = 'https://cit-cli-metrics.appspot.com'
  17. EMPTY_LINE = (
  18. '* *'
  19. )
  20. NOTICE_COUNTDOWN_HEADER = (
  21. '*****************************************************\n'
  22. '* METRICS COLLECTION WILL START IN %2d EXECUTIONS *'
  23. )
  24. NOTICE_COLLECTION_HEADER = (
  25. '*****************************************************\n'
  26. '* METRICS COLLECTION IS TAKING PLACE *'
  27. )
  28. NOTICE_VERSION_CHANGE_HEADER = (
  29. '*****************************************************\n'
  30. '* WE ARE COLLECTING ADDITIONAL METRICS *\n'
  31. '* *\n'
  32. '* Please review the changes and opt-in again. *'
  33. )
  34. NOTICE_FOOTER = (
  35. '* For more information, and for how to disable this *\n'
  36. '* message, please see metrics.README.md in your *\n'
  37. '* depot_tools checkout or visit *\n'
  38. '* https://bit.ly/2ufRS4p. *\n'
  39. '*****************************************************\n'
  40. )
  41. CHANGE_NOTICE = {
  42. # No changes for version 0
  43. 0: '',
  44. 1: ('* We want to collect the Git version. *\n'
  45. '* We want to collect information about the HTTP *\n'
  46. '* requests that depot_tools makes, and the git and *\n'
  47. '* cipd commands it executes. *\n'
  48. '* *\n'
  49. '* We only collect known strings to make sure we *\n'
  50. '* don\'t record PII. *')
  51. }
  52. KNOWN_PROJECT_URLS = {
  53. 'https://chrome-internal.googlesource.com/chrome/ios_internal',
  54. 'https://chrome-internal.googlesource.com/infra/infra_internal',
  55. 'https://chromium.googlesource.com/breakpad/breakpad',
  56. 'https://chromium.googlesource.com/chromium/src',
  57. 'https://chromium.googlesource.com/chromium/tools/depot_tools',
  58. 'https://chromium.googlesource.com/crashpad/crashpad',
  59. 'https://chromium.googlesource.com/external/gyp',
  60. 'https://chromium.googlesource.com/external/naclports',
  61. 'https://chromium.googlesource.com/infra/goma/client',
  62. 'https://chromium.googlesource.com/infra/infra',
  63. 'https://chromium.googlesource.com/native_client/',
  64. 'https://chromium.googlesource.com/syzygy',
  65. 'https://chromium.googlesource.com/v8/v8',
  66. 'https://dart.googlesource.com/sdk',
  67. 'https://pdfium.googlesource.com/pdfium',
  68. 'https://skia.googlesource.com/buildbot',
  69. 'https://skia.googlesource.com/skia',
  70. 'https://webrtc.googlesource.com/src',
  71. }
  72. KNOWN_HTTP_HOSTS = {
  73. 'chrome-internal-review.googlesource.com',
  74. 'chromium-review.googlesource.com',
  75. 'dart-review.googlesource.com',
  76. 'eu1-mirror-chromium-review.googlesource.com',
  77. 'pdfium-review.googlesource.com',
  78. 'skia-review.googlesource.com',
  79. 'us1-mirror-chromium-review.googlesource.com',
  80. 'us2-mirror-chromium-review.googlesource.com',
  81. 'us3-mirror-chromium-review.googlesource.com',
  82. 'webrtc-review.googlesource.com',
  83. }
  84. KNOWN_HTTP_METHODS = {
  85. 'DELETE',
  86. 'GET',
  87. 'PATCH',
  88. 'POST',
  89. 'PUT',
  90. }
  91. KNOWN_HTTP_PATHS = {
  92. 'accounts':
  93. re.compile(r'(/a)?/accounts/.*'),
  94. 'changes':
  95. re.compile(r'(/a)?/changes/([^/]+)?$'),
  96. 'changes/abandon':
  97. re.compile(r'(/a)?/changes/.*/abandon'),
  98. 'changes/comments':
  99. re.compile(r'(/a)?/changes/.*/comments'),
  100. 'changes/detail':
  101. re.compile(r'(/a)?/changes/.*/detail'),
  102. 'changes/edit':
  103. re.compile(r'(/a)?/changes/.*/edit'),
  104. 'changes/message':
  105. re.compile(r'(/a)?/changes/.*/message'),
  106. 'changes/restore':
  107. re.compile(r'(/a)?/changes/.*/restore'),
  108. 'changes/reviewers':
  109. re.compile(r'(/a)?/changes/.*/reviewers/.*'),
  110. 'changes/revisions/commit':
  111. re.compile(r'(/a)?/changes/.*/revisions/.*/commit'),
  112. 'changes/revisions/review':
  113. re.compile(r'(/a)?/changes/.*/revisions/.*/review'),
  114. 'changes/submit':
  115. re.compile(r'(/a)?/changes/.*/submit'),
  116. 'projects/branches':
  117. re.compile(r'(/a)?/projects/.*/branches/.*'),
  118. }
  119. KNOWN_HTTP_ARGS = {
  120. 'ALL_REVISIONS',
  121. 'CURRENT_COMMIT',
  122. 'CURRENT_REVISION',
  123. 'DETAILED_ACCOUNTS',
  124. 'LABELS',
  125. }
  126. def get_python_version():
  127. """Return the python version in the major.minor.micro format."""
  128. return '{v.major}.{v.minor}.{v.micro}'.format(v=sys.version_info)
  129. def return_code_from_exception(exception):
  130. """Returns the exit code that would result of raising the exception."""
  131. if exception is None:
  132. return 0
  133. if isinstance(exception[1], SystemExit):
  134. return exception[1].code
  135. return 1
  136. def seconds_to_weeks(duration):
  137. """Transform a |duration| from seconds to weeks approximately.
  138. Drops the lowest 19 bits of the integer representation, which ammounts to
  139. about 6 days.
  140. """
  141. return int(duration) >> 19
  142. def extract_http_metrics(request_uri, method, status, response_time):
  143. """Extract metrics from the request URI.
  144. Extracts the host, path, and arguments from the request URI, and returns them
  145. along with the method, status and response time.
  146. The host, method, path and arguments must be in the KNOWN_HTTP_* constants
  147. defined above.
  148. Arguments are the values of the o= url parameter. In Gerrit, additional fields
  149. can be obtained by adding o parameters, each option requires more database
  150. lookups and slows down the query response time to the client, so we make an
  151. effort to collect them.
  152. The regex defined in KNOWN_HTTP_PATH_RES are checked against the path, and
  153. those that match will be returned.
  154. """
  155. http_metrics = {
  156. 'status': status,
  157. 'response_time': response_time,
  158. }
  159. if method in KNOWN_HTTP_METHODS:
  160. http_metrics['method'] = method
  161. parsed_url = urlparse.urlparse(request_uri)
  162. if parsed_url.netloc in KNOWN_HTTP_HOSTS:
  163. http_metrics['host'] = parsed_url.netloc
  164. for name, path_re in KNOWN_HTTP_PATHS.iteritems():
  165. if path_re.match(parsed_url.path):
  166. http_metrics['path'] = name
  167. break
  168. parsed_query = urlparse.parse_qs(parsed_url.query)
  169. # Collect o-parameters from the request.
  170. args = [
  171. arg for arg in parsed_query.get('o', [])
  172. if arg in KNOWN_HTTP_ARGS
  173. ]
  174. if args:
  175. http_metrics['arguments'] = args
  176. return http_metrics
  177. def get_repo_timestamp(path_to_repo):
  178. """Get an approximate timestamp for the upstream of |path_to_repo|.
  179. Returns the top two bits of the timestamp of the HEAD for the upstream of the
  180. branch path_to_repo is checked out at.
  181. """
  182. # Get the upstream for the current branch. If we're not in a branch, fallback
  183. # to HEAD.
  184. try:
  185. upstream = scm.GIT.GetUpstreamBranch(path_to_repo)
  186. except subprocess2.CalledProcessError:
  187. upstream = 'HEAD'
  188. # Get the timestamp of the HEAD for the upstream of the current branch.
  189. p = subprocess2.Popen(
  190. ['git', '-C', path_to_repo, 'log', '-n1', upstream, '--format=%at'],
  191. stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
  192. stdout, _ = p.communicate()
  193. # If there was an error, give up.
  194. if p.returncode != 0:
  195. return None
  196. # Get the age of the checkout in weeks.
  197. return seconds_to_weeks(stdout.strip())
  198. def print_notice(countdown):
  199. """Print a notice to let the user know the status of metrics collection."""
  200. colorama.init()
  201. print(colorama.Fore.RED + '\033[1m', file=sys.stderr, end='')
  202. if countdown:
  203. print(NOTICE_COUNTDOWN_HEADER % countdown, file=sys.stderr)
  204. else:
  205. print(NOTICE_COLLECTION_HEADER, file=sys.stderr)
  206. print(EMPTY_LINE, file=sys.stderr)
  207. print(NOTICE_FOOTER + colorama.Style.RESET_ALL, file=sys.stderr)
  208. def print_version_change(config_version):
  209. """Print a notice to let the user know we are collecting more metrics."""
  210. colorama.init()
  211. print(colorama.Fore.RED + '\033[1m', file=sys.stderr, end='')
  212. print(NOTICE_VERSION_CHANGE_HEADER, file=sys.stderr)
  213. print(EMPTY_LINE, file=sys.stderr)
  214. for version in range(config_version + 1, CURRENT_VERSION + 1):
  215. print(CHANGE_NOTICE[version], file=sys.stderr)
  216. print(EMPTY_LINE, file=sys.stderr)