blob: 920934486d47fd3a6bf5f56cb40dd1b59d9b40ff [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 Lemur32e3d1e2018-07-12 00:54:05 +0000141
142def get_python_version():
143 """Return the python version in the major.minor.micro format."""
144 return '{v.major}.{v.minor}.{v.micro}'.format(v=sys.version_info)
145
146
147def return_code_from_exception(exception):
148 """Returns the exit code that would result of raising the exception."""
149 if exception is None:
150 return 0
151 if isinstance(exception[1], SystemExit):
152 return exception[1].code
153 return 1
154
155
156def seconds_to_weeks(duration):
157 """Transform a |duration| from seconds to weeks approximately.
158
159 Drops the lowest 19 bits of the integer representation, which ammounts to
160 about 6 days.
161 """
162 return int(duration) >> 19
163
164
Edward Lemur03d6d112018-10-23 15:17:36 +0000165def extract_http_metrics(request_uri, method, status, response_time):
166 """Extract metrics from the request URI.
167
168 Extracts the host, path, and arguments from the request URI, and returns them
169 along with the method, status and response time.
170
171 The host, method, path and arguments must be in the KNOWN_HTTP_* constants
172 defined above.
173
174 Arguments are the values of the o= url parameter. In Gerrit, additional fields
175 can be obtained by adding o parameters, each option requires more database
176 lookups and slows down the query response time to the client, so we make an
177 effort to collect them.
178
179 The regex defined in KNOWN_HTTP_PATH_RES are checked against the path, and
180 those that match will be returned.
181 """
182 http_metrics = {
183 'status': status,
184 'response_time': response_time,
185 }
186
187 if method in KNOWN_HTTP_METHODS:
188 http_metrics['method'] = method
189
190 parsed_url = urlparse.urlparse(request_uri)
191
192 if parsed_url.netloc in KNOWN_HTTP_HOSTS:
193 http_metrics['host'] = parsed_url.netloc
194
195 for name, path_re in KNOWN_HTTP_PATHS.iteritems():
196 if path_re.match(parsed_url.path):
197 http_metrics['path'] = name
198 break
199
200 parsed_query = urlparse.parse_qs(parsed_url.query)
201
202 # Collect o-parameters from the request.
203 args = [
204 arg for arg in parsed_query.get('o', [])
205 if arg in KNOWN_HTTP_ARGS
206 ]
207 if args:
208 http_metrics['arguments'] = args
209
210 return http_metrics
211
212
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000213def get_repo_timestamp(path_to_repo):
214 """Get an approximate timestamp for the upstream of |path_to_repo|.
215
216 Returns the top two bits of the timestamp of the HEAD for the upstream of the
217 branch path_to_repo is checked out at.
218 """
219 # Get the upstream for the current branch. If we're not in a branch, fallback
220 # to HEAD.
221 try:
222 upstream = scm.GIT.GetUpstreamBranch(path_to_repo)
223 except subprocess2.CalledProcessError:
224 upstream = 'HEAD'
225
226 # Get the timestamp of the HEAD for the upstream of the current branch.
227 p = subprocess2.Popen(
228 ['git', '-C', path_to_repo, 'log', '-n1', upstream, '--format=%at'],
229 stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
230 stdout, _ = p.communicate()
231
232 # If there was an error, give up.
233 if p.returncode != 0:
234 return None
235
236 # Get the age of the checkout in weeks.
237 return seconds_to_weeks(stdout.strip())
238
239
240def print_notice(countdown):
241 """Print a notice to let the user know the status of metrics collection."""
242 colorama.init()
Edward Lemur48836262018-10-18 02:08:06 +0000243 print(colorama.Fore.RED + '\033[1m', file=sys.stderr, end='')
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000244 if countdown:
Edward Lemurc87d45b2018-07-26 17:43:11 +0000245 print(NOTICE_COUNTDOWN_HEADER % countdown, file=sys.stderr)
Edward Lemur32e3d1e2018-07-12 00:54:05 +0000246 else:
Edward Lemurc87d45b2018-07-26 17:43:11 +0000247 print(NOTICE_COLLECTION_HEADER, file=sys.stderr)
Edward Lemur48836262018-10-18 02:08:06 +0000248 print(EMPTY_LINE, file=sys.stderr)
Edward Lemurc87d45b2018-07-26 17:43:11 +0000249 print(NOTICE_FOOTER + colorama.Style.RESET_ALL, file=sys.stderr)
Edward Lemur48836262018-10-18 02:08:06 +0000250
251
252def print_version_change(config_version):
253 """Print a notice to let the user know we are collecting more metrics."""
254 colorama.init()
255 print(colorama.Fore.RED + '\033[1m', file=sys.stderr, end='')
256 print(NOTICE_VERSION_CHANGE_HEADER, file=sys.stderr)
257 print(EMPTY_LINE, file=sys.stderr)
258 for version in range(config_version + 1, CURRENT_VERSION + 1):
259 print(CHANGE_NOTICE[version], file=sys.stderr)
260 print(EMPTY_LINE, file=sys.stderr)