Skip to content

Commit

Permalink
Log http requests (silent by default)
Browse files Browse the repository at this point in the history
- Critical for debugging issues like googledatalab#195 and googledatalab#220 (I've been using this locally)
- Silent by default, to avoid bothering users
- To enable, use standard logging idioms like `logging.basicConfig`, `logging.fileConfig`, etc.

Example with no logging (default):
```py
>>> import datalab.bigquery as bq
>>> bq.Query("select 3").to_dataframe()

Your active configuration is: [foo]

   f0_
0    3
```

Example with logging (opt in):
```py
>>> import logging
>>> logging.basicConfig(level=logging.DEBUG)
>>> import datalab.bigquery as bq
>>> print(bq.Query("select 3").to_dataframe())
Your active configuration is: [foo]

DEBUG:datalab.utils._http:request: method[POST], url[https://www.googleapis.com/bigquery/v2/projects/dwh-v2/jobs/], body[{"kind": "bigquery#job", "configuration": {"query": {"query": "select 3", "useQueryCache": true, "allowLargeResults": false, "useLegacySql": true, "userDefinedFunctionResources": []}, "dryRun": false, "priority": "INTERACTIVE"}}]
INFO:oauth2client.client:Attempting refresh to obtain initial access_token
INFO:oauth2client.client:Refreshing access_token
DEBUG:datalab.utils._http:request: method[GET], url[https://www.googleapis.com/bigquery/v2/projects/dwh-v2/queries/job_u67WYzV6RCbO4F-C5JB7hRocdxA?maxResults=0&timeoutMs=30000&startIndex=0], body[None]
DEBUG:datalab.utils._http:request: method[GET], url[https://www.googleapis.com/bigquery/v2/projects/dwh-v2/jobs/job_u67WYzV6RCbO4F-C5JB7hRocdxA], body[None]
DEBUG:datalab.utils._http:request: method[GET], url[https://www.googleapis.com/bigquery/v2/projects/dwh-v2/datasets/_2f96775300d8858559d2bd23c05bad0392345e30/tables/anonda2cd79fe2c683f6e17ec63437a72c0e2144c829], body[None]
DEBUG:datalab.utils._http:request: method[GET], url[https://www.googleapis.com/bigquery/v2/projects/dwh-v2/datasets/_2f96775300d8858559d2bd23c05bad0392345e30/tables/anonda2cd79fe2c683f6e17ec63437a72c0e2144c829/data?maxResults=1024], body[None]
   f0_
0    3
```
  • Loading branch information
jdanbrown committed Mar 9, 2017
1 parent e9def4e commit c495300
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions datalab/utils/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import json
import urllib.request, urllib.parse, urllib.error
import httplib2
import logging


log = logging.getLogger(__name__)


# TODO(nikhilko): Start using the requests library instead.
Expand Down Expand Up @@ -128,6 +132,7 @@ def request(url, args=None, data=None, headers=None, method=None,

response = None
try:
log.debug('request: method[%(method)s], url[%(url)s], body[%(data)s]' % locals())
response, content = http.request(url,
method=method,
body=data,
Expand Down

0 comments on commit c495300

Please sign in to comment.