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