blob: ed908cb1c5858924b7f758538f0ab9fb62721e11 [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
Samuel Huang98a7e802019-02-12 15:32:22 +000024def get_notice_countdown_header(countdown):
25 if countdown == 0:
26 yield ' METRICS COLLECTION IS TAKING PLACE'
27 else:
28 yield ' METRICS COLLECTION WILL START IN %d EXECUTIONS' % countdown
Edward Lemur32e3d1e2018-07-12 00:54:05 +000029
Samuel Huang98a7e802019-02-12 15:32:22 +000030def get_notice_version_change_header():
31 yield ' WE ARE COLLECTING ADDITIONAL METRICS'
32 yield ''
33 yield ' Please review the changes and opt-in again.'
34
35def get_notice_footer():
36 yield 'To suppress this message opt in or out using:'
37 yield '$ gclient metrics [--opt-in] [--opt-out]'
38 yield 'For more information please see metrics.README.md'
39 yield 'in your depot_tools checkout or visit'
40 yield 'https://goo.gl/yNpRDV.'
41
42def get_change_notice(version):
43 if version == 0:
44 pass # No changes for version 0
45 elif version == 1:
46 yield 'We want to collect the Git version.'
47 yield 'We want to collect information about the HTTP'
48 yield 'requests that depot_tools makes, and the git and'
49 yield 'cipd commands it executes.'
50 yield ''
51 yield 'We only collect known strings to make sure we'
52 yield 'don\'t record PII.'
Edward Lemur48836262018-10-18 02:08:06 +000053
54
Edward Lemur40764b02018-07-20 18:50:29 +000055KNOWN_PROJECT_URLS = {
56 'https://chrome-internal.googlesource.com/chrome/ios_internal',
57 'https://chrome-internal.googlesource.com/infra/infra_internal',
58 'https://chromium.googlesource.com/breakpad/breakpad',
59 'https://chromium.googlesource.com/chromium/src',
60 'https://chromium.googlesource.com/chromium/tools/depot_tools',
61 'https://chromium.googlesource.com/crashpad/crashpad',
62 'https://chromium.googlesource.com/external/gyp',
63 'https://chromium.googlesource.com/external/naclports',
64 'https://chromium.googlesource.com/infra/goma/client',
65 'https://chromium.googlesource.com/infra/infra',
66 'https://chromium.googlesource.com/native_client/',
67 'https://chromium.googlesource.com/syzygy',
68 'https://chromium.googlesource.com/v8/v8',
69 'https://dart.googlesource.com/sdk',
70 'https://pdfium.googlesource.com/pdfium',
71 'https://skia.googlesource.com/buildbot',
72 'https://skia.googlesource.com/skia',
73 'https://webrtc.googlesource.com/src',
74}
75
Edward Lemur03d6d112018-10-23 15:17:36 +000076KNOWN_HTTP_HOSTS = {
77 'chrome-internal-review.googlesource.com',
78 'chromium-review.googlesource.com',
79 'dart-review.googlesource.com',
80 'eu1-mirror-chromium-review.googlesource.com',
81 'pdfium-review.googlesource.com',
82 'skia-review.googlesource.com',
83 'us1-mirror-chromium-review.googlesource.com',
84 'us2-mirror-chromium-review.googlesource.com',
85 'us3-mirror-chromium-review.googlesource.com',
86 'webrtc-review.googlesource.com',
87}
88
89KNOWN_HTTP_METHODS = {
90 'DELETE',
91 'GET',
92 'PATCH',
93 'POST',
94 'PUT',
95}
96
97KNOWN_HTTP_PATHS = {
98 'accounts':
99 re.compile(r'(/a)?/accounts/.*'),
100 'changes':
101 re.compile(r'(/a)?/changes/([^/]+)?$'),
102 'changes/abandon':
103 re.compile(r'(/a)?/changes/.*/abandon'),
104 'changes/comments':
105 re.compile(r'(/a)?/changes/.*/comments'),
106 'changes/detail':
107 re.compile(r'(/a)?/changes/.*/detail'),
108 'changes/edit':
109 re.compile(r'(/a)?/changes/.*/edit'),
110 'changes/message':
111 re.compile(r'(/a)?/changes/.*/message'),
112 'changes/restore':
113 re.compile(r'(/a)?/changes/.*/restore'),
114 'changes/reviewers':
115 re.compile(r'(/a)?/changes/.*/reviewers/.*'),
116 'changes/revisions/commit':
117 re.compile(r'(/a)?/changes/.*/revisions/.*/commit'),
118 'changes/revisions/review':
119 re.compile(r'(/a)?/changes/.*/revisions/.*/review'),
120 'changes/submit':
121 re.compile(r'(/a)?/changes/.*/submit'),
122 'projects/branches':
123 re.compile(r'(/a)?/projects/.*/branches/.*'),
124}
125
126KNOWN_HTTP_ARGS = {
127 'ALL_REVISIONS',
128 'CURRENT_COMMIT',
129 'CURRENT_REVISION',
130 'DETAILED_ACCOUNTS',
131 'LABELS',
132}
133
Edward Lemur861640f2018-10-31 19:45:31 +0000134GIT_VERSION_RE = re.compile(
135 r'git version (\d)\.(\d{0,2})\.(\d{0,2})'
136)
137
Edward Lemurfec80c42018-11-01 23:14:14 +0000138KNOWN_SUBCOMMAND_ARGS = {
139 'cc',
140 'hashtag',
141 'l=Auto-Submit+1',
Edward Lemur687ca902018-12-05 02:30:30 +0000142 'l=Code-Review+1',
143 'l=Code-Review+2',
Edward Lemurfec80c42018-11-01 23:14:14 +0000144 'l=Commit-Queue+1',
145 'l=Commit-Queue+2',
146 'label',
147 'm',
148 'notify=ALL',
149 'notify=NONE',
150 'private',
151 'r',
152 'ready',
153 'topic',
154 'wip'
155}
156
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000157
158def get_python_version():
159 """Return the python version in the major.minor.micro format."""
160 return '{v.major}.{v.minor}.{v.micro}'.format(v=sys.version_info)
161
162
Edward Lemur861640f2018-10-31 19:45:31 +0000163def get_git_version():
164 """Return the Git version in the major.minor.micro format."""
165 p = subprocess2.Popen(
166 ['git', '--version'],
167 stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
168 stdout, _ = p.communicate()
169 match = GIT_VERSION_RE.match(stdout)
170 if not match:
171 return None
172 return '%s.%s.%s' % match.groups()
173
174
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000175def return_code_from_exception(exception):
176 """Returns the exit code that would result of raising the exception."""
177 if exception is None:
178 return 0
179 if isinstance(exception[1], SystemExit):
180 return exception[1].code
181 return 1
182
183
184def seconds_to_weeks(duration):
185 """Transform a |duration| from seconds to weeks approximately.
186
187 Drops the lowest 19 bits of the integer representation, which ammounts to
188 about 6 days.
189 """
190 return int(duration) >> 19
191
192
Edward Lemurfec80c42018-11-01 23:14:14 +0000193def extract_known_subcommand_args(args):
194 """Extract the known arguments from the passed list of args."""
195 known_args = []
196 for arg in args:
197 if arg in KNOWN_SUBCOMMAND_ARGS:
198 known_args.append(arg)
199 else:
200 arg = arg.split('=')[0]
201 if arg in KNOWN_SUBCOMMAND_ARGS:
202 known_args.append(arg)
Edward Lemur01f4a4f2018-11-03 00:40:38 +0000203 return sorted(known_args)
Edward Lemurfec80c42018-11-01 23:14:14 +0000204
205
Edward Lemur03d6d112018-10-23 15:17:36 +0000206def extract_http_metrics(request_uri, method, status, response_time):
207 """Extract metrics from the request URI.
208
209 Extracts the host, path, and arguments from the request URI, and returns them
210 along with the method, status and response time.
211
212 The host, method, path and arguments must be in the KNOWN_HTTP_* constants
213 defined above.
214
215 Arguments are the values of the o= url parameter. In Gerrit, additional fields
216 can be obtained by adding o parameters, each option requires more database
217 lookups and slows down the query response time to the client, so we make an
218 effort to collect them.
219
220 The regex defined in KNOWN_HTTP_PATH_RES are checked against the path, and
221 those that match will be returned.
222 """
223 http_metrics = {
224 'status': status,
225 'response_time': response_time,
226 }
227
228 if method in KNOWN_HTTP_METHODS:
229 http_metrics['method'] = method
230
231 parsed_url = urlparse.urlparse(request_uri)
232
233 if parsed_url.netloc in KNOWN_HTTP_HOSTS:
234 http_metrics['host'] = parsed_url.netloc
235
236 for name, path_re in KNOWN_HTTP_PATHS.iteritems():
237 if path_re.match(parsed_url.path):
238 http_metrics['path'] = name
239 break
240
241 parsed_query = urlparse.parse_qs(parsed_url.query)
242
243 # Collect o-parameters from the request.
244 args = [
245 arg for arg in parsed_query.get('o', [])
246 if arg in KNOWN_HTTP_ARGS
247 ]
248 if args:
249 http_metrics['arguments'] = args
250
251 return http_metrics
252
253
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000254def get_repo_timestamp(path_to_repo):
255 """Get an approximate timestamp for the upstream of |path_to_repo|.
256
257 Returns the top two bits of the timestamp of the HEAD for the upstream of the
258 branch path_to_repo is checked out at.
259 """
260 # Get the upstream for the current branch. If we're not in a branch, fallback
261 # to HEAD.
262 try:
Edward Lemur36974ad2019-02-21 23:29:47 +0000263 upstream = scm.GIT.GetUpstreamBranch(path_to_repo) or 'HEAD'
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000264 except subprocess2.CalledProcessError:
265 upstream = 'HEAD'
266
267 # Get the timestamp of the HEAD for the upstream of the current branch.
268 p = subprocess2.Popen(
269 ['git', '-C', path_to_repo, 'log', '-n1', upstream, '--format=%at'],
270 stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
271 stdout, _ = p.communicate()
272
273 # If there was an error, give up.
274 if p.returncode != 0:
275 return None
276
277 # Get the age of the checkout in weeks.
278 return seconds_to_weeks(stdout.strip())
279
Samuel Huang98a7e802019-02-12 15:32:22 +0000280def print_red_boxed_text(out, min_width, lines):
281 colorama.init()
282 if sys.platform == 'win32':
283 [EW, NS, SE, SW, NE, NW] = list('=|++++')
284 else:
285 [EW, NS, SE, SW, NE, NW] = list(u'\u2501\u2503\u250F\u2513\u2517\u251B')
286 out(colorama.Fore.RED + colorama.Style.BRIGHT)
287 width = max(min_width, max(len(line) for line in lines))
288 out(SE + EW * (width + 2) + SW + '\n')
289 for line in lines:
290 out('%s %-*s %s\n' % (NS, width, line, NS))
291 out(NE + EW * (width + 2) + NW + '\n')
292 out(colorama.Style.RESET_ALL)
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000293
294def print_notice(countdown):
295 """Print a notice to let the user know the status of metrics collection."""
Samuel Huang98a7e802019-02-12 15:32:22 +0000296 lines = list(get_notice_countdown_header(countdown))
297 lines.append('')
298 lines += list(get_notice_footer())
299 print_red_boxed_text(sys.stderr.write, 49, lines)
Edward Lemur48836262018-10-18 02:08:06 +0000300
301def print_version_change(config_version):
302 """Print a notice to let the user know we are collecting more metrics."""
Samuel Huang98a7e802019-02-12 15:32:22 +0000303 lines = list(get_notice_version_change_header())
304 for version in xrange(config_version + 1, CURRENT_VERSION + 1):
305 lines.append('')
306 lines += list(get_change_notice(version))
307 print_red_boxed_text(sys.stderr.write, 49, lines)