Пример #1
0
 def test_serialize(self):
   t = targets.DeviceTarget('reg', 'net', 'host')
   m = metrics.StringMetric('test', target=t, fields={'bar': 1})
   m.set('val', fields={'baz': False})
   p = metrics_pb2.MetricsCollection()
   m.serialize_to(p)
   e = textwrap.dedent('''\
       data {
         name: "test"
         metric_name_prefix: "/chrome/infra/"
         network_device {
           alertable: true
           realm: "ACQ_CHROME"
           metro: "reg"
           hostname: "host"
           hostgroup: "net"
         }
         fields {
           name: "bar"
           type: INT
           int_value: 1
         }
         fields {
           name: "baz"
           type: BOOL
           bool_value: false
         }
         string_value: "val"
       }
   ''')
   self.assertEquals(str(p), e)
Пример #2
0
 def test_serialze_too_many_fields(self):
   t = targets.DeviceTarget('reg', 'net', 'host')
   m = metrics.StringMetric('test', target=t,
                           fields={'a': 1, 'b': 2, 'c': 3, 'd': 4})
   m.set('val', fields={'e': 5, 'f': 6, 'g': 7})
   with self.assertRaises(errors.MonitoringTooManyFieldsError):
     m.set('val', fields={'e': 5, 'f': 6, 'g': 7, 'h': 8})
Пример #3
0
 def test_start_timestamp(self):
   t = targets.DeviceTarget('reg', 'net', 'host')
   m = metrics.CumulativeMetric(
       'test', target=t, fields={'foo': 'bar'}, time_fn=lambda: 1234)
   m.set(3.14)
   p = metrics_pb2.MetricsCollection()
   m.serialize_to(p)
   self.assertEquals(1234000000, p.data[0].start_timestamp_us)
Пример #4
0
 def test_populate_target(self):
     pb = metrics_pb2.MetricsData()
     t = targets.DeviceTarget('reg', 'net', 'host')
     t._populate_target_pb(pb)
     self.assertEquals(pb.network_device.metro, 'reg')
     self.assertEquals(pb.network_device.hostgroup, 'net')
     self.assertEquals(pb.network_device.hostname, 'host')
     self.assertEquals(pb.network_device.realm, 'ACQ_CHROME')
     self.assertEquals(pb.network_device.alertable, True)
Пример #5
0
 def test_start_timestamp(self):
   t = targets.DeviceTarget('reg', 'net', 'host')
   m = metrics.CumulativeDistributionMetric(
       'test', target=t, time_fn=lambda: 1234)
   m.add(1)
   m.add(5)
   m.add(25)
   p = metrics_pb2.MetricsCollection()
   m.serialize_to(p)
   self.assertEquals(1234000000, p.data[0].start_timestamp_us)
Пример #6
0
 def test_serialize_multiple_values(self):
   t = targets.DeviceTarget('reg', 'net', 'host')
   m = metrics.StringMetric('test', target=t)
   m.set('val1', fields={'foo': 1})
   m.set('val2', fields={'foo': 2})
   p = metrics_pb2.MetricsCollection()
   loop_action = mock.Mock()
   m.serialize_to(p, loop_action=loop_action)
   e = textwrap.dedent('''\
       data {
         name: "test"
         metric_name_prefix: "/chrome/infra/"
         network_device {
           alertable: true
           realm: "ACQ_CHROME"
           metro: "reg"
           hostname: "host"
           hostgroup: "net"
         }
         fields {
           name: "foo"
           type: INT
           int_value: 2
         }
         string_value: "val2"
       }
       data {
         name: "test"
         metric_name_prefix: "/chrome/infra/"
         network_device {
           alertable: true
           realm: "ACQ_CHROME"
           metro: "reg"
           hostname: "host"
           hostgroup: "net"
         }
         fields {
           name: "foo"
           type: INT
           int_value: 1
         }
         string_value: "val1"
       }
   ''')
   self.assertEquals(str(p), e)
   self.assertEquals(2, loop_action.call_count)
Пример #7
0
 def test_serialize_default_target(self):
   t = targets.DeviceTarget('reg', 'net', 'host')
   m = metrics.StringMetric('test')
   m.set('val')
   p = metrics_pb2.MetricsCollection()
   m.serialize_to(p, default_target=t)
   e = textwrap.dedent('''\
       data {
         name: "test"
         metric_name_prefix: "/chrome/infra/"
         network_device {
           alertable: true
           realm: "ACQ_CHROME"
           metro: "reg"
           hostname: "host"
           hostgroup: "net"
         }
         string_value: "val"
       }
   ''')
   self.assertEquals(str(p), e)
Пример #8
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:
        endpoint = args.ts_mon_endpoint
    if args.ts_mon_credentials:
        credentials = args.ts_mon_credentials

    if endpoint.startswith('file://'):
        interface.state.global_monitor = monitors.DiskMonitor(
            endpoint[len('file://'):])
    elif credentials:
        # If the flush mode is 'all' metrics will be sent immediately as they are
        # updated.  If this is the case, we mustn't set metrics while we're
        # sending metrics.
        use_instrumented_http = args.ts_mon_flush != 'all'

        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=use_instrumented_http)
        else:
            interface.state.global_monitor = monitors.ApiMonitor(
                credentials,
                endpoint,
                use_instrumented_http=use_instrumented_http)
    else:
        logging.error(
            'Monitoring is disabled because --ts-mon-credentials was not '
            'set')
        interface.state.global_monitor = monitors.NullMonitor()

    if args.ts_mon_target_type == 'device':
        interface.state.default_target = targets.DeviceTarget(
            args.ts_mon_device_region, 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.default_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()