blob: 931cb38e03b8fe7bf87cae535d99e2c92c26e311 [file] [log] [blame]
Josip Sokcevic4de5dea2022-03-23 21:15:14 +00001#!/usr/bin/env python3
Edward Lemur32e3d1e2018-07-12 00:54:05 +00002# 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 Lesmes1e59a242021-04-30 18:38:25 +00009import os
Josip Sokcevic7958e302023-03-01 23:02:21 +000010import scm
Edward Lemur32e3d1e2018-07-12 00:54:05 +000011import subprocess2
12import sys
Gavin Mak5f955df2023-08-30 15:39:13 +000013import urllib.parse
Edward Lemur32e3d1e2018-07-12 00:54:05 +000014
Edward Lemur32e3d1e2018-07-12 00:54:05 +000015
Edward Lemur48836262018-10-18 02:08:06 +000016# Current version of metrics recording.
17# When we add new metrics, the version number will be increased, we display the
18# user what has changed, and ask the user to agree again.
Edward Lesmes1e59a242021-04-30 18:38:25 +000019CURRENT_VERSION = 2
Edward Lemur48836262018-10-18 02:08:06 +000020
Edward Lemur5ba1e9c2018-07-23 18:19:02 +000021APP_URL = 'https://cit-cli-metrics.appspot.com'
22
Edward Lesmes9c349062021-05-06 20:02:39 +000023REPORT_BUILD = os.getenv('DEPOT_TOOLS_REPORT_BUILD')
Edward Lesmes09f358b2021-06-22 22:31:52 +000024COLLECT_METRICS = (
25 os.getenv('DEPOT_TOOLS_COLLECT_METRICS') != '0'
26 and os.getenv('DEPOT_TOOLS_METRICS') != '0')
Edward Lesmes1e59a242021-04-30 18:38:25 +000027
Edward Lesmesc8f63d32021-06-02 23:51:53 +000028SYNC_STATUS_SUCCESS = 'SYNC_STATUS_SUCCESS'
29SYNC_STATUS_FAILURE = 'SYNC_STATUS_FAILURE'
30
Edward Lesmes1e59a242021-04-30 18:38:25 +000031
Samuel Huang98a7e802019-02-12 15:32:22 +000032def get_notice_countdown_header(countdown):
33 if countdown == 0:
34 yield ' METRICS COLLECTION IS TAKING PLACE'
35 else:
36 yield ' METRICS COLLECTION WILL START IN %d EXECUTIONS' % countdown
Edward Lemur32e3d1e2018-07-12 00:54:05 +000037
Samuel Huang98a7e802019-02-12 15:32:22 +000038def get_notice_version_change_header():
39 yield ' WE ARE COLLECTING ADDITIONAL METRICS'
40 yield ''
41 yield ' Please review the changes and opt-in again.'
42
43def get_notice_footer():
44 yield 'To suppress this message opt in or out using:'
45 yield '$ gclient metrics [--opt-in] [--opt-out]'
46 yield 'For more information please see metrics.README.md'
47 yield 'in your depot_tools checkout or visit'
Gavin Mak019e73a2023-04-07 18:46:41 +000048 yield 'https://bit.ly/3MpLAYM.'
Samuel Huang98a7e802019-02-12 15:32:22 +000049
50def get_change_notice(version):
51 if version == 0:
Edward Lesmes1e59a242021-04-30 18:38:25 +000052 return [] # No changes for version 0
Aravind Vasudevanc5f0cbb2022-01-24 23:56:57 +000053
54 if version == 1:
Edward Lesmes1e59a242021-04-30 18:38:25 +000055 return [
56 'We want to collect the Git version.',
57 'We want to collect information about the HTTP',
58 'requests that depot_tools makes, and the git and',
59 'cipd commands it executes.',
60 '',
61 'We only collect known strings to make sure we',
62 'don\'t record PII.',
63 ]
Aravind Vasudevanc5f0cbb2022-01-24 23:56:57 +000064
65 if version == 2:
Edward Lesmes1e59a242021-04-30 18:38:25 +000066 return [
67 'We will start collecting metrics from bots.',
68 'There are no changes for developers.',
69 'If the DEPOT_TOOLS_REPORT_BUILD environment variable is set,',
70 'we will report information about the current build',
71 '(e.g. buildbucket project, bucket, builder and build id),',
72 'and authenticate to the metrics collection server.',
73 'This information will only be recorded for requests',
74 'authenticated as bot service accounts.',
75 ]
Edward Lemur48836262018-10-18 02:08:06 +000076
77
Edward Lemur40764b02018-07-20 18:50:29 +000078KNOWN_PROJECT_URLS = {
79 'https://chrome-internal.googlesource.com/chrome/ios_internal',
80 'https://chrome-internal.googlesource.com/infra/infra_internal',
81 'https://chromium.googlesource.com/breakpad/breakpad',
82 'https://chromium.googlesource.com/chromium/src',
83 'https://chromium.googlesource.com/chromium/tools/depot_tools',
84 'https://chromium.googlesource.com/crashpad/crashpad',
85 'https://chromium.googlesource.com/external/gyp',
86 'https://chromium.googlesource.com/external/naclports',
87 'https://chromium.googlesource.com/infra/goma/client',
88 'https://chromium.googlesource.com/infra/infra',
89 'https://chromium.googlesource.com/native_client/',
90 'https://chromium.googlesource.com/syzygy',
91 'https://chromium.googlesource.com/v8/v8',
92 'https://dart.googlesource.com/sdk',
93 'https://pdfium.googlesource.com/pdfium',
94 'https://skia.googlesource.com/buildbot',
95 'https://skia.googlesource.com/skia',
96 'https://webrtc.googlesource.com/src',
97}
98
Edward Lemur03d6d112018-10-23 15:17:36 +000099KNOWN_HTTP_HOSTS = {
100 'chrome-internal-review.googlesource.com',
101 'chromium-review.googlesource.com',
102 'dart-review.googlesource.com',
103 'eu1-mirror-chromium-review.googlesource.com',
104 'pdfium-review.googlesource.com',
105 'skia-review.googlesource.com',
106 'us1-mirror-chromium-review.googlesource.com',
107 'us2-mirror-chromium-review.googlesource.com',
108 'us3-mirror-chromium-review.googlesource.com',
109 'webrtc-review.googlesource.com',
110}
111
112KNOWN_HTTP_METHODS = {
113 'DELETE',
114 'GET',
115 'PATCH',
116 'POST',
117 'PUT',
118}
119
120KNOWN_HTTP_PATHS = {
121 'accounts':
122 re.compile(r'(/a)?/accounts/.*'),
123 'changes':
124 re.compile(r'(/a)?/changes/([^/]+)?$'),
125 'changes/abandon':
126 re.compile(r'(/a)?/changes/.*/abandon'),
127 'changes/comments':
128 re.compile(r'(/a)?/changes/.*/comments'),
129 'changes/detail':
130 re.compile(r'(/a)?/changes/.*/detail'),
131 'changes/edit':
132 re.compile(r'(/a)?/changes/.*/edit'),
133 'changes/message':
134 re.compile(r'(/a)?/changes/.*/message'),
135 'changes/restore':
136 re.compile(r'(/a)?/changes/.*/restore'),
137 'changes/reviewers':
138 re.compile(r'(/a)?/changes/.*/reviewers/.*'),
139 'changes/revisions/commit':
140 re.compile(r'(/a)?/changes/.*/revisions/.*/commit'),
141 'changes/revisions/review':
142 re.compile(r'(/a)?/changes/.*/revisions/.*/review'),
143 'changes/submit':
144 re.compile(r'(/a)?/changes/.*/submit'),
145 'projects/branches':
146 re.compile(r'(/a)?/projects/.*/branches/.*'),
147}
148
149KNOWN_HTTP_ARGS = {
150 'ALL_REVISIONS',
151 'CURRENT_COMMIT',
152 'CURRENT_REVISION',
153 'DETAILED_ACCOUNTS',
154 'LABELS',
155}
156
Edward Lemur861640f2018-10-31 19:45:31 +0000157GIT_VERSION_RE = re.compile(
158 r'git version (\d)\.(\d{0,2})\.(\d{0,2})'
159)
160
Edward Lemurfec80c42018-11-01 23:14:14 +0000161KNOWN_SUBCOMMAND_ARGS = {
162 'cc',
163 'hashtag',
164 'l=Auto-Submit+1',
Edward Lemur687ca902018-12-05 02:30:30 +0000165 'l=Code-Review+1',
166 'l=Code-Review+2',
Edward Lemurfec80c42018-11-01 23:14:14 +0000167 'l=Commit-Queue+1',
168 'l=Commit-Queue+2',
169 'label',
170 'm',
171 'notify=ALL',
172 'notify=NONE',
173 'private',
174 'r',
175 'ready',
176 'topic',
177 'wip'
178}
179
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000180
181def get_python_version():
182 """Return the python version in the major.minor.micro format."""
183 return '{v.major}.{v.minor}.{v.micro}'.format(v=sys.version_info)
184
185
Edward Lemur861640f2018-10-31 19:45:31 +0000186def get_git_version():
187 """Return the Git version in the major.minor.micro format."""
188 p = subprocess2.Popen(
189 ['git', '--version'],
190 stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
191 stdout, _ = p.communicate()
Edward Lemur73065b22019-07-22 20:12:01 +0000192 match = GIT_VERSION_RE.match(stdout.decode('utf-8'))
Edward Lemur861640f2018-10-31 19:45:31 +0000193 if not match:
194 return None
195 return '%s.%s.%s' % match.groups()
196
197
Edward Lesmes1e59a242021-04-30 18:38:25 +0000198def get_bot_metrics():
Edward Lesmes9c349062021-05-06 20:02:39 +0000199 try:
200 project, bucket, builder, build = REPORT_BUILD.split('/')
201 return {
202 'build_id': int(build),
203 'builder': {
204 'project': project,
205 'bucket': bucket,
206 'builder': builder,
207 },
208 }
209 except (AttributeError, ValueError):
Edward Lesmes1e59a242021-04-30 18:38:25 +0000210 return None
Edward Lesmes1e59a242021-04-30 18:38:25 +0000211
212
213
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000214def return_code_from_exception(exception):
215 """Returns the exit code that would result of raising the exception."""
216 if exception is None:
217 return 0
Gavin Mak5b2255d2023-07-14 18:22:24 +0000218 e = exception[1]
219 if isinstance(e, KeyboardInterrupt):
220 return 130
221 if isinstance(e, SystemExit):
222 return e.code
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000223 return 1
224
225
Edward Lemurfec80c42018-11-01 23:14:14 +0000226def extract_known_subcommand_args(args):
227 """Extract the known arguments from the passed list of args."""
228 known_args = []
229 for arg in args:
230 if arg in KNOWN_SUBCOMMAND_ARGS:
231 known_args.append(arg)
232 else:
233 arg = arg.split('=')[0]
234 if arg in KNOWN_SUBCOMMAND_ARGS:
235 known_args.append(arg)
Edward Lemur01f4a4f2018-11-03 00:40:38 +0000236 return sorted(known_args)
Edward Lemurfec80c42018-11-01 23:14:14 +0000237
238
Edward Lemur03d6d112018-10-23 15:17:36 +0000239def extract_http_metrics(request_uri, method, status, response_time):
240 """Extract metrics from the request URI.
241
242 Extracts the host, path, and arguments from the request URI, and returns them
243 along with the method, status and response time.
244
245 The host, method, path and arguments must be in the KNOWN_HTTP_* constants
246 defined above.
247
248 Arguments are the values of the o= url parameter. In Gerrit, additional fields
249 can be obtained by adding o parameters, each option requires more database
250 lookups and slows down the query response time to the client, so we make an
251 effort to collect them.
252
253 The regex defined in KNOWN_HTTP_PATH_RES are checked against the path, and
254 those that match will be returned.
255 """
256 http_metrics = {
257 'status': status,
258 'response_time': response_time,
259 }
260
261 if method in KNOWN_HTTP_METHODS:
262 http_metrics['method'] = method
263
Gavin Mak5f955df2023-08-30 15:39:13 +0000264 parsed_url = urllib.parse.urlparse(request_uri)
Edward Lemur03d6d112018-10-23 15:17:36 +0000265
266 if parsed_url.netloc in KNOWN_HTTP_HOSTS:
267 http_metrics['host'] = parsed_url.netloc
268
Edward Lemur9217ff82019-07-16 23:14:24 +0000269 for name, path_re in KNOWN_HTTP_PATHS.items():
Edward Lemur03d6d112018-10-23 15:17:36 +0000270 if path_re.match(parsed_url.path):
271 http_metrics['path'] = name
272 break
273
Gavin Mak5f955df2023-08-30 15:39:13 +0000274 parsed_query = urllib.parse.parse_qs(parsed_url.query)
Edward Lemur03d6d112018-10-23 15:17:36 +0000275
276 # Collect o-parameters from the request.
277 args = [
278 arg for arg in parsed_query.get('o', [])
279 if arg in KNOWN_HTTP_ARGS
280 ]
281 if args:
282 http_metrics['arguments'] = args
283
284 return http_metrics
285
286
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000287def get_repo_timestamp(path_to_repo):
288 """Get an approximate timestamp for the upstream of |path_to_repo|.
289
290 Returns the top two bits of the timestamp of the HEAD for the upstream of the
291 branch path_to_repo is checked out at.
292 """
293 # Get the upstream for the current branch. If we're not in a branch, fallback
294 # to HEAD.
295 try:
Edward Lemur36974ad2019-02-21 23:29:47 +0000296 upstream = scm.GIT.GetUpstreamBranch(path_to_repo) or 'HEAD'
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000297 except subprocess2.CalledProcessError:
298 upstream = 'HEAD'
299
300 # Get the timestamp of the HEAD for the upstream of the current branch.
301 p = subprocess2.Popen(
302 ['git', '-C', path_to_repo, 'log', '-n1', upstream, '--format=%at'],
303 stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
304 stdout, _ = p.communicate()
305
306 # If there was an error, give up.
307 if p.returncode != 0:
308 return None
309
Edward Lemur18df41e2019-04-26 00:42:04 +0000310 return stdout.strip()
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000311
Hans Wennborg24b5f902019-03-15 18:51:27 +0000312def print_boxed_text(out, min_width, lines):
313 [EW, NS, SE, SW, NE, NW] = list('=|++++')
Samuel Huang98a7e802019-02-12 15:32:22 +0000314 width = max(min_width, max(len(line) for line in lines))
315 out(SE + EW * (width + 2) + SW + '\n')
316 for line in lines:
Raul Tambreb946b232019-03-26 14:48:46 +0000317 out('%s %-*s %s\n' % (NS, width, line, NS))
Samuel Huang98a7e802019-02-12 15:32:22 +0000318 out(NE + EW * (width + 2) + NW + '\n')
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000319
320def print_notice(countdown):
321 """Print a notice to let the user know the status of metrics collection."""
Samuel Huang98a7e802019-02-12 15:32:22 +0000322 lines = list(get_notice_countdown_header(countdown))
323 lines.append('')
324 lines += list(get_notice_footer())
Hans Wennborg24b5f902019-03-15 18:51:27 +0000325 print_boxed_text(sys.stderr.write, 49, lines)
Edward Lemur48836262018-10-18 02:08:06 +0000326
327def print_version_change(config_version):
328 """Print a notice to let the user know we are collecting more metrics."""
Samuel Huang98a7e802019-02-12 15:32:22 +0000329 lines = list(get_notice_version_change_header())
Edward Lemur9217ff82019-07-16 23:14:24 +0000330 for version in range(config_version + 1, CURRENT_VERSION + 1):
Samuel Huang98a7e802019-02-12 15:32:22 +0000331 lines.append('')
Edward Lesmes1e59a242021-04-30 18:38:25 +0000332 lines += get_change_notice(version)
Hans Wennborg24b5f902019-03-15 18:51:27 +0000333 print_boxed_text(sys.stderr.write, 49, lines)