From 52e6c4ad41ab5ea6fd3331cd22e4efb49fda827f Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Thu, 31 Oct 2019 17:15:53 -0700 Subject: [PATCH] sheetsync, cutter: Collect metrics on http calls In particular, to google apis. --- common/common/googleapis.py | 8 ++++++-- cutter/cutter/upload_backends.py | 4 +++- sheetsync/sheetsync/sheets.py | 2 ++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/common/common/googleapis.py b/common/common/googleapis.py index b2b898d..51c59df 100644 --- a/common/common/googleapis.py +++ b/common/common/googleapis.py @@ -2,7 +2,11 @@ import time import gevent -import requests + +from .requests import InstrumentedSession + +# Wraps all requests in some metric collection +requests = InstrumentedSession() class GoogleAPIClient(object): @@ -40,7 +44,7 @@ class GoogleAPIClient(object): 'client_secret': self.client_secret, 'refresh_token': self.refresh_token, 'grant_type': 'refresh_token', - }) + }, metric_name='get_access_token') resp.raise_for_status() data = resp.json() self._access_token = data['access_token'] diff --git a/cutter/cutter/upload_backends.py b/cutter/cutter/upload_backends.py index d9ab1b0..734c4a6 100644 --- a/cutter/cutter/upload_backends.py +++ b/cutter/cutter/upload_backends.py @@ -143,13 +143,14 @@ class Youtube(UploadBackend): 'uploadType': 'resumable', }, json=json, + metric_name='create_video', ) if not resp.ok: # Don't retry, because failed calls still count against our upload quota. # The risk of repeated failed attempts blowing through our quota is too high. raise UploadError("Youtube create video call failed with {resp.status_code}: {resp.content}".format(resp=resp)) upload_url = resp.headers['Location'] - resp = self.client.request('POST', upload_url, data=data) + resp = self.client.request('POST', upload_url, data=data, metric_name='upload_video') if 400 <= resp.status_code < 500: # As above, don't retry. But with 4xx's we know the upload didn't go through. # On a 5xx, we can't be sure (the server is in an unspecified state). @@ -169,6 +170,7 @@ class Youtube(UploadBackend): 'part': 'id,status', 'id': ','.join(group), }, + metric_name='list_videos', ) resp.raise_for_status() for item in resp.json()['items']: diff --git a/sheetsync/sheetsync/sheets.py b/sheetsync/sheetsync/sheets.py index 63386b2..3294bf6 100644 --- a/sheetsync/sheetsync/sheets.py +++ b/sheetsync/sheetsync/sheets.py @@ -23,6 +23,7 @@ class Sheets(object): 'https://sheets.googleapis.com/v4/spreadsheets/{}/values/{}'.format( spreadsheet_id, range, ), + metric_name='get_rows', ) resp.raise_for_status() data = resp.json() @@ -46,6 +47,7 @@ class Sheets(object): "range": range, "values": [[value]], }, + metric_name='write_value', ) resp.raise_for_status()