def test_stdout_smoke(self): event = LogRequestLite.LogEventLite() event.event_time_ms = router.time_ms() event.event_code = 1 event.event_flow_id = 2 r = router._TextStreamRouter() self.assertTrue(r.push_event(event))
def test_closed_stream(self): event = LogRequestLite.LogEventLite() event.event_time_ms = router.time_ms() event.event_code = 1 event.event_flow_id = 2 stream = StringIO.StringIO() stream.close() r = router._TextStreamRouter(stream=stream) self.assertFalse(r.push_event(event))
def test_stringio_stream(self): config._cache['default_event'] = ChromeInfraEvent() log_event = infra_libs.event_mon.monitoring._get_service_event( 'START', timestamp_kind='POINT', event_timestamp=1234).log_event() stream = StringIO.StringIO() r = router._TextStreamRouter(stream=stream) self.assertTrue(r.push_event(log_event)) stream.seek(0) output = stream.read() self.assertGreater(stream.len, 10) # Minimal checking because if this is printed the rest is printed as well # unless google.protobuf is completely broken. self.assertIn('timestamp_kind: POINT', output)
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)