def test_events_are_logged_with_specified_severity(self, log_mock): logger = router._LoggingStreamRouter(logging.WARN) event = LogRequestLite.LogEventLite() event.event_time_ms = router.time_ms() event.event_code = 1 event.event_flow_id = 2 self.assertTrue(logger.push_event(event)) self.assertEqual(log_mock.call_args[0], (logging.WARN, mock.ANY))
def test_fails_to_log(self, exception_mock): logger = router._LoggingStreamRouter(logging.WARN) event = LogRequestLite.LogEventLite() event.event_time_ms = router.time_ms() event.event_code = 1 event.event_flow_id = 2 event.source_extension = 'not-a-message' self.assertFalse(logger.push_event(event)) self.assertEqual(exception_mock.call_args[0], ('Unable to log the events',))
def test_fails_to_log(self, exception_mock): logger = router._LoggingStreamRouter(logging.WARN) event = LogRequestLite.LogEventLite() event.event_time_ms = router.time_ms() event.event_code = 1 event.event_flow_id = 2 event.source_extension = 'not-a-message' self.assertFalse(logger.push_event(event)) self.assertEqual(exception_mock.call_args[0], ('Unable to log the events', ))
def test_events_are_logged_correctly(self, log_mock): logger = router._LoggingStreamRouter() events = [] for i in range(3): event = LogRequestLite.LogEventLite() event.event_time_ms = router.time_ms() event.event_code = 1 event.event_flow_id = 2 infra_event = ChromeInfraEvent() infra_event.cq_event.issue = str(i + 1) event.source_extension = infra_event.SerializeToString() events.append(event) self.assertTrue(logger.push_event(events)) expected_calls = [ ((logging.INFO, 'Sending event_mon event:\ncq_event {\n issue: "1"\n}\n'),), ((logging.INFO, 'Sending event_mon event:\ncq_event {\n issue: "2"\n}\n'),), ((logging.INFO, 'Sending event_mon event:\ncq_event {\n issue: "3"\n}\n'),)] self.assertEqual(log_mock.call_args_list, expected_calls)
def test_events_are_logged_correctly(self, log_mock): logger = router._LoggingStreamRouter() events = [] for i in range(3): event = LogRequestLite.LogEventLite() event.event_time_ms = router.time_ms() event.event_code = 1 event.event_flow_id = 2 infra_event = ChromeInfraEvent() infra_event.cq_event.issue = str(i + 1) event.source_extension = infra_event.SerializeToString() events.append(event) self.assertTrue(logger.push_event(events)) expected_calls = [ ((logging.INFO, 'Sending event_mon event:\ncq_event {\n issue: "1"\n}\n'), ), ((logging.INFO, 'Sending event_mon event:\ncq_event {\n issue: "2"\n}\n'), ), ((logging.INFO, 'Sending event_mon event:\ncq_event {\n issue: "3"\n}\n'), ) ] self.assertEqual(log_mock.call_args_list, expected_calls)
def setup_monitoring(run_type='dry', hostname=None, service_name=None, appengine_name=None, output_file=None, dry_run=False, http_timeout=10, http_retry_backoff=2.): """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. output_file (str): file where to write the output in run_type == 'file' mode. dry_run (bool): if True, the code has no side-effect, what would have been done is printed instead. http_timeout (int): timeout in seconds for HTTP requests to send events. http_retry_backoff (float): time in seconds before retrying POSTing to the HTTP endpoint. Randomized exponential backoff is applied on subsequent retries. """ global _router logging.debug('event_mon: setting up monitoring.') if _router: return 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: # pragma: no cover 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 if run_type not in RUNTYPES: logging.error('Unknown run_type (%s). Setting to "dry"', run_type) run_type = 'dry' if run_type == 'dry': # If we are running on AppEngine or devserver, use logging module. server_software = os.environ.get('SERVER_SOFTWARE', '') if (server_software.startswith('Google App Engine') or server_software.startswith('Development')): _router = ev_router._LoggingStreamRouter() else: _router = ev_router._TextStreamRouter() elif run_type == 'file': _router = ev_router._LocalFileRouter(output_file, dry_run=dry_run) else: _router = ev_router._HttpRouter(_cache, ENDPOINTS.get(run_type), dry_run=dry_run, timeout=http_timeout, retry_backoff=http_retry_backoff)
def setup_monitoring(run_type='dry', hostname=None, service_name=None, appengine_name=None, service_account_creds=None, service_accounts_creds_root=None, output_file=None, dry_run=False): """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. output_file (str): file where to write the output in run_type == 'file' mode. dry_run (bool): if True, the code has no side-effect, what would have been done is printed instead. """ global _router logging.debug('event_mon: setting up monitoring.') if not _router: 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: # pragma: no cover 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 if run_type in ('prod', 'test'): _cache['service_account_creds'] = service_account_creds _cache['service_accounts_creds_root'] = service_accounts_creds_root else: _cache['service_account_creds'] = None _cache['service_accounts_creds_root'] = None if run_type not in RUNTYPES: logging.error('Unknown run_type (%s). Setting to "dry"', run_type) run_type = 'dry' if run_type == 'dry': # If we are running on AppEngine or devserver, use logging module. server_software = os.environ.get('SERVER_SOFTWARE', '') if (server_software.startswith('Google App Engine') or server_software.startswith('Development')): _router = ev_router._LoggingStreamRouter() else: _router = ev_router._TextStreamRouter() elif run_type == 'file': _router = ev_router._LocalFileRouter(output_file, dry_run=dry_run) else: _router = ev_router._HttpRouter(_cache, ENDPOINTS.get(run_type), dry_run=dry_run)