本文整理匯總了Python中singer.metrics.http_request_timer方法的典型用法代碼示例。如果您正苦於以下問題:Python metrics.http_request_timer方法的具體用法?Python metrics.http_request_timer怎麽用?Python metrics.http_request_timer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類singer.metrics
的用法示例。
在下文中一共展示了metrics.http_request_timer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _add_batch
# 需要導入模塊: from singer import metrics [as 別名]
# 或者: from singer.metrics import http_request_timer [as 別名]
def _add_batch(self, catalog_entry, job_id, start_date, order_by_clause=True):
endpoint = "job/{}/batch".format(job_id)
url = self.bulk_url.format(self.sf.instance_url, endpoint)
body = self.sf._build_query_string(catalog_entry, start_date, order_by_clause=order_by_clause)
headers = self._get_bulk_headers()
headers['Content-Type'] = 'text/csv'
with metrics.http_request_timer("add_batch") as timer:
timer.tags['sobject'] = catalog_entry['stream']
resp = self.sf._make_request('POST', url, headers=headers, body=body)
batch = xmltodict.parse(resp.text)
return batch['batchInfo']['id']
示例2: job_exists
# 需要導入模塊: from singer import metrics [as 別名]
# 或者: from singer.metrics import http_request_timer [as 別名]
def job_exists(self, job_id):
try:
endpoint = "job/{}".format(job_id)
url = self.bulk_url.format(self.sf.instance_url, endpoint)
headers = self._get_bulk_headers()
with metrics.http_request_timer("get_job"):
self.sf._make_request('GET', url, headers=headers)
return True # requests will raise for a 400 InvalidJob
except RequestException as ex:
if ex.response.headers["Content-Type"] == 'application/json':
exception_code = ex.response.json()['exceptionCode']
if exception_code == 'InvalidJob':
return False
raise
示例3: describe
# 需要導入模塊: from singer import metrics [as 別名]
# 或者: from singer.metrics import http_request_timer [as 別名]
def describe(self, sobject=None):
"""Describes all objects or a specific object"""
headers = self._get_standard_headers()
if sobject is None:
endpoint = "sobjects"
endpoint_tag = "sobjects"
url = self.data_url.format(self.instance_url, endpoint)
else:
endpoint = "sobjects/{}/describe".format(sobject)
endpoint_tag = sobject
url = self.data_url.format(self.instance_url, endpoint)
with metrics.http_request_timer("describe") as timer:
timer.tags['endpoint'] = endpoint_tag
resp = self._make_request('GET', url, headers=headers)
return resp.json()
# pylint: disable=no-self-use
示例4: check_bulk_quota_usage
# 需要導入模塊: from singer import metrics [as 別名]
# 或者: from singer.metrics import http_request_timer [as 別名]
def check_bulk_quota_usage(self):
endpoint = "limits"
url = self.sf.data_url.format(self.sf.instance_url, endpoint)
with metrics.http_request_timer(endpoint):
resp = self.sf._make_request('GET', url, headers=self.sf._get_standard_headers()).json()
quota_max = resp['DailyBulkApiRequests']['Max']
max_requests_for_run = int((self.sf.quota_percent_per_run * quota_max) / 100)
quota_remaining = resp['DailyBulkApiRequests']['Remaining']
percent_used = (1 - (quota_remaining / quota_max)) * 100
if percent_used > self.sf.quota_percent_total:
total_message = ("Salesforce has reported {}/{} ({:3.2f}%) total Bulk API quota " +
"used across all Salesforce Applications. Terminating " +
"replication to not continue past configured percentage " +
"of {}% total quota.").format(quota_max - quota_remaining,
quota_max,
percent_used,
self.sf.quota_percent_total)
raise TapSalesforceQuotaExceededException(total_message)
elif self.sf.jobs_completed > max_requests_for_run:
partial_message = ("This replication job has completed {} Bulk API jobs ({:3.2f}% of " +
"total quota). Terminating replication due to allotted " +
"quota of {}% per replication.").format(self.sf.jobs_completed,
(self.sf.jobs_completed / quota_max) * 100,
self.sf.quota_percent_per_run)
raise TapSalesforceQuotaExceededException(partial_message)
示例5: _create_job
# 需要導入模塊: from singer import metrics [as 別名]
# 或者: from singer.metrics import http_request_timer [as 別名]
def _create_job(self, catalog_entry, pk_chunking=False):
url = self.bulk_url.format(self.sf.instance_url, "job")
body = {"operation": "queryAll", "object": catalog_entry['stream'], "contentType": "CSV"}
headers = self._get_bulk_headers()
headers['Sforce-Disable-Batch-Retry'] = "true"
if pk_chunking:
LOGGER.info("ADDING PK CHUNKING HEADER")
headers['Sforce-Enable-PKChunking'] = "true; chunkSize={}".format(DEFAULT_CHUNK_SIZE)
# If the stream ends with 'CleanInfo' or 'History', we can PK Chunk on the object's parent
if any(catalog_entry['stream'].endswith(suffix) for suffix in ["CleanInfo", "History"]):
parent = find_parent(catalog_entry['stream'])
headers['Sforce-Enable-PKChunking'] = headers['Sforce-Enable-PKChunking'] + "; parent={}".format(parent)
with metrics.http_request_timer("create_job") as timer:
timer.tags['sobject'] = catalog_entry['stream']
resp = self.sf._make_request(
'POST',
url,
headers=headers,
body=json.dumps(body))
job = resp.json()
return job['id']
示例6: _get_batches
# 需要導入模塊: from singer import metrics [as 別名]
# 或者: from singer.metrics import http_request_timer [as 別名]
def _get_batches(self, job_id):
endpoint = "job/{}/batch".format(job_id)
url = self.bulk_url.format(self.sf.instance_url, endpoint)
headers = self._get_bulk_headers()
with metrics.http_request_timer("get_batches"):
resp = self.sf._make_request('GET', url, headers=headers)
batches = xmltodict.parse(resp.text,
xml_attribs=False,
force_list=('batchInfo',))['batchInfoList']['batchInfo']
return batches
示例7: get_batch_results
# 需要導入模塊: from singer import metrics [as 別名]
# 或者: from singer.metrics import http_request_timer [as 別名]
def get_batch_results(self, job_id, batch_id, catalog_entry):
"""Given a job_id and batch_id, queries the batches results and reads
CSV lines yielding each line as a record."""
headers = self._get_bulk_headers()
endpoint = "job/{}/batch/{}/result".format(job_id, batch_id)
url = self.bulk_url.format(self.sf.instance_url, endpoint)
with metrics.http_request_timer("batch_result_list") as timer:
timer.tags['sobject'] = catalog_entry['stream']
batch_result_resp = self.sf._make_request('GET', url, headers=headers)
# Returns a Dict where input:
# <result-list><result>1</result><result>2</result></result-list>
# will return: {'result', ['1', '2']}
batch_result_list = xmltodict.parse(batch_result_resp.text,
xml_attribs=False,
force_list={'result'})['result-list']
for result in batch_result_list['result']:
endpoint = "job/{}/batch/{}/result/{}".format(job_id, batch_id, result)
url = self.bulk_url.format(self.sf.instance_url, endpoint)
headers['Content-Type'] = 'text/csv'
with tempfile.NamedTemporaryFile(mode="w+", encoding="utf8") as csv_file:
resp = self.sf._make_request('GET', url, headers=headers, stream=True)
for chunk in resp.iter_content(chunk_size=ITER_CHUNK_SIZE, decode_unicode=True):
if chunk:
# Replace any NULL bytes in the chunk so it can be safely given to the CSV reader
csv_file.write(chunk.replace('\0', ''))
csv_file.seek(0)
csv_reader = csv.reader(csv_file,
delimiter=',',
quotechar='"')
column_name_list = next(csv_reader)
for line in csv_reader:
rec = dict(zip(column_name_list, line))
yield rec
示例8: _close_job
# 需要導入模塊: from singer import metrics [as 別名]
# 或者: from singer.metrics import http_request_timer [as 別名]
def _close_job(self, job_id):
endpoint = "job/{}".format(job_id)
url = self.bulk_url.format(self.sf.instance_url, endpoint)
body = {"state": "Closed"}
with metrics.http_request_timer("close_job"):
self.sf._make_request(
'POST',
url,
headers=self._get_bulk_headers(),
body=json.dumps(body))
# pylint: disable=no-self-use
示例9: request
# 需要導入模塊: from singer import metrics [as 別名]
# 或者: from singer.metrics import http_request_timer [as 別名]
def request(self, tap_stream_id, *args, **kwargs):
wait = (self.next_request_at - datetime.now()).total_seconds()
if wait > 0:
time.sleep(wait)
with metrics.http_request_timer(tap_stream_id) as timer:
response = self.send(*args, **kwargs)
self.next_request_at = datetime.now() + TIME_BETWEEN_REQUESTS
timer.tags[metrics.Tag.http_status_code] = response.status_code
if response.status_code == 429:
raise RateLimitException()
response.raise_for_status()
return response.json()
示例10: request
# 需要導入模塊: from singer import metrics [as 別名]
# 或者: from singer.metrics import http_request_timer [as 別名]
def request(url, params=None):
params = params or {}
hapikey = CONFIG['hapikey']
if hapikey is None:
if CONFIG['token_expires'] is None or CONFIG['token_expires'] < datetime.datetime.utcnow():
acquire_access_token_from_refresh_token()
headers = {'Authorization': 'Bearer {}'.format(CONFIG['access_token'])}
else:
params['hapikey'] = hapikey
headers = {}
if 'user_agent' in CONFIG:
headers['User-Agent'] = CONFIG['user_agent']
req = requests.Request('GET', url, params=params, headers=headers).prepare()
LOGGER.info("GET %s", req.url)
with metrics.http_request_timer(parse_source_from_url(url)) as timer:
resp = SESSION.send(req)
timer.tags[metrics.Tag.http_status_code] = resp.status_code
if resp.status_code == 403:
raise SourceUnavailableException(resp.content)
else:
resp.raise_for_status()
return resp
# {"bookmarks" : {"contacts" : { "lastmodifieddate" : "2001-01-01"
# "offset" : {"vidOffset": 1234
# "timeOffset": "3434434 }}
# "users" : { "timestamp" : "2001-01-01"}}
# "currently_syncing" : "contacts"
# }
# }
示例11: get_page
# 需要導入模塊: from singer import metrics [as 別名]
# 或者: from singer.metrics import http_request_timer [as 別名]
def get_page(sdk_client, selector, stream, start_index):
service_caller = get_service_caller(sdk_client, stream)
selector = set_index(selector, start_index)
with metrics.http_request_timer(stream):
LOGGER.info("Request %s %s for customer %s with startIndex %s using selector %s",
PAGE_SIZE,
stream,
sdk_client.client_customer_id,
selector['paging']['startIndex'],
hash(str(selector)))
page = attempt_get_from_service(service_caller, selector)
return page
#pylint: disable=too-many-return-statements
示例12: authed_get
# 需要導入模塊: from singer import metrics [as 別名]
# 或者: from singer.metrics import http_request_timer [as 別名]
def authed_get(source, url, headers={}):
with metrics.http_request_timer(source) as timer:
session.headers.update(headers)
resp = session.request(method='get', url=url)
if resp.status_code == 401:
raise AuthException(resp.text)
if resp.status_code == 403:
raise AuthException(resp.text)
if resp.status_code == 404:
raise NotFoundException(resp.text)
timer.tags[metrics.Tag.http_status_code] = resp.status_code
return resp
示例13: test_success
# 需要導入模塊: from singer import metrics [as 別名]
# 或者: from singer.metrics import http_request_timer [as 別名]
def test_success(self, log):
timer = metrics.http_request_timer('users')
timer.elapsed = lambda: 0
with timer:
pass
got = logged_points(log)
self.assertEqual(
[metrics.Point('timer', 'http_request_duration', 0, {'endpoint': 'users', 'status': 'succeeded'})],
got)
示例14: test_success_with_http_status_code
# 需要導入模塊: from singer import metrics [as 別名]
# 或者: from singer.metrics import http_request_timer [as 別名]
def test_success_with_http_status_code(self, log):
with metrics.http_request_timer('users') as timer:
timer.elapsed = lambda: 0
timer.tags[metrics.Tag.http_status_code] = 200
self.assertEqual(
[metrics.Point('timer', 'http_request_duration', 0, {'endpoint': 'users', 'status': 'succeeded', 'http_status_code': 200})],
logged_points(log))
示例15: test_failure
# 需要導入模塊: from singer import metrics [as 別名]
# 或者: from singer.metrics import http_request_timer [as 別名]
def test_failure(self, log):
try:
with metrics.http_request_timer('users') as timer:
timer.elapsed = lambda: 0
timer.tags[metrics.Tag.http_status_code] = 400
raise ValueError('foo is not bar')
except ValueError:
pass
self.assertEqual(
[metrics.Point('timer', 'http_request_duration', 0, {'endpoint': 'users', 'status': 'failed', 'http_status_code': 400})],
logged_points(log))