Пример #1
0
    def test_close_stops_flush_thread(self):
        interface.state.flush_thread = interface._FlushThread(10)
        interface.state.flush_thread.start()

        self.assertTrue(interface.state.flush_thread.is_alive())
        interface.close()
        self.assertFalse(interface.state.flush_thread.is_alive())
Пример #2
0
  def test_close_stops_flush_thread(self):
    interface.state.flush_thread = interface._FlushThread(10)
    interface.state.flush_thread.start()

    self.assertTrue(interface.state.flush_thread.is_alive())
    interface.close()
    self.assertFalse(interface.state.flush_thread.is_alive())
Пример #3
0
    def setUp(self):
        mock.patch('infra_libs.ts_mon.common.interface.flush',
                   autospec=True).start()
        mock.patch('time.time', autospec=True).start()

        self.fake_time = 0
        time.time.side_effect = lambda: self.fake_time

        self.stop_event = FakeThreadingEvent()
        self.stop_event.increment_time_func = self.increment_time

        self.t = interface._FlushThread(60, stop_event=self.stop_event)
Пример #4
0
  def setUp(self):
    mock.patch('infra_libs.ts_mon.common.interface.flush',
               autospec=True).start()
    mock.patch('time.time', autospec=True).start()

    self.fake_time = 0
    time.time.side_effect = lambda: self.fake_time

    self.stop_event = FakeThreadingEvent()
    self.stop_event.increment_time_func = self.increment_time

    self.t = interface._FlushThread(60, stop_event=self.stop_event)
Пример #5
0
def process_argparse_options(args):
    """Process command line arguments to initialize the global monitor.

  Also initializes the default target.

  Starts a background thread to automatically flush monitoring metrics if not
  disabled by command line arguments.

  Args:
    args (argparse.Namespace): the result of parsing the command line arguments
  """
    # Parse the config file if it exists.
    config = load_machine_config(args.ts_mon_config_file)
    endpoint = config.get('endpoint', '')
    credentials = config.get('credentials', '')
    autogen_hostname = config.get('autogen_hostname', False)

    # Command-line args override the values in the config file.
    if args.ts_mon_endpoint is not None:
        endpoint = args.ts_mon_endpoint
    if args.ts_mon_credentials is not None:
        credentials = args.ts_mon_credentials

    if args.ts_mon_target_type == 'device':
        hostname = args.ts_mon_device_hostname
        if args.ts_mon_autogen_hostname or autogen_hostname:
            hostname = 'autogen:' + hostname
        interface.state.target = targets.DeviceTarget(
            args.ts_mon_device_region, args.ts_mon_device_role,
            args.ts_mon_device_network, hostname)
    if args.ts_mon_target_type == 'task':
        # Reimplement ArgumentParser.error, since we don't have access to the parser
        if not args.ts_mon_task_service_name:
            print >> sys.stderr, (
                'Argument --ts-mon-task-service-name must be '
                'provided when the target type is "task".')
            sys.exit(2)
        if not args.ts_mon_task_job_name:
            print >> sys.stderr, (
                'Argument --ts-mon-task-job-name must be provided '
                'when the target type is "task".')
            sys.exit(2)
        hostname = args.ts_mon_task_hostname
        if args.ts_mon_autogen_hostname or autogen_hostname:
            hostname = 'autogen:' + hostname
        interface.state.target = targets.TaskTarget(
            args.ts_mon_task_service_name, args.ts_mon_task_job_name,
            args.ts_mon_task_region, hostname, args.ts_mon_task_number)

    interface.state.metric_name_prefix = args.ts_mon_metric_name_prefix
    interface.state.global_monitor = monitors.NullMonitor()

    if endpoint.startswith('file://'):
        interface.state.global_monitor = monitors.DebugMonitor(
            endpoint[len('file://'):])
    elif endpoint.startswith('https://'):
        interface.state.global_monitor = monitors.HttpsMonitor(
            endpoint,
            monitors.CredentialFactory.from_string(credentials),
            ca_certs=args.ts_mon_ca_certs)
    elif endpoint.lower() == 'none' or not endpoint:
        logging.info('ts_mon monitoring has been explicitly disabled')
    else:
        logging.error(
            'ts_mon monitoring is disabled because the endpoint provided'
            ' is invalid or not supported: %s', endpoint)

    interface.state.flush_mode = args.ts_mon_flush

    if args.ts_mon_flush == 'auto':
        interface.state.flush_thread = interface._FlushThread(
            args.ts_mon_flush_interval_secs)
        interface.state.flush_thread.start()

    standard_metrics.init()
