def testContextManagerIgnoresInvalidField(self): """Test that we ignore fields that are set with no default.""" with metrics.SuccessCounter('fooname', fields={'foo': 'bar'}) as c: c['qux'] = 'qwert' self._mockMetric.increment.assert_called_with( fields={'foo': 'bar', 'success': True})
def testContextManagerFailedExplicit(self): """Test that we fail when an exception is raised.""" with metrics.SuccessCounter('fooname') as s: s['success'] = False self._mockMetric.increment.assert_called_with( fields={'success': False})
def testContextManagerWithoutUpdate(self): """Tests that the default value for fields is used when not updated.""" # pylint: disable=unused-variable with metrics.SuccessCounter('fooname', fields={'foo': 'bar'}) as c: pass self._mockMetric.increment.assert_called_with( fields={'foo': 'bar', 'success': True})
def testContextManager(self): """Test that timing context manager emits a metric.""" with metrics.SuccessCounter('fooname'): pass self._mockMetric.increment.assert_called_with( fields={'success': True}) self.assertEqual(self._mockMetric.increment.call_count, 1)
def main(argv): parser = commandline.ArgumentParser(description=__doc__) parser.add_argument('swarming_server', action='store', help='Swarming server to send no-op requests to.') options = parser.parse_args(argv) m_timer = 'chromeos/autotest/swarming_proxy/no_op_durations' m_count = 'chromeos/autotest/swarming_proxy/no_op_attempts' command = commands.RUN_SUITE_PATH fields = {'success': False, 'swarming_server': options.swarming_server} with ts_mon_config.SetupTsMonGlobalState('swarm_mon', indirect=True): while True: with metrics.SecondsTimer(m_timer, fields=fields) as f: try: with metrics.SuccessCounter(m_count): swarming_lib.RunSwarmingCommand( [command, '--do_nothing'], options.swarming_server, dimensions=[('pool', 'default')], timeout_secs=120) f['success'] = True except (cros_build_lib.RunCommandError, timeout_util.TimeoutError): pass time.sleep(60)
def testContextManagerFailedException(self): """Test that we fail when an exception is raised.""" with self.assertRaises(FakeException): with metrics.SuccessCounter('fooname'): raise FakeException self._mockMetric.increment.assert_called_with( fields={'success': False})
def testContextManagerWithUpdate(self): """Tests that context manager with a field update emits metric.""" with metrics.SuccessCounter('fooname', fields={'foo': 'bar'}) as c: c['foo'] = 'qux' self._mockMetric.increment.assert_called_with(fields={ 'foo': 'qux', 'success': True })
def main(): """tko_parse entry point.""" options, args = parse_args() # We are obliged to use indirect=False, not use the SetupTsMonGlobalState # context manager, and add a manual flush, because tko/parse is expected to # be a very short lived (<1 min) script when working effectively, and we # can't afford to either a) wait for up to 1min for metrics to flush at the # end or b) drop metrics that were sent within the last minute of execution. site_utils.SetupTsMonGlobalState('tko_parse', indirect=False, short_lived=True) try: with metrics.SuccessCounter('chromeos/autotest/tko_parse/runs'): _main_with_options(options, args) finally: metrics.Flush()
def main(): """Main method of gs_offloader.""" options = parse_options() if options.process_all: offloader_type = 'all' elif options.process_hosts_only: offloader_type = 'hosts' else: offloader_type = 'jobs' _setup_logging(options, offloader_type) if options.enable_timestamp_cache: # Extend the cache expiry time by another 1% so the timstamps # are available as the results are purged. job_timestamp_cache.setup(options.age_to_delete * 1.01) # Nice our process (carried to subprocesses) so we don't overload # the system. if not options.normal_priority: logging.debug('Set process to nice value: %d', NICENESS) os.nice(NICENESS) if psutil: proc = psutil.Process() logging.debug('Set process to ionice IDLE') proc.ionice(psutil.IOPRIO_CLASS_IDLE) # os.listdir returns relative paths, so change to where we need to # be to avoid an os.path.join on each loop. logging.debug('Offloading Autotest results in %s', RESULTS_DIR) os.chdir(RESULTS_DIR) service_name = 'gs_offloader(%s)' % offloader_type with ts_mon_config.SetupTsMonGlobalState(service_name, indirect=True, short_lived=False, debug_file=options.metrics_file): with metrics.SuccessCounter('chromeos/autotest/gs_offloader/exit'): offloader = Offloader(options) if not options.delete_only: wait_for_gs_write_access(offloader.gs_uri) while True: offloader.offload_once() if options.offload_once: break time.sleep(SLEEP_TIME_SECS)
def main(argv): """Standard main routine. @param argv Command line arguments including `sys.argv[0]`. """ arguments = _parse_command(argv) if arguments.production: metrics_manager = site_utils.SetupTsMonGlobalState('balance_pools', indirect=True) else: metrics_manager = site_utils.TrivialContextManager() with metrics_manager: with metrics.SuccessCounter('chromeos/autotest/balance_pools/runs'): end_time = time.time() start_time = end_time - 24 * 60 * 60 afe = frontend_wrappers.RetryingAFE(server=arguments.web) def balancer(pool, labels): """Balance the specified model. @param pool: The pool to rebalance for the model. @param labels: labels to restrict to balancing operations within. """ _balance_model(arguments, afe, pool, labels, start_time, end_time) _log_message('') pools = (lab_inventory.CRITICAL_POOLS if arguments.pool == _ALL_CRITICAL_POOLS else [arguments.pool]) balancer_targets = infer_balancer_targets(afe, arguments, pools) try: parallel.RunTasksInProcessPool( balancer, balancer_targets, processes=8, ) except KeyboardInterrupt: pass
def main(): """Standard main routine.""" parser = argparse.ArgumentParser( description='Update the stable repair version for all ' 'boards') parser.add_argument('-n', '--dry-run', action='store_true', help='print changes without executing them') loglib.add_logging_options(parser) # TODO(crbug/888046) Make these arguments required once puppet is updated to # pass them in. parser.add_argument('--web', default='cautotest', help='URL to the AFE to update.') arguments = parser.parse_args() loglib.configure_logging_with_args(parser, arguments) tsmon_args = { 'service_name': parser.prog, 'indirect': False, 'auto_flush': False, } if arguments.dry_run: logging.info('DRYRUN: No changes will be made.') # metrics will be logged to logging stream anyway. tsmon_args['debug_file'] = '/dev/null' try: with ts_mon_config.SetupTsMonGlobalState(**tsmon_args): with metrics.SuccessCounter(_METRICS_PREFIX + '/tick', fields={'afe': arguments.web}): _assign_stable_images(arguments) finally: metrics.Flush()
def _main(options, argv): """main method of script. Args: options: preparsed options object for the build. argv: All command line arguments to pass as list of strings. Returns: Return code of cbuildbot as an integer. """ branchname = options.branch or 'master' root = options.buildroot buildroot = os.path.join(root, 'repository') workspace = os.path.join(root, 'workspace') depot_tools_path = os.path.join(buildroot, constants.DEPOT_TOOLS_SUBPATH) # Does the entire build pass or fail. with metrics.Presence(METRIC_ACTIVE), \ metrics.SuccessCounter(METRIC_COMPLETED) as s_fields: # Preliminary set, mostly command line parsing. with metrics.SuccessCounter(METRIC_INVOKED): if options.enable_buildbot_tags: logging.EnableBuildbotMarkers() ConfigureGlobalEnvironment() # Prepare the buildroot with source for the build. with metrics.SuccessCounter(METRIC_PREP): manifest_url = config_lib.GetSiteParams().MANIFEST_INT_URL repo = repository.RepoRepository( manifest_url, buildroot, branch=branchname, git_cache_dir=options.git_cache_dir) previous_build_state = GetLastBuildState(root) # Clean up the buildroot to a safe state. with metrics.SecondsTimer(METRIC_CLEAN): build_state = GetCurrentBuildState(options, branchname) CleanBuildRoot(root, repo, options.cache_dir, build_state) # Get a checkout close enough to the branch that cbuildbot can handle it. if options.sync: with metrics.SecondsTimer(METRIC_INITIAL): InitialCheckout(repo) # Run cbuildbot inside the full ChromeOS checkout, on the specified branch. with metrics.SecondsTimer(METRIC_CBUILDBOT), \ metrics.SecondsInstanceTimer(METRIC_CBUILDBOT_INSTANCE): if previous_build_state.is_valid(): argv.append('--previous-build-state') argv.append( base64.b64encode(previous_build_state.to_json().encode( 'utf-8')).decode('utf-8')) argv.extend(['--workspace', workspace]) if not options.cache_dir_specified: argv.extend(['--cache-dir', options.cache_dir]) result = Cbuildbot(buildroot, depot_tools_path, argv) s_fields['success'] = (result == 0) build_state.status = (constants.BUILDER_STATUS_PASSED if result == 0 else constants.BUILDER_STATUS_FAILED) SetLastBuildState(root, build_state) with metrics.SecondsTimer(METRIC_CHROOT_CLEANUP): CleanupChroot(buildroot) return result
def _main(argv): """main method of script. Args: argv: All command line arguments to pass as list of strings. Returns: Return code of cbuildbot as an integer. """ options = PreParseArguments(argv) branchname = options.branch or 'master' root = options.buildroot buildroot = os.path.join(root, 'repository') depot_tools_path = os.path.join(buildroot, constants.DEPOT_TOOLS_SUBPATH) metrics_fields = { 'branch_name': branchname, 'build_config': options.build_config_name, 'tryjob': options.remote_trybot, } # Does the entire build pass or fail. with metrics.Presence(METRIC_ACTIVE, metrics_fields), \ metrics.SuccessCounter(METRIC_COMPLETED, metrics_fields) as s_fields: # Preliminary set, mostly command line parsing. with metrics.SuccessCounter(METRIC_INVOKED, metrics_fields): if options.enable_buildbot_tags: logging.EnableBuildbotMarkers() ConfigureGlobalEnvironment() # Prepare the buildroot with source for the build. with metrics.SuccessCounter(METRIC_PREP, metrics_fields): site_config = config_lib.GetConfig() manifest_url = site_config.params['MANIFEST_INT_URL'] repo = repository.RepoRepository( manifest_url, buildroot, branch=branchname, git_cache_dir=options.git_cache_dir) previous_build_state = GetLastBuildState(root) # Clean up the buildroot to a safe state. with metrics.SecondsTimer(METRIC_CLEAN, fields=metrics_fields): build_state = GetCurrentBuildState(options, branchname) CleanBuildRoot(root, repo, metrics_fields, build_state) # Get a checkout close enough to the branch that cbuildbot can handle it. if options.sync: with metrics.SecondsTimer(METRIC_INITIAL, fields=metrics_fields): InitialCheckout(repo) # Get a checkout close enough to the branch that cbuildbot can handle it. with metrics.SecondsTimer(METRIC_DEPOT_TOOLS, fields=metrics_fields): DepotToolsEnsureBootstrap(depot_tools_path) # Run cbuildbot inside the full ChromeOS checkout, on the specified branch. with metrics.SecondsTimer(METRIC_CBUILDBOT, fields=metrics_fields): if previous_build_state.is_valid(): argv.append('--previous-build-state') argv.append(base64.b64encode(previous_build_state.to_json())) result = Cbuildbot(buildroot, depot_tools_path, argv) s_fields['success'] = (result == 0) build_state.status = (constants.BUILDER_STATUS_PASSED if result == 0 else constants.BUILDER_STATUS_FAILED) SetLastBuildState(root, build_state) CleanupChroot(buildroot) return result