Exemple #1
0
    def test_send(self, _psmonitor):
        mon = monitors.PubSubMonitor('/path/to/creds.p8.json', 'myproject',
                                     'mytopic')
        mon._api = mock.MagicMock()
        mon._topic = 'mytopic'

        metric1 = metrics_pb2.MetricsData(name='m1')
        mon.send(metric1)
        metric2 = metrics_pb2.MetricsData(name='m2')
        mon.send([metric1, metric2])
        collection = metrics_pb2.MetricsCollection(data=[metric1, metric2])
        mon.send(collection)

        def message(pb):
            pb = monitors.Monitor._wrap_proto(pb)
            return {
                'messages': [{
                    'data': base64.b64encode(pb.SerializeToString())
                }]
            }

        publish = mon._api.projects.return_value.topics.return_value.publish
        publish.assert_has_calls([
            mock.call(topic='mytopic', body=message(metric1)),
            mock.call().execute(num_retries=5),
            mock.call(topic='mytopic', body=message([metric1, metric2])),
            mock.call().execute(num_retries=5),
            mock.call(topic='mytopic', body=message(collection)),
            mock.call().execute(num_retries=5),
        ])
Exemple #2
0
    def test_init_gce_credential(self, aac, discovery, instrumented_http):
        creds = aac.return_value
        http_mock = instrumented_http.return_value
        mon = monitors.PubSubMonitor(':gce', 'myproject', 'mytopic')

        aac.assert_called_once_with(monitors.PubSubMonitor._SCOPES)
        creds.authorize.assert_called_once_with(http_mock)
        discovery.build.assert_called_once_with('pubsub', 'v1', http=http_mock)
        self.assertEquals(mon._topic, 'projects/myproject/topics/mytopic')
Exemple #3
0
    def test_init_service_account(self, gc, discovery, instrumented_http):
        m_open = mock.mock_open(read_data='{"type": "service_account"}')
        creds = gc.from_stream.return_value
        scoped_creds = creds.create_scoped.return_value
        http_mock = instrumented_http.return_value
        with mock.patch('infra_libs.ts_mon.monitors.open', m_open,
                        create=True):
            mon = monitors.PubSubMonitor('/path/to/creds.p8.json', 'myproject',
                                         'mytopic')

        m_open.assert_called_once_with('/path/to/creds.p8.json', 'r')
        creds.create_scoped.assert_called_once_with(
            monitors.PubSubMonitor._SCOPES)
        scoped_creds.authorize.assert_called_once_with(http_mock)
        discovery.build.assert_called_once_with('pubsub', 'v1', http=http_mock)
        self.assertEquals(mon._topic, 'projects/myproject/topics/mytopic')
Exemple #4
0
    def test_init_storage(self, storage, discovery, instrumented_http):
        storage_inst = mock.Mock()
        storage.return_value = storage_inst
        creds = storage_inst.get.return_value

        m_open = mock.mock_open(read_data='{}')
        http_mock = instrumented_http.return_value
        with mock.patch('infra_libs.ts_mon.monitors.open', m_open,
                        create=True):
            mon = monitors.PubSubMonitor('/path/to/creds.p8.json', 'myproject',
                                         'mytopic')

        m_open.assert_called_once_with('/path/to/creds.p8.json', 'r')
        storage_inst.get.assert_called_once_with()
        creds.authorize.assert_called_once_with(http_mock)
        discovery.build.assert_called_once_with('pubsub', 'v1', http=http_mock)
        self.assertEquals(mon._topic, 'projects/myproject/topics/mytopic')
Exemple #5
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()