Exemplo n.º 1
0
 def setUp(self):
   super(InstrumentedHttplib2Test, self).setUp()
   self.mock_time = mock.create_autospec(time.time, spec_set=True)
   self.mock_time.return_value = 42
   self.http = infra_libs.InstrumentedHttp('test', time_fn=self.mock_time)
   self.http._request = mock.Mock()
   ts_mon.reset_for_unittest()
Exemplo n.º 2
0
 def _initialize(self, credsfile, project, topic, use_instrumented_http):
     # Copied from acquisition_api.AcquisitionCredential.Load.
     creds = self._load_credentials(credsfile)
     if use_instrumented_http:
         self._http = infra_libs.InstrumentedHttp('acq-mon-api-pubsub')
     else:  # pragma: no cover
         self._http = httplib2.Http()
     creds.authorize(self._http)
     self._api = discovery.build('pubsub', 'v1', http=self._http)
     self._topic = 'projects/%s/topics/%s' % (project, topic)
Exemplo n.º 3
0
def _create_http(credentials_db):
    token_expiry = datetime.datetime.now() + datetime.timedelta(minutes=15)
    credentials = creds_service.get_credentials(credentials_db,
                                                'bugdroid',
                                                token_expiry=token_expiry)

    http = infra_libs.InstrumentedHttp('gcs')
    http = infra_libs.RetriableHttp(http,
                                    retrying_statuses_fn=lambda x: x >= 400)
    http = credentials.authorize(http)
    return http
Exemplo n.º 4
0
    def __init__(self,
                 cache,
                 endpoint,
                 timeout=10,
                 try_num=3,
                 retry_backoff=2.,
                 dry_run=False,
                 _sleep_fn=time.sleep):
        """Initialize the router.

    Args:
      cache(dict): This must be config._cache. Passed as a parameter to
        avoid a circular import.
      endpoint(str or None): None means 'dry run'. What would be sent is printed
        on stdout. If endpoint starts with 'https://' data is POSTed there.
        Otherwise it is interpreted as a file where to write serialized
        LogEventLite protos.
      try_num(int): max number of http requests send to the endpoint.
      retry_backoff(float): time in seconds before retrying posting to the
         endpoint. Randomized exponential backoff is applied on subsequent
         retries.
      dry_run(boolean): if True, no http request is sent. Instead a message is
         printed.
      _sleep_fn (function): function to wait specified number of seconds. This
        argument is provided for testing purposes.
    """
        HTTP_IDENTIFIER = 'event_mon'
        _Router.__init__(self)
        self.endpoint = endpoint
        self.try_num = try_num
        self.retry_backoff = retry_backoff
        self._cache = cache
        self._http = infra_libs.InstrumentedHttp(HTTP_IDENTIFIER,
                                                 timeout=timeout)
        self._dry_run = dry_run
        self._sleep_fn = _sleep_fn

        # TODO(pgervais) pass this as parameters instead.
        if self._cache.get('service_account_creds'):
            try:
                logging.debug('Activating OAuth2 authentication.')
                self._http = infra_libs.get_authenticated_http(
                    self._cache['service_account_creds'],
                    service_accounts_creds_root=self.
                    _cache['service_accounts_creds_root'],
                    scope='https://www.googleapis.com/auth/cclog',
                    http_identifier=HTTP_IDENTIFIER,
                    timeout=timeout)
            except IOError:
                logging.error(
                    'Unable to read credentials, requests will be '
                    'unauthenticated. File: %s',
                    self._cache.get('service_account_creds'))
Exemplo n.º 5
0
    def __init__(self, credsfile, endpoint, use_instrumented_http=True):
        """Process monitoring related command line flags and initialize api.

    Args:
      credsfile (str): path to the credentials json file
      endpoint (str): url of the monitoring endpoint to hit
    """

        if credsfile == GCE_CREDENTIALS:
            raise NotImplementedError(
                'GCE service accounts are not supported for ApiMonitor')

        creds = acquisition_api.AcquisitionCredential.Load(
            os.path.abspath(credsfile))
        api = acquisition_api.AcquisitionApi(creds, endpoint)
        api.SetResponseCallback(_logging_callback)

        if use_instrumented_http:
            api.SetHttp(infra_libs.InstrumentedHttp('acq-mon-api'))

        self._api = api