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