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