blob: 60328a6c4ec5dff131cad145013500606f162aba [file] [log] [blame]
Edward Lemur32e3d1e2018-07-12 00:54:05 +00001#!/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
Edward Lemurc87d45b2018-07-26 17:43:11 +00006from __future__ import print_function
7
Edward Lemur03d6d112018-10-23 15:17:36 +00008import re
Edward Lemur32e3d1e2018-07-12 00:54:05 +00009import scm
10import subprocess2
11import sys
Edward Lemur03d6d112018-10-23 15:17:36 +000012import urlparse
Edward Lemur32e3d1e2018-07-12 00:54:05 +000013
14from third_party import colorama
15
16
Edward Lemur48836262018-10-18 02:08:06 +000017# Current version of metrics recording.
18# When we add new metrics, the version number will be increased, we display the
19# user what has changed, and ask the user to agree again.
Edward Lemur5a9ff432018-10-30 19:00:22 +000020CURRENT_VERSION = 1
Edward Lemur48836262018-10-18 02:08:06 +000021
Edward Lemur5ba1e9c2018-07-23 18:19:02 +000022APP_URL = 'https://cit-cli-metrics.appspot.com'
23
Edward Lemur48836262018-10-18 02:08:06 +000024EMPTY_LINE = (
25 '* *'
26)
Edward Lemur32e3d1e2018-07-12 00:54:05 +000027NOTICE_COUNTDOWN_HEADER = (
28 '*****************************************************\n'
29 '* METRICS COLLECTION WILL START IN %2d EXECUTIONS *'
30)
31NOTICE_COLLECTION_HEADER = (
32 '*****************************************************\n'
33 '* METRICS COLLECTION IS TAKING PLACE *'
34)
Edward Lemur48836262018-10-18 02:08:06 +000035NOTICE_VERSION_CHANGE_HEADER = (
36 '*****************************************************\n'
Edward Lemur5a9ff432018-10-30 19:00:22 +000037 '* WE ARE COLLECTING ADDITIONAL METRICS *\n'
38 '* *\n'
39 '* Please review the changes and opt-in again. *'
Edward Lemur48836262018-10-18 02:08:06 +000040)
Edward Lemur32e3d1e2018-07-12 00:54:05 +000041NOTICE_FOOTER = (
Peter Boströme28390c2018-11-09 19:24:17 +000042 '* To suppress this message opt in or out using: *\n'
43 '* $ gclient metrics [--opt-in] [--opt-out] *\n'
44 '* For more information please see metrics.README.md *\n'
45 '* in your depot_tools checkout or visit *\n'
Edward Lemur5a9ff432018-10-30 19:00:22 +000046 '* https://bit.ly/2ufRS4p. *\n'
Edward Lemur32e3d1e2018-07-12 00:54:05 +000047 '*****************************************************\n'
48)
49
Edward Lemur48836262018-10-18 02:08:06 +000050CHANGE_NOTICE = {
51 # No changes for version 0
52 0: '',
Edward Lemur5a9ff432018-10-30 19:00:22 +000053 1: ('* We want to collect the Git version. *\n'
54 '* We want to collect information about the HTTP *\n'
55 '* requests that depot_tools makes, and the git and *\n'
56 '* cipd commands it executes. *\n'
57 '* *\n'
58 '* We only collect known strings to make sure we *\n'
59 '* don\'t record PII. *')
Edward Lemur48836262018-10-18 02:08:06 +000060}
61
62
Edward Lemur40764b02018-07-20 18:50:29 +000063KNOWN_PROJECT_URLS = {
64 'https://chrome-internal.googlesource.com/chrome/ios_internal',
65 'https://chrome-internal.googlesource.com/infra/infra_internal',
66 'https://chromium.googlesource.com/breakpad/breakpad',
67 'https://chromium.googlesource.com/chromium/src',
68 'https://chromium.googlesource.com/chromium/tools/depot_tools',
69 'https://chromium.googlesource.com/crashpad/crashpad',
70 'https://chromium.googlesource.com/external/gyp',
71 'https://chromium.googlesource.com/external/naclports',
72 'https://chromium.googlesource.com/infra/goma/client',
73 'https://chromium.googlesource.com/infra/infra',
74 'https://chromium.googlesource.com/native_client/',
75 'https://chromium.googlesource.com/syzygy',
76 'https://chromium.googlesource.com/v8/v8',
77 'https://dart.googlesource.com/sdk',
78 'https://pdfium.googlesource.com/pdfium',
79 'https://skia.googlesource.com/buildbot',
80 'https://skia.googlesource.com/skia',
81 'https://webrtc.googlesource.com/src',
82}
83
Edward Lemur03d6d112018-10-23 15:17:36 +000084KNOWN_HTTP_HOSTS = {
85 'chrome-internal-review.googlesource.com',
86 'chromium-review.googlesource.com',
87 'dart-review.googlesource.com',
88 'eu1-mirror-chromium-review.googlesource.com',
89 'pdfium-review.googlesource.com',
90 'skia-review.googlesource.com',
91 'us1-mirror-chromium-review.googlesource.com',
92 'us2-mirror-chromium-review.googlesource.com',
93 'us3-mirror-chromium-review.googlesource.com',
94 'webrtc-review.googlesource.com',
95}
96
97KNOWN_HTTP_METHODS = {
98 'DELETE',
99 'GET',
100 'PATCH',
101 'POST',
102 'PUT',
103}
104
105KNOWN_HTTP_PATHS = {
106 'accounts':
107 re.compile(r'(/a)?/accounts/.*'),
108 'changes':
109 re.compile(r'(/a)?/changes/([^/]+)?$'),
110 'changes/abandon':
111 re.compile(r'(/a)?/changes/.*/abandon'),
112 'changes/comments':
113 re.compile(r'(/a)?/changes/.*/comments'),
114 'changes/detail':
115 re.compile(r'(/a)?/changes/.*/detail'),
116 'changes/edit':
117 re.compile(r'(/a)?/changes/.*/edit'),
118 'changes/message':
119 re.compile(r'(/a)?/changes/.*/message'),
120 'changes/restore':
121 re.compile(r'(/a)?/changes/.*/restore'),
122 'changes/reviewers':
123 re.compile(r'(/a)?/changes/.*/reviewers/.*'),
124 'changes/revisions/commit':
125 re.compile(r'(/a)?/changes/.*/revisions/.*/commit'),
126 'changes/revisions/review':
127 re.compile(r'(/a)?/changes/.*/revisions/.*/review'),
128 'changes/submit':
129 re.compile(r'(/a)?/changes/.*/submit'),
130 'projects/branches':
131 re.compile(r'(/a)?/projects/.*/branches/.*'),
132}
133
134KNOWN_HTTP_ARGS = {
135 'ALL_REVISIONS',
136 'CURRENT_COMMIT',
137 'CURRENT_REVISION',
138 'DETAILED_ACCOUNTS',
139 'LABELS',
140}
141
Edward Lemur861640f2018-10-31 19:45:31 +0000142GIT_VERSION_RE = re.compile(
143 r'git version (\d)\.(\d{0,2})\.(\d{0,2})'
144)
145
Edward Lemurfec80c42018-11-01 23:14:14 +0000146KNOWN_SUBCOMMAND_ARGS = {
147 'cc',
148 'hashtag',
149 'l=Auto-Submit+1',
150 'l=Commit-Queue+1',
151 'l=Commit-Queue+2',
152 'label',
153 'm',
154 'notify=ALL',
155 'notify=NONE',
156 'private',
157 'r',
158 'ready',
159 'topic',
160 'wip'
161}
162
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000163
164def get_python_version():
165 """Return the python version in the major.minor.micro format."""
166 return '{v.major}.{v.minor}.{v.micro}'.format(v=sys.version_info)
167
168
Edward Lemur861640f2018-10-31 19:45:31 +0000169def get_git_version():
170 """Return the Git version in the major.minor.micro format."""
171 p = subprocess2.Popen(
172 ['git', '--version'],
173 stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
174 stdout, _ = p.communicate()
175 match = GIT_VERSION_RE.match(stdout)
176 if not match:
177 return None
178 return '%s.%s.%s' % match.groups()
179
180
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000181def return_code_from_exception(exception):
182 """Returns the exit code that would result of raising the exception."""
183 if exception is None:
184 return 0
185 if isinstance(exception[1], SystemExit):
186 return exception[1].code
187 return 1
188
189
190def seconds_to_weeks(duration):
191 """Transform a |duration| from seconds to weeks approximately.
192
193 Drops the lowest 19 bits of the integer representation, which ammounts to
194 about 6 days.
195 """
196 return int(duration) >> 19
197
198
Edward Lemurfec80c42018-11-01 23:14:14 +0000199def extract_known_subcommand_args(args):
200 """Extract the known arguments from the passed list of args."""
201 known_args = []
202 for arg in args:
203 if arg in KNOWN_SUBCOMMAND_ARGS:
204 known_args.append(arg)
205 else:
206 arg = arg.split('=')[0]
207 if arg in KNOWN_SUBCOMMAND_ARGS:
208 known_args.append(arg)
Edward Lemur01f4a4f2018-11-03 00:40:38 +0000209 return sorted(known_args)
Edward Lemurfec80c42018-11-01 23:14:14 +0000210
211
Edward Lemur03d6d112018-10-23 15:17:36 +0000212def extract_http_metrics(request_uri, method, status, response_time):
213 """Extract metrics from the request URI.
214
215 Extracts the host, path, and arguments from the request URI, and returns them
216 along with the method, status and response time.
217
218 The host, method, path and arguments must be in the KNOWN_HTTP_* constants
219 defined above.
220
221 Arguments are the values of the o= url parameter. In Gerrit, additional fields
222 can be obtained by adding o parameters, each option requires more database
223 lookups and slows down the query response time to the client, so we make an
224 effort to collect them.
225
226 The regex defined in KNOWN_HTTP_PATH_RES are checked against the path, and
227 those that match will be returned.
228 """
229 http_metrics = {
230 'status': status,
231 'response_time': response_time,
232 }
233
234 if method in KNOWN_HTTP_METHODS:
235 http_metrics['method'] = method
236
237 parsed_url = urlparse.urlparse(request_uri)
238
239 if parsed_url.netloc in KNOWN_HTTP_HOSTS:
240 http_metrics['host'] = parsed_url.netloc
241
242 for name, path_re in KNOWN_HTTP_PATHS.iteritems():
243 if path_re.match(parsed_url.path):
244 http_metrics['path'] = name
245 break
246
247 parsed_query = urlparse.parse_qs(parsed_url.query)
248
249 # Collect o-parameters from the request.
250 args = [
251 arg for arg in parsed_query.get('o', [])
252 if arg in KNOWN_HTTP_ARGS
253 ]
254 if args:
255 http_metrics['arguments'] = args
256
257 return http_metrics
258
259
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000260def get_repo_timestamp(path_to_repo):
261 """Get an approximate timestamp for the upstream of |path_to_repo|.
262
263 Returns the top two bits of the timestamp of the HEAD for the upstream of the
264 branch path_to_repo is checked out at.
265 """
266 # Get the upstream for the current branch. If we're not in a branch, fallback
267 # to HEAD.
268 try:
269 upstream = scm.GIT.GetUpstreamBranch(path_to_repo)
270 except subprocess2.CalledProcessError:
271 upstream = 'HEAD'
272
273 # Get the timestamp of the HEAD for the upstream of the current branch.
274 p = subprocess2.Popen(
275 ['git', '-C', path_to_repo, 'log', '-n1', upstream, '--format=%at'],
276 stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
277 stdout, _ = p.communicate()
278
279 # If there was an error, give up.
280 if p.returncode != 0:
281 return None
282
283 # Get the age of the checkout in weeks.
284 return seconds_to_weeks(stdout.strip())
285
286
287def print_notice(countdown):
288 """Print a notice to let the user know the status of metrics collection."""
289 colorama.init()
Edward Lemur48836262018-10-18 02:08:06 +0000290 print(colorama.Fore.RED + '\033[1m', file=sys.stderr, end='')
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000291 if countdown:
Edward Lemurc87d45b2018-07-26 17:43:11 +0000292 print(NOTICE_COUNTDOWN_HEADER % countdown, file=sys.stderr)
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000293 else:
Edward Lemurc87d45b2018-07-26 17:43:11 +0000294 print(NOTICE_COLLECTION_HEADER, file=sys.stderr)
Edward Lemur48836262018-10-18 02:08:06 +0000295 print(EMPTY_LINE, file=sys.stderr)
Edward Lemurc87d45b2018-07-26 17:43:11 +0000296 print(NOTICE_FOOTER + colorama.Style.RESET_ALL, file=sys.stderr)
Edward Lemur48836262018-10-18 02:08:06 +0000297
298
299def print_version_change(config_version):
300 """Print a notice to let the user know we are collecting more metrics."""
301 colorama.init()
302 print(colorama.Fore.RED + '\033[1m', file=sys.stderr, end='')
303 print(NOTICE_VERSION_CHANGE_HEADER, file=sys.stderr)
304 print(EMPTY_LINE, file=sys.stderr)
305 for version in range(config_version + 1, CURRENT_VERSION + 1):
306 print(CHANGE_NOTICE[version], file=sys.stderr)
307 print(EMPTY_LINE, file=sys.stderr)