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 Lesmes | 1e59a24 | 2021-04-30 18:38:25 +0000 | [diff] [blame] | 9 | import os |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 10 | import scm |
| 11 | import subprocess2 |
| 12 | import sys |
Raul Tambre | b946b23 | 2019-03-26 14:48:46 +0000 | [diff] [blame] | 13 | |
| 14 | try: |
| 15 | import urlparse |
| 16 | except ImportError: # For Py3 compatibility |
| 17 | import urllib.parse as urlparse |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 18 | |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 19 | |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 +0000 | [diff] [blame] | 20 | # 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 Lesmes | 1e59a24 | 2021-04-30 18:38:25 +0000 | [diff] [blame] | 23 | CURRENT_VERSION = 2 |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 +0000 | [diff] [blame] | 24 | |
Edward Lemur | 5ba1e9c | 2018-07-23 18:19:02 +0000 | [diff] [blame] | 25 | APP_URL = 'https://cit-cli-metrics.appspot.com' |
| 26 | |
Edward Lesmes | 9c34906 | 2021-05-06 20:02:39 +0000 | [diff] [blame] | 27 | REPORT_BUILD = os.getenv('DEPOT_TOOLS_REPORT_BUILD') |
Edward Lesmes | 09f358b | 2021-06-22 22:31:52 +0000 | [diff] [blame] | 28 | COLLECT_METRICS = ( |
| 29 | os.getenv('DEPOT_TOOLS_COLLECT_METRICS') != '0' |
| 30 | and os.getenv('DEPOT_TOOLS_METRICS') != '0') |
Edward Lesmes | 1e59a24 | 2021-04-30 18:38:25 +0000 | [diff] [blame] | 31 | |
Edward Lesmes | c8f63d3 | 2021-06-02 23:51:53 +0000 | [diff] [blame] | 32 | SYNC_STATUS_SUCCESS = 'SYNC_STATUS_SUCCESS' |
| 33 | SYNC_STATUS_FAILURE = 'SYNC_STATUS_FAILURE' |
| 34 | |
Edward Lesmes | 1e59a24 | 2021-04-30 18:38:25 +0000 | [diff] [blame] | 35 | |
Samuel Huang | 98a7e80 | 2019-02-12 15:32:22 +0000 | [diff] [blame] | 36 | def get_notice_countdown_header(countdown): |
| 37 | if countdown == 0: |
| 38 | yield ' METRICS COLLECTION IS TAKING PLACE' |
| 39 | else: |
| 40 | yield ' METRICS COLLECTION WILL START IN %d EXECUTIONS' % countdown |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 41 | |
Samuel Huang | 98a7e80 | 2019-02-12 15:32:22 +0000 | [diff] [blame] | 42 | def get_notice_version_change_header(): |
| 43 | yield ' WE ARE COLLECTING ADDITIONAL METRICS' |
| 44 | yield '' |
| 45 | yield ' Please review the changes and opt-in again.' |
| 46 | |
| 47 | def get_notice_footer(): |
| 48 | yield 'To suppress this message opt in or out using:' |
| 49 | yield '$ gclient metrics [--opt-in] [--opt-out]' |
| 50 | yield 'For more information please see metrics.README.md' |
| 51 | yield 'in your depot_tools checkout or visit' |
| 52 | yield 'https://goo.gl/yNpRDV.' |
| 53 | |
| 54 | def get_change_notice(version): |
| 55 | if version == 0: |
Edward Lesmes | 1e59a24 | 2021-04-30 18:38:25 +0000 | [diff] [blame] | 56 | return [] # No changes for version 0 |
Aravind Vasudevan | 22bf605 | 2022-01-24 21:11:19 +0000 | [diff] [blame^] | 57 | |
| 58 | if version == 1: |
Edward Lesmes | 1e59a24 | 2021-04-30 18:38:25 +0000 | [diff] [blame] | 59 | return [ |
| 60 | 'We want to collect the Git version.', |
| 61 | 'We want to collect information about the HTTP', |
| 62 | 'requests that depot_tools makes, and the git and', |
| 63 | 'cipd commands it executes.', |
| 64 | '', |
| 65 | 'We only collect known strings to make sure we', |
| 66 | 'don\'t record PII.', |
| 67 | ] |
Aravind Vasudevan | 22bf605 | 2022-01-24 21:11:19 +0000 | [diff] [blame^] | 68 | |
| 69 | if version == 2: |
Edward Lesmes | 1e59a24 | 2021-04-30 18:38:25 +0000 | [diff] [blame] | 70 | return [ |
| 71 | 'We will start collecting metrics from bots.', |
| 72 | 'There are no changes for developers.', |
| 73 | 'If the DEPOT_TOOLS_REPORT_BUILD environment variable is set,', |
| 74 | 'we will report information about the current build', |
| 75 | '(e.g. buildbucket project, bucket, builder and build id),', |
| 76 | 'and authenticate to the metrics collection server.', |
| 77 | 'This information will only be recorded for requests', |
| 78 | 'authenticated as bot service accounts.', |
| 79 | ] |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 +0000 | [diff] [blame] | 80 | |
| 81 | |
Edward Lemur | 40764b0 | 2018-07-20 18:50:29 +0000 | [diff] [blame] | 82 | KNOWN_PROJECT_URLS = { |
| 83 | 'https://chrome-internal.googlesource.com/chrome/ios_internal', |
| 84 | 'https://chrome-internal.googlesource.com/infra/infra_internal', |
| 85 | 'https://chromium.googlesource.com/breakpad/breakpad', |
| 86 | 'https://chromium.googlesource.com/chromium/src', |
| 87 | 'https://chromium.googlesource.com/chromium/tools/depot_tools', |
| 88 | 'https://chromium.googlesource.com/crashpad/crashpad', |
| 89 | 'https://chromium.googlesource.com/external/gyp', |
| 90 | 'https://chromium.googlesource.com/external/naclports', |
| 91 | 'https://chromium.googlesource.com/infra/goma/client', |
| 92 | 'https://chromium.googlesource.com/infra/infra', |
| 93 | 'https://chromium.googlesource.com/native_client/', |
| 94 | 'https://chromium.googlesource.com/syzygy', |
| 95 | 'https://chromium.googlesource.com/v8/v8', |
| 96 | 'https://dart.googlesource.com/sdk', |
| 97 | 'https://pdfium.googlesource.com/pdfium', |
| 98 | 'https://skia.googlesource.com/buildbot', |
| 99 | 'https://skia.googlesource.com/skia', |
| 100 | 'https://webrtc.googlesource.com/src', |
| 101 | } |
| 102 | |
Edward Lemur | 03d6d11 | 2018-10-23 15:17:36 +0000 | [diff] [blame] | 103 | KNOWN_HTTP_HOSTS = { |
| 104 | 'chrome-internal-review.googlesource.com', |
| 105 | 'chromium-review.googlesource.com', |
| 106 | 'dart-review.googlesource.com', |
| 107 | 'eu1-mirror-chromium-review.googlesource.com', |
| 108 | 'pdfium-review.googlesource.com', |
| 109 | 'skia-review.googlesource.com', |
| 110 | 'us1-mirror-chromium-review.googlesource.com', |
| 111 | 'us2-mirror-chromium-review.googlesource.com', |
| 112 | 'us3-mirror-chromium-review.googlesource.com', |
| 113 | 'webrtc-review.googlesource.com', |
| 114 | } |
| 115 | |
| 116 | KNOWN_HTTP_METHODS = { |
| 117 | 'DELETE', |
| 118 | 'GET', |
| 119 | 'PATCH', |
| 120 | 'POST', |
| 121 | 'PUT', |
| 122 | } |
| 123 | |
| 124 | KNOWN_HTTP_PATHS = { |
| 125 | 'accounts': |
| 126 | re.compile(r'(/a)?/accounts/.*'), |
| 127 | 'changes': |
| 128 | re.compile(r'(/a)?/changes/([^/]+)?$'), |
| 129 | 'changes/abandon': |
| 130 | re.compile(r'(/a)?/changes/.*/abandon'), |
| 131 | 'changes/comments': |
| 132 | re.compile(r'(/a)?/changes/.*/comments'), |
| 133 | 'changes/detail': |
| 134 | re.compile(r'(/a)?/changes/.*/detail'), |
| 135 | 'changes/edit': |
| 136 | re.compile(r'(/a)?/changes/.*/edit'), |
| 137 | 'changes/message': |
| 138 | re.compile(r'(/a)?/changes/.*/message'), |
| 139 | 'changes/restore': |
| 140 | re.compile(r'(/a)?/changes/.*/restore'), |
| 141 | 'changes/reviewers': |
| 142 | re.compile(r'(/a)?/changes/.*/reviewers/.*'), |
| 143 | 'changes/revisions/commit': |
| 144 | re.compile(r'(/a)?/changes/.*/revisions/.*/commit'), |
| 145 | 'changes/revisions/review': |
| 146 | re.compile(r'(/a)?/changes/.*/revisions/.*/review'), |
| 147 | 'changes/submit': |
| 148 | re.compile(r'(/a)?/changes/.*/submit'), |
| 149 | 'projects/branches': |
| 150 | re.compile(r'(/a)?/projects/.*/branches/.*'), |
| 151 | } |
| 152 | |
| 153 | KNOWN_HTTP_ARGS = { |
| 154 | 'ALL_REVISIONS', |
| 155 | 'CURRENT_COMMIT', |
| 156 | 'CURRENT_REVISION', |
| 157 | 'DETAILED_ACCOUNTS', |
| 158 | 'LABELS', |
| 159 | } |
| 160 | |
Edward Lemur | 861640f | 2018-10-31 19:45:31 +0000 | [diff] [blame] | 161 | GIT_VERSION_RE = re.compile( |
| 162 | r'git version (\d)\.(\d{0,2})\.(\d{0,2})' |
| 163 | ) |
| 164 | |
Edward Lemur | fec80c4 | 2018-11-01 23:14:14 +0000 | [diff] [blame] | 165 | KNOWN_SUBCOMMAND_ARGS = { |
| 166 | 'cc', |
| 167 | 'hashtag', |
| 168 | 'l=Auto-Submit+1', |
Edward Lemur | 687ca90 | 2018-12-05 02:30:30 +0000 | [diff] [blame] | 169 | 'l=Code-Review+1', |
| 170 | 'l=Code-Review+2', |
Edward Lemur | fec80c4 | 2018-11-01 23:14:14 +0000 | [diff] [blame] | 171 | 'l=Commit-Queue+1', |
| 172 | 'l=Commit-Queue+2', |
| 173 | 'label', |
| 174 | 'm', |
| 175 | 'notify=ALL', |
| 176 | 'notify=NONE', |
| 177 | 'private', |
| 178 | 'r', |
| 179 | 'ready', |
| 180 | 'topic', |
| 181 | 'wip' |
| 182 | } |
| 183 | |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 184 | |
| 185 | def get_python_version(): |
| 186 | """Return the python version in the major.minor.micro format.""" |
| 187 | return '{v.major}.{v.minor}.{v.micro}'.format(v=sys.version_info) |
| 188 | |
| 189 | |
Edward Lemur | 861640f | 2018-10-31 19:45:31 +0000 | [diff] [blame] | 190 | def get_git_version(): |
| 191 | """Return the Git version in the major.minor.micro format.""" |
| 192 | p = subprocess2.Popen( |
| 193 | ['git', '--version'], |
| 194 | stdout=subprocess2.PIPE, stderr=subprocess2.PIPE) |
| 195 | stdout, _ = p.communicate() |
Edward Lemur | 73065b2 | 2019-07-22 20:12:01 +0000 | [diff] [blame] | 196 | match = GIT_VERSION_RE.match(stdout.decode('utf-8')) |
Edward Lemur | 861640f | 2018-10-31 19:45:31 +0000 | [diff] [blame] | 197 | if not match: |
| 198 | return None |
| 199 | return '%s.%s.%s' % match.groups() |
| 200 | |
| 201 | |
Edward Lesmes | 1e59a24 | 2021-04-30 18:38:25 +0000 | [diff] [blame] | 202 | def get_bot_metrics(): |
Edward Lesmes | 9c34906 | 2021-05-06 20:02:39 +0000 | [diff] [blame] | 203 | try: |
| 204 | project, bucket, builder, build = REPORT_BUILD.split('/') |
| 205 | return { |
| 206 | 'build_id': int(build), |
| 207 | 'builder': { |
| 208 | 'project': project, |
| 209 | 'bucket': bucket, |
| 210 | 'builder': builder, |
| 211 | }, |
| 212 | } |
| 213 | except (AttributeError, ValueError): |
Edward Lesmes | 1e59a24 | 2021-04-30 18:38:25 +0000 | [diff] [blame] | 214 | return None |
Edward Lesmes | 1e59a24 | 2021-04-30 18:38:25 +0000 | [diff] [blame] | 215 | |
| 216 | |
| 217 | |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 218 | def return_code_from_exception(exception): |
| 219 | """Returns the exit code that would result of raising the exception.""" |
| 220 | if exception is None: |
| 221 | return 0 |
| 222 | if isinstance(exception[1], SystemExit): |
| 223 | return exception[1].code |
| 224 | return 1 |
| 225 | |
| 226 | |
Edward Lemur | fec80c4 | 2018-11-01 23:14:14 +0000 | [diff] [blame] | 227 | def extract_known_subcommand_args(args): |
| 228 | """Extract the known arguments from the passed list of args.""" |
| 229 | known_args = [] |
| 230 | for arg in args: |
| 231 | if arg in KNOWN_SUBCOMMAND_ARGS: |
| 232 | known_args.append(arg) |
| 233 | else: |
| 234 | arg = arg.split('=')[0] |
| 235 | if arg in KNOWN_SUBCOMMAND_ARGS: |
| 236 | known_args.append(arg) |
Edward Lemur | 01f4a4f | 2018-11-03 00:40:38 +0000 | [diff] [blame] | 237 | return sorted(known_args) |
Edward Lemur | fec80c4 | 2018-11-01 23:14:14 +0000 | [diff] [blame] | 238 | |
| 239 | |
Edward Lemur | 03d6d11 | 2018-10-23 15:17:36 +0000 | [diff] [blame] | 240 | def extract_http_metrics(request_uri, method, status, response_time): |
| 241 | """Extract metrics from the request URI. |
| 242 | |
| 243 | Extracts the host, path, and arguments from the request URI, and returns them |
| 244 | along with the method, status and response time. |
| 245 | |
| 246 | The host, method, path and arguments must be in the KNOWN_HTTP_* constants |
| 247 | defined above. |
| 248 | |
| 249 | Arguments are the values of the o= url parameter. In Gerrit, additional fields |
| 250 | can be obtained by adding o parameters, each option requires more database |
| 251 | lookups and slows down the query response time to the client, so we make an |
| 252 | effort to collect them. |
| 253 | |
| 254 | The regex defined in KNOWN_HTTP_PATH_RES are checked against the path, and |
| 255 | those that match will be returned. |
| 256 | """ |
| 257 | http_metrics = { |
| 258 | 'status': status, |
| 259 | 'response_time': response_time, |
| 260 | } |
| 261 | |
| 262 | if method in KNOWN_HTTP_METHODS: |
| 263 | http_metrics['method'] = method |
| 264 | |
| 265 | parsed_url = urlparse.urlparse(request_uri) |
| 266 | |
| 267 | if parsed_url.netloc in KNOWN_HTTP_HOSTS: |
| 268 | http_metrics['host'] = parsed_url.netloc |
| 269 | |
Edward Lemur | 9217ff8 | 2019-07-16 23:14:24 +0000 | [diff] [blame] | 270 | for name, path_re in KNOWN_HTTP_PATHS.items(): |
Edward Lemur | 03d6d11 | 2018-10-23 15:17:36 +0000 | [diff] [blame] | 271 | if path_re.match(parsed_url.path): |
| 272 | http_metrics['path'] = name |
| 273 | break |
| 274 | |
| 275 | parsed_query = urlparse.parse_qs(parsed_url.query) |
| 276 | |
| 277 | # Collect o-parameters from the request. |
| 278 | args = [ |
| 279 | arg for arg in parsed_query.get('o', []) |
| 280 | if arg in KNOWN_HTTP_ARGS |
| 281 | ] |
| 282 | if args: |
| 283 | http_metrics['arguments'] = args |
| 284 | |
| 285 | return http_metrics |
| 286 | |
| 287 | |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 288 | def get_repo_timestamp(path_to_repo): |
| 289 | """Get an approximate timestamp for the upstream of |path_to_repo|. |
| 290 | |
| 291 | Returns the top two bits of the timestamp of the HEAD for the upstream of the |
| 292 | branch path_to_repo is checked out at. |
| 293 | """ |
| 294 | # Get the upstream for the current branch. If we're not in a branch, fallback |
| 295 | # to HEAD. |
| 296 | try: |
Edward Lemur | 36974ad | 2019-02-21 23:29:47 +0000 | [diff] [blame] | 297 | upstream = scm.GIT.GetUpstreamBranch(path_to_repo) or 'HEAD' |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 298 | except subprocess2.CalledProcessError: |
| 299 | upstream = 'HEAD' |
| 300 | |
| 301 | # Get the timestamp of the HEAD for the upstream of the current branch. |
| 302 | p = subprocess2.Popen( |
| 303 | ['git', '-C', path_to_repo, 'log', '-n1', upstream, '--format=%at'], |
| 304 | stdout=subprocess2.PIPE, stderr=subprocess2.PIPE) |
| 305 | stdout, _ = p.communicate() |
| 306 | |
| 307 | # If there was an error, give up. |
| 308 | if p.returncode != 0: |
| 309 | return None |
| 310 | |
Edward Lemur | 18df41e | 2019-04-26 00:42:04 +0000 | [diff] [blame] | 311 | return stdout.strip() |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 312 | |
Hans Wennborg | 24b5f90 | 2019-03-15 18:51:27 +0000 | [diff] [blame] | 313 | def print_boxed_text(out, min_width, lines): |
| 314 | [EW, NS, SE, SW, NE, NW] = list('=|++++') |
Samuel Huang | 98a7e80 | 2019-02-12 15:32:22 +0000 | [diff] [blame] | 315 | width = max(min_width, max(len(line) for line in lines)) |
| 316 | out(SE + EW * (width + 2) + SW + '\n') |
| 317 | for line in lines: |
Raul Tambre | b946b23 | 2019-03-26 14:48:46 +0000 | [diff] [blame] | 318 | out('%s %-*s %s\n' % (NS, width, line, NS)) |
Samuel Huang | 98a7e80 | 2019-02-12 15:32:22 +0000 | [diff] [blame] | 319 | out(NE + EW * (width + 2) + NW + '\n') |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 320 | |
| 321 | def print_notice(countdown): |
| 322 | """Print a notice to let the user know the status of metrics collection.""" |
Samuel Huang | 98a7e80 | 2019-02-12 15:32:22 +0000 | [diff] [blame] | 323 | lines = list(get_notice_countdown_header(countdown)) |
| 324 | lines.append('') |
| 325 | lines += list(get_notice_footer()) |
Hans Wennborg | 24b5f90 | 2019-03-15 18:51:27 +0000 | [diff] [blame] | 326 | print_boxed_text(sys.stderr.write, 49, lines) |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 +0000 | [diff] [blame] | 327 | |
| 328 | def print_version_change(config_version): |
| 329 | """Print a notice to let the user know we are collecting more metrics.""" |
Samuel Huang | 98a7e80 | 2019-02-12 15:32:22 +0000 | [diff] [blame] | 330 | lines = list(get_notice_version_change_header()) |
Edward Lemur | 9217ff8 | 2019-07-16 23:14:24 +0000 | [diff] [blame] | 331 | for version in range(config_version + 1, CURRENT_VERSION + 1): |
Samuel Huang | 98a7e80 | 2019-02-12 15:32:22 +0000 | [diff] [blame] | 332 | lines.append('') |
Edward Lesmes | 1e59a24 | 2021-04-30 18:38:25 +0000 | [diff] [blame] | 333 | lines += get_change_notice(version) |
Hans Wennborg | 24b5f90 | 2019-03-15 18:51:27 +0000 | [diff] [blame] | 334 | print_boxed_text(sys.stderr.write, 49, lines) |