blob: 37b668dae24a3477bdc69f959124a81b03883245 [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',
Edward Lemur687ca902018-12-05 02:30:30 +0000150 'l=Code-Review+1',
151 'l=Code-Review+2',
Edward Lemurfec80c42018-11-01 23:14:14 +0000152 'l=Commit-Queue+1',
153 'l=Commit-Queue+2',
154 'label',
155 'm',
156 'notify=ALL',
157 'notify=NONE',
158 'private',
159 'r',
160 'ready',
161 'topic',
162 'wip'
163}
164
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000165
166def get_python_version():
167 """Return the python version in the major.minor.micro format."""
168 return '{v.major}.{v.minor}.{v.micro}'.format(v=sys.version_info)
169
170
Edward Lemur861640f2018-10-31 19:45:31 +0000171def get_git_version():
172 """Return the Git version in the major.minor.micro format."""
173 p = subprocess2.Popen(
174 ['git', '--version'],
175 stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
176 stdout, _ = p.communicate()
177 match = GIT_VERSION_RE.match(stdout)
178 if not match:
179 return None
180 return '%s.%s.%s' % match.groups()
181
182
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000183def return_code_from_exception(exception):
184 """Returns the exit code that would result of raising the exception."""
185 if exception is None:
186 return 0
187 if isinstance(exception[1], SystemExit):
188 return exception[1].code
189 return 1
190
191
192def seconds_to_weeks(duration):
193 """Transform a |duration| from seconds to weeks approximately.
194
195 Drops the lowest 19 bits of the integer representation, which ammounts to
196 about 6 days.
197 """
198 return int(duration) >> 19
199
200
Edward Lemurfec80c42018-11-01 23:14:14 +0000201def extract_known_subcommand_args(args):
202 """Extract the known arguments from the passed list of args."""
203 known_args = []
204 for arg in args:
205 if arg in KNOWN_SUBCOMMAND_ARGS:
206 known_args.append(arg)
207 else:
208 arg = arg.split('=')[0]
209 if arg in KNOWN_SUBCOMMAND_ARGS:
210 known_args.append(arg)
Edward Lemur01f4a4f2018-11-03 00:40:38 +0000211 return sorted(known_args)
Edward Lemurfec80c42018-11-01 23:14:14 +0000212
213
Edward Lemur03d6d112018-10-23 15:17:36 +0000214def extract_http_metrics(request_uri, method, status, response_time):
215 """Extract metrics from the request URI.
216
217 Extracts the host, path, and arguments from the request URI, and returns them
218 along with the method, status and response time.
219
220 The host, method, path and arguments must be in the KNOWN_HTTP_* constants
221 defined above.
222
223 Arguments are the values of the o= url parameter. In Gerrit, additional fields
224 can be obtained by adding o parameters, each option requires more database
225 lookups and slows down the query response time to the client, so we make an
226 effort to collect them.
227
228 The regex defined in KNOWN_HTTP_PATH_RES are checked against the path, and
229 those that match will be returned.
230 """
231 http_metrics = {
232 'status': status,
233 'response_time': response_time,
234 }
235
236 if method in KNOWN_HTTP_METHODS:
237 http_metrics['method'] = method
238
239 parsed_url = urlparse.urlparse(request_uri)
240
241 if parsed_url.netloc in KNOWN_HTTP_HOSTS:
242 http_metrics['host'] = parsed_url.netloc
243
244 for name, path_re in KNOWN_HTTP_PATHS.iteritems():
245 if path_re.match(parsed_url.path):
246 http_metrics['path'] = name
247 break
248
249 parsed_query = urlparse.parse_qs(parsed_url.query)
250
251 # Collect o-parameters from the request.
252 args = [
253 arg for arg in parsed_query.get('o', [])
254 if arg in KNOWN_HTTP_ARGS
255 ]
256 if args:
257 http_metrics['arguments'] = args
258
259 return http_metrics
260
261
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000262def get_repo_timestamp(path_to_repo):
263 """Get an approximate timestamp for the upstream of |path_to_repo|.
264
265 Returns the top two bits of the timestamp of the HEAD for the upstream of the
266 branch path_to_repo is checked out at.
267 """
268 # Get the upstream for the current branch. If we're not in a branch, fallback
269 # to HEAD.
270 try:
271 upstream = scm.GIT.GetUpstreamBranch(path_to_repo)
272 except subprocess2.CalledProcessError:
273 upstream = 'HEAD'
274
275 # Get the timestamp of the HEAD for the upstream of the current branch.
276 p = subprocess2.Popen(
277 ['git', '-C', path_to_repo, 'log', '-n1', upstream, '--format=%at'],
278 stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
279 stdout, _ = p.communicate()
280
281 # If there was an error, give up.
282 if p.returncode != 0:
283 return None
284
285 # Get the age of the checkout in weeks.
286 return seconds_to_weeks(stdout.strip())
287
288
289def print_notice(countdown):
290 """Print a notice to let the user know the status of metrics collection."""
291 colorama.init()
Edward Lemur48836262018-10-18 02:08:06 +0000292 print(colorama.Fore.RED + '\033[1m', file=sys.stderr, end='')
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000293 if countdown:
Edward Lemurc87d45b2018-07-26 17:43:11 +0000294 print(NOTICE_COUNTDOWN_HEADER % countdown, file=sys.stderr)
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000295 else:
Edward Lemurc87d45b2018-07-26 17:43:11 +0000296 print(NOTICE_COLLECTION_HEADER, file=sys.stderr)
Edward Lemur48836262018-10-18 02:08:06 +0000297 print(EMPTY_LINE, file=sys.stderr)
Edward Lemurc87d45b2018-07-26 17:43:11 +0000298 print(NOTICE_FOOTER + colorama.Style.RESET_ALL, file=sys.stderr)
Edward Lemur48836262018-10-18 02:08:06 +0000299
300
301def print_version_change(config_version):
302 """Print a notice to let the user know we are collecting more metrics."""
303 colorama.init()
304 print(colorama.Fore.RED + '\033[1m', file=sys.stderr, end='')
305 print(NOTICE_VERSION_CHANGE_HEADER, file=sys.stderr)
306 print(EMPTY_LINE, file=sys.stderr)
307 for version in range(config_version + 1, CURRENT_VERSION + 1):
308 print(CHANGE_NOTICE[version], file=sys.stderr)
309 print(EMPTY_LINE, file=sys.stderr)