blob: 5a7c77e80e792c9b0ef59f97b5ad0d64aeb2bdaf [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 Lesmes1e59a242021-04-30 18:38:25 +00009import os
Edward Lemur32e3d1e2018-07-12 00:54:05 +000010import scm
11import subprocess2
12import sys
Raul Tambreb946b232019-03-26 14:48:46 +000013
14try:
15 import urlparse
16except ImportError: # For Py3 compatibility
17 import urllib.parse as urlparse
Edward Lemur32e3d1e2018-07-12 00:54:05 +000018
Edward Lemur32e3d1e2018-07-12 00:54:05 +000019
Edward Lemur48836262018-10-18 02:08:06 +000020# Current version of metrics recording.
21# When we add new metrics, the version number will be increased, we display the
22# user what has changed, and ask the user to agree again.
Edward Lesmes1e59a242021-04-30 18:38:25 +000023CURRENT_VERSION = 2
Edward Lemur48836262018-10-18 02:08:06 +000024
Edward Lemur5ba1e9c2018-07-23 18:19:02 +000025APP_URL = 'https://cit-cli-metrics.appspot.com'
26
Edward Lesmes9c349062021-05-06 20:02:39 +000027REPORT_BUILD = os.getenv('DEPOT_TOOLS_REPORT_BUILD')
28COLLECT_METRICS = os.getenv('DEPOT_TOOLS_COLLECT_METRICS') != '0'
Edward Lesmes1e59a242021-04-30 18:38:25 +000029
30
Samuel Huang98a7e802019-02-12 15:32:22 +000031def get_notice_countdown_header(countdown):
32 if countdown == 0:
33 yield ' METRICS COLLECTION IS TAKING PLACE'
34 else:
35 yield ' METRICS COLLECTION WILL START IN %d EXECUTIONS' % countdown
Edward Lemur32e3d1e2018-07-12 00:54:05 +000036
Samuel Huang98a7e802019-02-12 15:32:22 +000037def get_notice_version_change_header():
38 yield ' WE ARE COLLECTING ADDITIONAL METRICS'
39 yield ''
40 yield ' Please review the changes and opt-in again.'
41
42def get_notice_footer():
43 yield 'To suppress this message opt in or out using:'
44 yield '$ gclient metrics [--opt-in] [--opt-out]'
45 yield 'For more information please see metrics.README.md'
46 yield 'in your depot_tools checkout or visit'
47 yield 'https://goo.gl/yNpRDV.'
48
49def get_change_notice(version):
50 if version == 0:
Edward Lesmes1e59a242021-04-30 18:38:25 +000051 return [] # No changes for version 0
Samuel Huang98a7e802019-02-12 15:32:22 +000052 elif version == 1:
Edward Lesmes1e59a242021-04-30 18:38:25 +000053 return [
54 'We want to collect the Git version.',
55 'We want to collect information about the HTTP',
56 'requests that depot_tools makes, and the git and',
57 'cipd commands it executes.',
58 '',
59 'We only collect known strings to make sure we',
60 'don\'t record PII.',
61 ]
62 elif version == 2:
63 return [
64 'We will start collecting metrics from bots.',
65 'There are no changes for developers.',
66 'If the DEPOT_TOOLS_REPORT_BUILD environment variable is set,',
67 'we will report information about the current build',
68 '(e.g. buildbucket project, bucket, builder and build id),',
69 'and authenticate to the metrics collection server.',
70 'This information will only be recorded for requests',
71 'authenticated as bot service accounts.',
72 ]
Edward Lemur48836262018-10-18 02:08:06 +000073
74
Edward Lemur40764b02018-07-20 18:50:29 +000075KNOWN_PROJECT_URLS = {
76 'https://chrome-internal.googlesource.com/chrome/ios_internal',
77 'https://chrome-internal.googlesource.com/infra/infra_internal',
78 'https://chromium.googlesource.com/breakpad/breakpad',
79 'https://chromium.googlesource.com/chromium/src',
80 'https://chromium.googlesource.com/chromium/tools/depot_tools',
81 'https://chromium.googlesource.com/crashpad/crashpad',
82 'https://chromium.googlesource.com/external/gyp',
83 'https://chromium.googlesource.com/external/naclports',
84 'https://chromium.googlesource.com/infra/goma/client',
85 'https://chromium.googlesource.com/infra/infra',
86 'https://chromium.googlesource.com/native_client/',
87 'https://chromium.googlesource.com/syzygy',
88 'https://chromium.googlesource.com/v8/v8',
89 'https://dart.googlesource.com/sdk',
90 'https://pdfium.googlesource.com/pdfium',
91 'https://skia.googlesource.com/buildbot',
92 'https://skia.googlesource.com/skia',
93 'https://webrtc.googlesource.com/src',
94}
95
Edward Lemur03d6d112018-10-23 15:17:36 +000096KNOWN_HTTP_HOSTS = {
97 'chrome-internal-review.googlesource.com',
98 'chromium-review.googlesource.com',
99 'dart-review.googlesource.com',
100 'eu1-mirror-chromium-review.googlesource.com',
101 'pdfium-review.googlesource.com',
102 'skia-review.googlesource.com',
103 'us1-mirror-chromium-review.googlesource.com',
104 'us2-mirror-chromium-review.googlesource.com',
105 'us3-mirror-chromium-review.googlesource.com',
106 'webrtc-review.googlesource.com',
107}
108
109KNOWN_HTTP_METHODS = {
110 'DELETE',
111 'GET',
112 'PATCH',
113 'POST',
114 'PUT',
115}
116
117KNOWN_HTTP_PATHS = {
118 'accounts':
119 re.compile(r'(/a)?/accounts/.*'),
120 'changes':
121 re.compile(r'(/a)?/changes/([^/]+)?$'),
122 'changes/abandon':
123 re.compile(r'(/a)?/changes/.*/abandon'),
124 'changes/comments':
125 re.compile(r'(/a)?/changes/.*/comments'),
126 'changes/detail':
127 re.compile(r'(/a)?/changes/.*/detail'),
128 'changes/edit':
129 re.compile(r'(/a)?/changes/.*/edit'),
130 'changes/message':
131 re.compile(r'(/a)?/changes/.*/message'),
132 'changes/restore':
133 re.compile(r'(/a)?/changes/.*/restore'),
134 'changes/reviewers':
135 re.compile(r'(/a)?/changes/.*/reviewers/.*'),
136 'changes/revisions/commit':
137 re.compile(r'(/a)?/changes/.*/revisions/.*/commit'),
138 'changes/revisions/review':
139 re.compile(r'(/a)?/changes/.*/revisions/.*/review'),
140 'changes/submit':
141 re.compile(r'(/a)?/changes/.*/submit'),
142 'projects/branches':
143 re.compile(r'(/a)?/projects/.*/branches/.*'),
144}
145
146KNOWN_HTTP_ARGS = {
147 'ALL_REVISIONS',
148 'CURRENT_COMMIT',
149 'CURRENT_REVISION',
150 'DETAILED_ACCOUNTS',
151 'LABELS',
152}
153
Edward Lemur861640f2018-10-31 19:45:31 +0000154GIT_VERSION_RE = re.compile(
155 r'git version (\d)\.(\d{0,2})\.(\d{0,2})'
156)
157
Edward Lemurfec80c42018-11-01 23:14:14 +0000158KNOWN_SUBCOMMAND_ARGS = {
159 'cc',
160 'hashtag',
161 'l=Auto-Submit+1',
Edward Lemur687ca902018-12-05 02:30:30 +0000162 'l=Code-Review+1',
163 'l=Code-Review+2',
Edward Lemurfec80c42018-11-01 23:14:14 +0000164 'l=Commit-Queue+1',
165 'l=Commit-Queue+2',
166 'label',
167 'm',
168 'notify=ALL',
169 'notify=NONE',
170 'private',
171 'r',
172 'ready',
173 'topic',
174 'wip'
175}
176
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000177
178def get_python_version():
179 """Return the python version in the major.minor.micro format."""
180 return '{v.major}.{v.minor}.{v.micro}'.format(v=sys.version_info)
181
182
Edward Lemur861640f2018-10-31 19:45:31 +0000183def get_git_version():
184 """Return the Git version in the major.minor.micro format."""
185 p = subprocess2.Popen(
186 ['git', '--version'],
187 stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
188 stdout, _ = p.communicate()
Edward Lemur73065b22019-07-22 20:12:01 +0000189 match = GIT_VERSION_RE.match(stdout.decode('utf-8'))
Edward Lemur861640f2018-10-31 19:45:31 +0000190 if not match:
191 return None
192 return '%s.%s.%s' % match.groups()
193
194
Edward Lesmes1e59a242021-04-30 18:38:25 +0000195def get_bot_metrics():
Edward Lesmes9c349062021-05-06 20:02:39 +0000196 try:
197 project, bucket, builder, build = REPORT_BUILD.split('/')
198 return {
199 'build_id': int(build),
200 'builder': {
201 'project': project,
202 'bucket': bucket,
203 'builder': builder,
204 },
205 }
206 except (AttributeError, ValueError):
Edward Lesmes1e59a242021-04-30 18:38:25 +0000207 return None
Edward Lesmes1e59a242021-04-30 18:38:25 +0000208
209
210
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000211def return_code_from_exception(exception):
212 """Returns the exit code that would result of raising the exception."""
213 if exception is None:
214 return 0
215 if isinstance(exception[1], SystemExit):
216 return exception[1].code
217 return 1
218
219
Edward Lemurfec80c42018-11-01 23:14:14 +0000220def extract_known_subcommand_args(args):
221 """Extract the known arguments from the passed list of args."""
222 known_args = []
223 for arg in args:
224 if arg in KNOWN_SUBCOMMAND_ARGS:
225 known_args.append(arg)
226 else:
227 arg = arg.split('=')[0]
228 if arg in KNOWN_SUBCOMMAND_ARGS:
229 known_args.append(arg)
Edward Lemur01f4a4f2018-11-03 00:40:38 +0000230 return sorted(known_args)
Edward Lemurfec80c42018-11-01 23:14:14 +0000231
232
Edward Lemur03d6d112018-10-23 15:17:36 +0000233def extract_http_metrics(request_uri, method, status, response_time):
234 """Extract metrics from the request URI.
235
236 Extracts the host, path, and arguments from the request URI, and returns them
237 along with the method, status and response time.
238
239 The host, method, path and arguments must be in the KNOWN_HTTP_* constants
240 defined above.
241
242 Arguments are the values of the o= url parameter. In Gerrit, additional fields
243 can be obtained by adding o parameters, each option requires more database
244 lookups and slows down the query response time to the client, so we make an
245 effort to collect them.
246
247 The regex defined in KNOWN_HTTP_PATH_RES are checked against the path, and
248 those that match will be returned.
249 """
250 http_metrics = {
251 'status': status,
252 'response_time': response_time,
253 }
254
255 if method in KNOWN_HTTP_METHODS:
256 http_metrics['method'] = method
257
258 parsed_url = urlparse.urlparse(request_uri)
259
260 if parsed_url.netloc in KNOWN_HTTP_HOSTS:
261 http_metrics['host'] = parsed_url.netloc
262
Edward Lemur9217ff82019-07-16 23:14:24 +0000263 for name, path_re in KNOWN_HTTP_PATHS.items():
Edward Lemur03d6d112018-10-23 15:17:36 +0000264 if path_re.match(parsed_url.path):
265 http_metrics['path'] = name
266 break
267
268 parsed_query = urlparse.parse_qs(parsed_url.query)
269
270 # Collect o-parameters from the request.
271 args = [
272 arg for arg in parsed_query.get('o', [])
273 if arg in KNOWN_HTTP_ARGS
274 ]
275 if args:
276 http_metrics['arguments'] = args
277
278 return http_metrics
279
280
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000281def get_repo_timestamp(path_to_repo):
282 """Get an approximate timestamp for the upstream of |path_to_repo|.
283
284 Returns the top two bits of the timestamp of the HEAD for the upstream of the
285 branch path_to_repo is checked out at.
286 """
287 # Get the upstream for the current branch. If we're not in a branch, fallback
288 # to HEAD.
289 try:
Edward Lemur36974ad2019-02-21 23:29:47 +0000290 upstream = scm.GIT.GetUpstreamBranch(path_to_repo) or 'HEAD'
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000291 except subprocess2.CalledProcessError:
292 upstream = 'HEAD'
293
294 # Get the timestamp of the HEAD for the upstream of the current branch.
295 p = subprocess2.Popen(
296 ['git', '-C', path_to_repo, 'log', '-n1', upstream, '--format=%at'],
297 stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
298 stdout, _ = p.communicate()
299
300 # If there was an error, give up.
301 if p.returncode != 0:
302 return None
303
Edward Lemur18df41e2019-04-26 00:42:04 +0000304 return stdout.strip()
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000305
Hans Wennborg24b5f902019-03-15 18:51:27 +0000306def print_boxed_text(out, min_width, lines):
307 [EW, NS, SE, SW, NE, NW] = list('=|++++')
Samuel Huang98a7e802019-02-12 15:32:22 +0000308 width = max(min_width, max(len(line) for line in lines))
309 out(SE + EW * (width + 2) + SW + '\n')
310 for line in lines:
Raul Tambreb946b232019-03-26 14:48:46 +0000311 out('%s %-*s %s\n' % (NS, width, line, NS))
Samuel Huang98a7e802019-02-12 15:32:22 +0000312 out(NE + EW * (width + 2) + NW + '\n')
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000313
314def print_notice(countdown):
315 """Print a notice to let the user know the status of metrics collection."""
Samuel Huang98a7e802019-02-12 15:32:22 +0000316 lines = list(get_notice_countdown_header(countdown))
317 lines.append('')
318 lines += list(get_notice_footer())
Hans Wennborg24b5f902019-03-15 18:51:27 +0000319 print_boxed_text(sys.stderr.write, 49, lines)
Edward Lemur48836262018-10-18 02:08:06 +0000320
321def print_version_change(config_version):
322 """Print a notice to let the user know we are collecting more metrics."""
Samuel Huang98a7e802019-02-12 15:32:22 +0000323 lines = list(get_notice_version_change_header())
Edward Lemur9217ff82019-07-16 23:14:24 +0000324 for version in range(config_version + 1, CURRENT_VERSION + 1):
Samuel Huang98a7e802019-02-12 15:32:22 +0000325 lines.append('')
Edward Lesmes1e59a242021-04-30 18:38:25 +0000326 lines += get_change_notice(version)
Hans Wennborg24b5f902019-03-15 18:51:27 +0000327 print_boxed_text(sys.stderr.write, 49, lines)