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 | dd5051f | 2018-08-08 00:56:41 +0000 | [diff] [blame] | 6 | from __future__ import print_function |
| 7 | |
Edward Lemur | 6f812e1 | 2018-07-31 22:45:57 +0000 | [diff] [blame] | 8 | import contextlib |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 9 | import functools |
| 10 | import json |
| 11 | import os |
| 12 | import subprocess |
| 13 | import sys |
| 14 | import tempfile |
| 15 | import threading |
| 16 | import time |
| 17 | import traceback |
| 18 | import urllib2 |
| 19 | |
| 20 | import detect_host_arch |
| 21 | import gclient_utils |
| 22 | import metrics_utils |
| 23 | |
| 24 | |
| 25 | DEPOT_TOOLS = os.path.dirname(os.path.abspath(__file__)) |
| 26 | CONFIG_FILE = os.path.join(DEPOT_TOOLS, 'metrics.cfg') |
| 27 | UPLOAD_SCRIPT = os.path.join(DEPOT_TOOLS, 'upload_metrics.py') |
| 28 | |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 29 | DISABLE_METRICS_COLLECTION = os.environ.get('DEPOT_TOOLS_METRICS') == '0' |
| 30 | DEFAULT_COUNTDOWN = 10 |
| 31 | |
| 32 | INVALID_CONFIG_WARNING = ( |
Edward Lemur | dd5051f | 2018-08-08 00:56:41 +0000 | [diff] [blame] | 33 | 'WARNING: Your metrics.cfg file was invalid or nonexistent. A new one will ' |
| 34 | 'been created.' |
| 35 | ) |
| 36 | PERMISSION_DENIED_WARNING = ( |
| 37 | 'Could not write the metrics collection config:\n\t%s\n' |
| 38 | 'Metrics collection will be disabled.' |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 39 | ) |
| 40 | |
| 41 | |
| 42 | class _Config(object): |
| 43 | def __init__(self): |
| 44 | self._initialized = False |
| 45 | self._config = {} |
| 46 | |
| 47 | def _ensure_initialized(self): |
| 48 | if self._initialized: |
| 49 | return |
| 50 | |
| 51 | try: |
| 52 | config = json.loads(gclient_utils.FileRead(CONFIG_FILE)) |
| 53 | except (IOError, ValueError): |
| 54 | config = {} |
| 55 | |
| 56 | self._config = config.copy() |
| 57 | |
| 58 | if 'is-googler' not in self._config: |
| 59 | # /should-upload is only accessible from Google IPs, so we only need to |
| 60 | # check if we can reach the page. An external developer would get access |
| 61 | # denied. |
| 62 | try: |
Edward Lemur | 5ba1e9c | 2018-07-23 18:19:02 +0000 | [diff] [blame] | 63 | req = urllib2.urlopen(metrics_utils.APP_URL + '/should-upload') |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 64 | self._config['is-googler'] = req.getcode() == 200 |
| 65 | except (urllib2.URLError, urllib2.HTTPError): |
| 66 | self._config['is-googler'] = False |
| 67 | |
| 68 | # Make sure the config variables we need are present, and initialize them to |
| 69 | # safe values otherwise. |
| 70 | self._config.setdefault('countdown', DEFAULT_COUNTDOWN) |
| 71 | self._config.setdefault('opt-in', None) |
| 72 | |
| 73 | if config != self._config: |
Edward Lemur | dd5051f | 2018-08-08 00:56:41 +0000 | [diff] [blame] | 74 | print(INVALID_CONFIG_WARNING, file=sys.stderr) |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 75 | self._write_config() |
| 76 | |
| 77 | self._initialized = True |
| 78 | |
| 79 | def _write_config(self): |
Edward Lemur | dd5051f | 2018-08-08 00:56:41 +0000 | [diff] [blame] | 80 | try: |
| 81 | gclient_utils.FileWrite(CONFIG_FILE, json.dumps(self._config)) |
| 82 | except IOError as e: |
| 83 | print(PERMISSION_DENIED_WARNING % e, file=sys.stderr) |
| 84 | self._config['opt-in'] = False |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 85 | |
| 86 | @property |
| 87 | def is_googler(self): |
| 88 | self._ensure_initialized() |
| 89 | return self._config['is-googler'] |
| 90 | |
| 91 | @property |
| 92 | def opted_in(self): |
| 93 | self._ensure_initialized() |
| 94 | return self._config['opt-in'] |
| 95 | |
| 96 | @opted_in.setter |
| 97 | def opted_in(self, value): |
| 98 | self._ensure_initialized() |
| 99 | self._config['opt-in'] = value |
| 100 | self._write_config() |
| 101 | |
| 102 | @property |
| 103 | def countdown(self): |
| 104 | self._ensure_initialized() |
| 105 | return self._config['countdown'] |
| 106 | |
| 107 | def decrease_countdown(self): |
| 108 | self._ensure_initialized() |
| 109 | if self.countdown == 0: |
| 110 | return |
| 111 | self._config['countdown'] -= 1 |
| 112 | self._write_config() |
| 113 | |
| 114 | |
| 115 | class MetricsCollector(object): |
| 116 | def __init__(self): |
| 117 | self._metrics_lock = threading.Lock() |
| 118 | self._reported_metrics = {} |
| 119 | self._config = _Config() |
Edward Lemur | 3298e7b | 2018-07-17 18:21:27 +0000 | [diff] [blame] | 120 | self._collecting_metrics = False |
Edward Lemur | 6f812e1 | 2018-07-31 22:45:57 +0000 | [diff] [blame] | 121 | self._collect_custom_metrics = True |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 122 | |
| 123 | @property |
| 124 | def config(self): |
| 125 | return self._config |
| 126 | |
Edward Lemur | 3298e7b | 2018-07-17 18:21:27 +0000 | [diff] [blame] | 127 | @property |
| 128 | def collecting_metrics(self): |
| 129 | return self._collecting_metrics |
| 130 | |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 131 | def add(self, name, value): |
Edward Lemur | 6f812e1 | 2018-07-31 22:45:57 +0000 | [diff] [blame] | 132 | if self._collect_custom_metrics: |
| 133 | with self._metrics_lock: |
| 134 | self._reported_metrics[name] = value |
| 135 | |
| 136 | @contextlib.contextmanager |
| 137 | def pause_metrics_collection(self): |
| 138 | collect_custom_metrics = self._collect_custom_metrics |
| 139 | self._collect_custom_metrics = False |
| 140 | try: |
| 141 | yield |
| 142 | finally: |
| 143 | self._collect_custom_metrics = collect_custom_metrics |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 144 | |
| 145 | def _upload_metrics_data(self): |
| 146 | """Upload the metrics data to the AppEngine app.""" |
| 147 | # We invoke a subprocess, and use stdin.write instead of communicate(), |
| 148 | # so that we are able to return immediately, leaving the upload running in |
| 149 | # the background. |
| 150 | p = subprocess.Popen([sys.executable, UPLOAD_SCRIPT], stdin=subprocess.PIPE) |
| 151 | p.stdin.write(json.dumps(self._reported_metrics)) |
| 152 | |
| 153 | def _collect_metrics(self, func, command_name, *args, **kwargs): |
Edward Lemur | 6f812e1 | 2018-07-31 22:45:57 +0000 | [diff] [blame] | 154 | # If we're already collecting metrics, just execute the function. |
| 155 | # e.g. git-cl split invokes git-cl upload several times to upload each |
| 156 | # splitted CL. |
| 157 | if self.collecting_metrics: |
| 158 | # Don't collect metrics for this function. |
| 159 | # e.g. Don't record the arguments git-cl split passes to git-cl upload. |
| 160 | with self.pause_metrics_collection(): |
| 161 | return func(*args, **kwargs) |
Edward Lemur | 7fa0f19 | 2018-07-17 21:33:37 +0000 | [diff] [blame] | 162 | |
| 163 | self._collecting_metrics = True |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 164 | self.add('command', command_name) |
| 165 | try: |
| 166 | start = time.time() |
Edward Lemur | 6f812e1 | 2018-07-31 22:45:57 +0000 | [diff] [blame] | 167 | result = func(*args, **kwargs) |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 168 | exception = None |
| 169 | # pylint: disable=bare-except |
| 170 | except: |
| 171 | exception = sys.exc_info() |
| 172 | finally: |
| 173 | self.add('execution_time', time.time() - start) |
| 174 | |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 175 | exit_code = metrics_utils.return_code_from_exception(exception) |
| 176 | self.add('exit_code', exit_code) |
| 177 | |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 178 | # Add metrics regarding environment information. |
| 179 | self.add('timestamp', metrics_utils.seconds_to_weeks(time.time())) |
| 180 | self.add('python_version', metrics_utils.get_python_version()) |
| 181 | self.add('host_os', gclient_utils.GetMacWinOrLinux()) |
| 182 | self.add('host_arch', detect_host_arch.HostArch()) |
| 183 | self.add('depot_tools_age', metrics_utils.get_repo_timestamp(DEPOT_TOOLS)) |
| 184 | |
| 185 | self._upload_metrics_data() |
Edward Lemur | 6f812e1 | 2018-07-31 22:45:57 +0000 | [diff] [blame] | 186 | if exception: |
| 187 | raise exception[0], exception[1], exception[2] |
| 188 | return result |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 189 | |
| 190 | def collect_metrics(self, command_name): |
| 191 | """A decorator used to collect metrics over the life of a function. |
| 192 | |
| 193 | This decorator executes the function and collects metrics about the system |
Edward Lemur | 6f812e1 | 2018-07-31 22:45:57 +0000 | [diff] [blame] | 194 | environment and the function performance. |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 195 | """ |
| 196 | def _decorator(func): |
| 197 | # Do this first so we don't have to read, and possibly create a config |
| 198 | # file. |
| 199 | if DISABLE_METRICS_COLLECTION: |
| 200 | return func |
Edward Lemur | 6f812e1 | 2018-07-31 22:45:57 +0000 | [diff] [blame] | 201 | # Don't collect the metrics unless the user is a googler, the user has |
| 202 | # opted in, or the countdown has expired. |
| 203 | if (not self.config.is_googler or self.config.opted_in == False |
| 204 | or (self.config.opted_in is None and self.config.countdown > 0)): |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 205 | return func |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 206 | # Otherwise, collect the metrics. |
| 207 | # Needed to preserve the __name__ and __doc__ attributes of func. |
| 208 | @functools.wraps(func) |
| 209 | def _inner(*args, **kwargs): |
Edward Lemur | 6f812e1 | 2018-07-31 22:45:57 +0000 | [diff] [blame] | 210 | return self._collect_metrics(func, command_name, *args, **kwargs) |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 211 | return _inner |
| 212 | return _decorator |
| 213 | |
Edward Lemur | 6f812e1 | 2018-07-31 22:45:57 +0000 | [diff] [blame] | 214 | @contextlib.contextmanager |
| 215 | def print_notice_and_exit(self): |
| 216 | """A context manager used to print the notice and terminate execution. |
| 217 | |
| 218 | This decorator executes the function and prints the monitoring notice if |
| 219 | necessary. If an exception is raised, we will catch it, and print it before |
| 220 | printing the metrics collection notice. |
| 221 | This will call sys.exit() with an appropriate exit code to ensure the notice |
| 222 | is the last thing printed.""" |
| 223 | # Needed to preserve the __name__ and __doc__ attributes of func. |
| 224 | try: |
| 225 | yield |
| 226 | exception = None |
| 227 | # pylint: disable=bare-except |
| 228 | except: |
| 229 | exception = sys.exc_info() |
| 230 | |
| 231 | # Print the exception before the metrics notice, so that the notice is |
| 232 | # clearly visible even if gclient fails. |
| 233 | if exception: |
| 234 | if isinstance(exception[1], KeyboardInterrupt): |
| 235 | sys.stderr.write('Interrupted\n') |
| 236 | elif not isinstance(exception[1], SystemExit): |
| 237 | traceback.print_exception(*exception) |
| 238 | |
| 239 | # Print the notice |
| 240 | if (not DISABLE_METRICS_COLLECTION and self.config.is_googler |
| 241 | and self.config.opted_in is None): |
| 242 | metrics_utils.print_notice(self.config.countdown) |
| 243 | self.config.decrease_countdown() |
| 244 | |
| 245 | sys.exit(metrics_utils.return_code_from_exception(exception)) |
| 246 | |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 +0000 | [diff] [blame] | 247 | |
| 248 | collector = MetricsCollector() |