Пример #6
0
def process_argparse_options(args):
  """Process command line arguments to initialize the global monitor.

  Also initializes the default target.

  Starts a background thread to automatically flush monitoring metrics if not
  disabled by command line arguments.

  Args:
    args (argparse.Namespace): the result of parsing the command line arguments
  """
  # Parse the config file if it exists.
  config = load_machine_config(args.ts_mon_config_file)
  endpoint = config.get('endpoint', '')
  credentials = config.get('credentials', '')

  # Command-line args override the values in the config file.
  if args.ts_mon_endpoint is not None:
    endpoint = args.ts_mon_endpoint
  if args.ts_mon_credentials is not None:
    credentials = args.ts_mon_credentials

  if args.ts_mon_target_type == 'device':
    hostname = args.ts_mon_device_hostname
    if args.ts_mon_autogen_hostname:
      hostname = 'autogen:' + hostname
    interface.state.target = targets.DeviceTarget(
        args.ts_mon_device_region,
        args.ts_mon_device_role,
        args.ts_mon_device_network,
        hostname)
  if args.ts_mon_target_type == 'task':
    # Reimplement ArgumentParser.error, since we don't have access to the parser
    if not args.ts_mon_task_service_name:
      print >> sys.stderr, ('Argument --ts-mon-task-service-name must be '
                            'provided when the target type is "task".')
      sys.exit(2)
    if not args.ts_mon_task_job_name:
      print >> sys.stderr, ('Argument --ts-mon-task-job-name must be provided '
                            'when the target type is "task".')
      sys.exit(2)
    hostname = args.ts_mon_task_hostname
    if args.ts_mon_autogen_hostname:
      hostname = 'autogen:' + hostname
    interface.state.target = targets.TaskTarget(
        args.ts_mon_task_service_name,
        args.ts_mon_task_job_name,
        args.ts_mon_task_region,
        hostname,
        args.ts_mon_task_number)

  interface.state.global_monitor = monitors.NullMonitor()

  if endpoint.startswith('file://'):
    interface.state.global_monitor = monitors.DebugMonitor(
        endpoint[len('file://'):])
  elif endpoint.startswith('pubsub://'):
    if credentials:
      url = urlparse.urlparse(endpoint)
      project = url.netloc
      topic = url.path.strip('/')
      interface.state.global_monitor = monitors.PubSubMonitor(
          credentials, project, topic, use_instrumented_http=True)
    else:
      logging.error('ts_mon monitoring is disabled because credentials are not '
                    'available')
  elif endpoint.lower() == 'none':
    logging.info('ts_mon monitoring has been explicitly disabled')
  else:
    logging.error('ts_mon monitoring is disabled because the endpoint provided'
                  ' is invalid or not supported: %s', endpoint)

  interface.state.flush_mode = args.ts_mon_flush

  if args.ts_mon_flush == 'auto':
    interface.state.flush_thread = interface._FlushThread(
        args.ts_mon_flush_interval_secs)
    interface.state.flush_thread.start()

  standard_metrics.init()
Пример #7
0
def process_argparse_options(args):
  """Process command line arguments to initialize the global monitor.

  Also initializes the default target if sufficient arguments are supplied.
  If they aren't, all created metrics will have to supply their own target.
  This is generally a bad idea, as many libraries rely on the default target
  being set up.

  Starts a background thread to automatically flush monitoring metrics if not
  disabled by command line arguments.

  Args:
    args (argparse.Namespace): the result of parsing the command line arguments
  """

  # Parse the config file if it exists.
  config = load_machine_config(args.ts_mon_config_file)
  endpoint = config.get('endpoint', '')
  credentials = config.get('credentials', '')

  # Command-line args override the values in the config file.
  if args.ts_mon_endpoint is not None:
    endpoint = args.ts_mon_endpoint
  if args.ts_mon_credentials is not None:
    credentials = args.ts_mon_credentials

  interface.state.global_monitor = monitors.NullMonitor()

  if endpoint.startswith('file://'):
    interface.state.global_monitor = monitors.DebugMonitor(
        endpoint[len('file://'):])
  elif credentials:
    if endpoint.startswith('pubsub://'):
      url = urlparse.urlparse(endpoint)
      project = url.netloc
      topic = url.path.strip('/')
      interface.state.global_monitor = monitors.PubSubMonitor(
          credentials, project, topic, use_instrumented_http=True)
    else:
      logging.error('Monitoring is disabled because the endpoint provided is '
                    'invalid or not supported: %s', endpoint)
  else:
    logging.error('Monitoring is disabled because credentials are not '
                  'available')

  if args.ts_mon_target_type == 'device':
    interface.state.target = targets.DeviceTarget(
        args.ts_mon_device_region,
        args.ts_mon_device_role,
        args.ts_mon_device_network,
        args.ts_mon_device_hostname)
  if args.ts_mon_target_type == 'task':  # pragma: no cover
    # Reimplement ArgumentParser.error, since we don't have access to the parser
    if not args.ts_mon_task_service_name:
      print >> sys.stderr, ('Argument --ts-mon-task-service-name must be '
                            'provided when the target type is "task".')
      sys.exit(2)
    if not args.ts_mon_task_job_name:  # pragma: no cover
      print >> sys.stderr, ('Argument --ts-mon-task-job-name must be provided '
                            'when the target type is "task".')
      sys.exit(2)
    interface.state.target = targets.TaskTarget(
        args.ts_mon_task_service_name,
        args.ts_mon_task_job_name,
        args.ts_mon_task_region,
        args.ts_mon_task_hostname,
        args.ts_mon_task_number)

  interface.state.flush_mode = args.ts_mon_flush

  if args.ts_mon_flush == 'auto':
    interface.state.flush_thread = interface._FlushThread(
        args.ts_mon_flush_interval_secs)
    interface.state.flush_thread.start()

  standard_metrics.init()