Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 1 | #!/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 Lemur | c87d45b | 2018-07-26 17:43:11 +0000 | [diff] [blame] | 6 | from __future__ import print_function |
| 7 | |
Edward Lemur | 03d6d11 | 2018-10-23 15:17:36 +0000 | [diff] [blame] | 8 | import re |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 9 | import scm |
| 10 | import subprocess2 |
| 11 | import sys |
Edward Lemur | 03d6d11 | 2018-10-23 15:17:36 +0000 | [diff] [blame] | 12 | import urlparse |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 13 | |
| 14 | from third_party import colorama |
| 15 | |
| 16 | |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 +0000 | [diff] [blame] | 17 | # 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 Lemur | 5a9ff43 | 2018-10-30 19:00:22 +0000 | [diff] [blame] | 20 | CURRENT_VERSION = 1 |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 +0000 | [diff] [blame] | 21 | |
Edward Lemur | 5ba1e9c | 2018-07-23 18:19:02 +0000 | [diff] [blame] | 22 | APP_URL = 'https://cit-cli-metrics.appspot.com' |
| 23 | |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 +0000 | [diff] [blame] | 24 | EMPTY_LINE = ( |
| 25 | '* *' |
| 26 | ) |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 27 | NOTICE_COUNTDOWN_HEADER = ( |
| 28 | '*****************************************************\n' |
| 29 | '* METRICS COLLECTION WILL START IN %2d EXECUTIONS *' |
| 30 | ) |
| 31 | NOTICE_COLLECTION_HEADER = ( |
| 32 | '*****************************************************\n' |
| 33 | '* METRICS COLLECTION IS TAKING PLACE *' |
| 34 | ) |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 +0000 | [diff] [blame] | 35 | NOTICE_VERSION_CHANGE_HEADER = ( |
| 36 | '*****************************************************\n' |
Edward Lemur | 5a9ff43 | 2018-10-30 19:00:22 +0000 | [diff] [blame] | 37 | '* WE ARE COLLECTING ADDITIONAL METRICS *\n' |
| 38 | '* *\n' |
| 39 | '* Please review the changes and opt-in again. *' |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 +0000 | [diff] [blame] | 40 | ) |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 41 | NOTICE_FOOTER = ( |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 42 | '* For more information, and for how to disable this *\n' |
| 43 | '* message, please see metrics.README.md in your *\n' |
Edward Lemur | 5a9ff43 | 2018-10-30 19:00:22 +0000 | [diff] [blame] | 44 | '* depot_tools checkout or visit *\n' |
| 45 | '* https://bit.ly/2ufRS4p. *\n' |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 46 | '*****************************************************\n' |
| 47 | ) |
| 48 | |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 +0000 | [diff] [blame] | 49 | CHANGE_NOTICE = { |
| 50 | # No changes for version 0 |
| 51 | 0: '', |
Edward Lemur | 5a9ff43 | 2018-10-30 19:00:22 +0000 | [diff] [blame] | 52 | 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 Lemur | 4883626 | 2018-10-18 02:08:06 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | |
Edward Lemur | 40764b0 | 2018-07-20 18:50:29 +0000 | [diff] [blame] | 62 | KNOWN_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 Lemur | 03d6d11 | 2018-10-23 15:17:36 +0000 | [diff] [blame] | 83 | KNOWN_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 | |
| 96 | KNOWN_HTTP_METHODS = { |
| 97 | 'DELETE', |
| 98 | 'GET', |
| 99 | 'PATCH', |
| 100 | 'POST', |
| 101 | 'PUT', |
| 102 | } |
| 103 | |
| 104 | KNOWN_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 | |
| 133 | KNOWN_HTTP_ARGS = { |
| 134 | 'ALL_REVISIONS', |
| 135 | 'CURRENT_COMMIT', |
| 136 | 'CURRENT_REVISION', |
| 137 | 'DETAILED_ACCOUNTS', |
| 138 | 'LABELS', |
| 139 | } |
| 140 | |
Edward Lemur | 861640f | 2018-10-31 19:45:31 +0000 | [diff] [blame] | 141 | GIT_VERSION_RE = re.compile( |
| 142 | r'git version (\d)\.(\d{0,2})\.(\d{0,2})' |
| 143 | ) |
| 144 | |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 145 | |
| 146 | def get_python_version(): |
| 147 | """Return the python version in the major.minor.micro format.""" |
| 148 | return '{v.major}.{v.minor}.{v.micro}'.format(v=sys.version_info) |
| 149 | |
| 150 | |
Edward Lemur | 861640f | 2018-10-31 19:45:31 +0000 | [diff] [blame] | 151 | def get_git_version(): |
| 152 | """Return the Git version in the major.minor.micro format.""" |
| 153 | p = subprocess2.Popen( |
| 154 | ['git', '--version'], |
| 155 | stdout=subprocess2.PIPE, stderr=subprocess2.PIPE) |
| 156 | stdout, _ = p.communicate() |
| 157 | match = GIT_VERSION_RE.match(stdout) |
| 158 | if not match: |
| 159 | return None |
| 160 | return '%s.%s.%s' % match.groups() |
| 161 | |
| 162 | |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 163 | def return_code_from_exception(exception): |
| 164 | """Returns the exit code that would result of raising the exception.""" |
| 165 | if exception is None: |
| 166 | return 0 |
| 167 | if isinstance(exception[1], SystemExit): |
| 168 | return exception[1].code |
| 169 | return 1 |
| 170 | |
| 171 | |
| 172 | def seconds_to_weeks(duration): |
| 173 | """Transform a |duration| from seconds to weeks approximately. |
| 174 | |
| 175 | Drops the lowest 19 bits of the integer representation, which ammounts to |
| 176 | about 6 days. |
| 177 | """ |
| 178 | return int(duration) >> 19 |
| 179 | |
| 180 | |
Edward Lemur | 03d6d11 | 2018-10-23 15:17:36 +0000 | [diff] [blame] | 181 | def extract_http_metrics(request_uri, method, status, response_time): |
| 182 | """Extract metrics from the request URI. |
| 183 | |
| 184 | Extracts the host, path, and arguments from the request URI, and returns them |
| 185 | along with the method, status and response time. |
| 186 | |
| 187 | The host, method, path and arguments must be in the KNOWN_HTTP_* constants |
| 188 | defined above. |
| 189 | |
| 190 | Arguments are the values of the o= url parameter. In Gerrit, additional fields |
| 191 | can be obtained by adding o parameters, each option requires more database |
| 192 | lookups and slows down the query response time to the client, so we make an |
| 193 | effort to collect them. |
| 194 | |
| 195 | The regex defined in KNOWN_HTTP_PATH_RES are checked against the path, and |
| 196 | those that match will be returned. |
| 197 | """ |
| 198 | http_metrics = { |
| 199 | 'status': status, |
| 200 | 'response_time': response_time, |
| 201 | } |
| 202 | |
| 203 | if method in KNOWN_HTTP_METHODS: |
| 204 | http_metrics['method'] = method |
| 205 | |
| 206 | parsed_url = urlparse.urlparse(request_uri) |
| 207 | |
| 208 | if parsed_url.netloc in KNOWN_HTTP_HOSTS: |
| 209 | http_metrics['host'] = parsed_url.netloc |
| 210 | |
| 211 | for name, path_re in KNOWN_HTTP_PATHS.iteritems(): |
| 212 | if path_re.match(parsed_url.path): |
| 213 | http_metrics['path'] = name |
| 214 | break |
| 215 | |
| 216 | parsed_query = urlparse.parse_qs(parsed_url.query) |
| 217 | |
| 218 | # Collect o-parameters from the request. |
| 219 | args = [ |
| 220 | arg for arg in parsed_query.get('o', []) |
| 221 | if arg in KNOWN_HTTP_ARGS |
| 222 | ] |
| 223 | if args: |
| 224 | http_metrics['arguments'] = args |
| 225 | |
| 226 | return http_metrics |
| 227 | |
| 228 | |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 229 | def get_repo_timestamp(path_to_repo): |
| 230 | """Get an approximate timestamp for the upstream of |path_to_repo|. |
| 231 | |
| 232 | Returns the top two bits of the timestamp of the HEAD for the upstream of the |
| 233 | branch path_to_repo is checked out at. |
| 234 | """ |
| 235 | # Get the upstream for the current branch. If we're not in a branch, fallback |
| 236 | # to HEAD. |
| 237 | try: |
| 238 | upstream = scm.GIT.GetUpstreamBranch(path_to_repo) |
| 239 | except subprocess2.CalledProcessError: |
| 240 | upstream = 'HEAD' |
| 241 | |
| 242 | # Get the timestamp of the HEAD for the upstream of the current branch. |
| 243 | p = subprocess2.Popen( |
| 244 | ['git', '-C', path_to_repo, 'log', '-n1', upstream, '--format=%at'], |
| 245 | stdout=subprocess2.PIPE, stderr=subprocess2.PIPE) |
| 246 | stdout, _ = p.communicate() |
| 247 | |
| 248 | # If there was an error, give up. |
| 249 | if p.returncode != 0: |
| 250 | return None |
| 251 | |
| 252 | # Get the age of the checkout in weeks. |
| 253 | return seconds_to_weeks(stdout.strip()) |
| 254 | |
| 255 | |
| 256 | def print_notice(countdown): |
| 257 | """Print a notice to let the user know the status of metrics collection.""" |
| 258 | colorama.init() |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 +0000 | [diff] [blame] | 259 | print(colorama.Fore.RED + '\033[1m', file=sys.stderr, end='') |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 260 | if countdown: |
Edward Lemur | c87d45b | 2018-07-26 17:43:11 +0000 | [diff] [blame] | 261 | print(NOTICE_COUNTDOWN_HEADER % countdown, file=sys.stderr) |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 262 | else: |
Edward Lemur | c87d45b | 2018-07-26 17:43:11 +0000 | [diff] [blame] | 263 | print(NOTICE_COLLECTION_HEADER, file=sys.stderr) |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 +0000 | [diff] [blame] | 264 | print(EMPTY_LINE, file=sys.stderr) |
Edward Lemur | c87d45b | 2018-07-26 17:43:11 +0000 | [diff] [blame] | 265 | print(NOTICE_FOOTER + colorama.Style.RESET_ALL, file=sys.stderr) |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 +0000 | [diff] [blame] | 266 | |
| 267 | |
| 268 | def print_version_change(config_version): |
| 269 | """Print a notice to let the user know we are collecting more metrics.""" |
| 270 | colorama.init() |
| 271 | print(colorama.Fore.RED + '\033[1m', file=sys.stderr, end='') |
| 272 | print(NOTICE_VERSION_CHANGE_HEADER, file=sys.stderr) |
| 273 | print(EMPTY_LINE, file=sys.stderr) |
| 274 | for version in range(config_version + 1, CURRENT_VERSION + 1): |
| 275 | print(CHANGE_NOTICE[version], file=sys.stderr) |
| 276 | print(EMPTY_LINE, file=sys.stderr) |