def setup_monitoring(run_type='dry', hostname=None, service_name=None, appengine_name=None, service_account_creds=None, service_accounts_creds_root=None): """Initializes event monitoring. This function is mainly used to provide default global values which are required for the module to work. If you're implementing a command-line tool, use process_argparse_options instead. Args: run_type (str): One of 'dry', 'test', or 'prod'. Do respectively nothing, hit the testing endpoint and the production endpoint. hostname (str): hostname as it should appear in the event. If not provided a default value is computed. service_name (str): logical name of the service that emits events. e.g. "commit_queue". appengine_name (str): name of the appengine app, if running on appengine. service_account_creds (str): path to a json file containing a service account's credentials obtained from a Google Cloud project. **Path is relative to service_account_creds_root**, which is not the current path by default. See infra_libs.authentication for details. service_account_creds_root (str): path containing credentials files. """ global _router logging.debug('event_mon: setting up monitoring.') if not _router: # pragma: no cover default_event = ChromeInfraEvent() hostname = hostname or socket.getfqdn() # hostname might be empty string or None on some systems, who knows. if hostname: # pragma: no branch default_event.event_source.host_name = hostname else: logging.warning('event_mon: unable to determine hostname.') if service_name: default_event.event_source.service_name = service_name if appengine_name: default_event.event_source.appengine_name = appengine_name cache['default_event'] = default_event cache['service_account_creds'] = service_account_creds cache['service_accounts_creds_root'] = service_accounts_creds_root if run_type not in ENDPOINTS: logging.error('Unknown run_type (%s). Setting to "dry"', run_type) endpoint = ENDPOINTS.get(run_type) _router = _Router(cache, endpoint=endpoint)
def test_push_smoke(self): r = router._Router({}, endpoint=None) req = LogRequestLite.LogEventLite() req.event_time_ms = router.time_ms() req.event_code = 1 req.event_flow_id = 2 r.push_event(req) self.assertTrue(r.close())
def test_push_error_handling(self): r = router._Router({}, endpoint=None) r.push_event(None) self.assertTrue(r.close())
def test_smoke_with_credentials(self): cache = {'service_account_creds': os.path.join(DATA_DIR, 'valid_creds.json'), 'service_accounts_creds_root': 'whatever.the/other/is/absolute'} r = router._Router(cache, endpoint='https://any.where') self.assertTrue(r.close())
def test_smoke(self): # Use dry_run to avoid code that deals with http (including auth). r = router._Router({}, endpoint=None) self.assertTrue(r.close())