Skip to content

Commit 6ad0646

Browse files
committed
Add a debugging flag
If --debug is used or if DATABRICKS_DEBUGGING is set in the environment the entire contents of the http requests will be printed to stdout.
1 parent a629bd5 commit 6ad0646

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

databricks_cli/configure/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ def callback(ctx, param, value): # NOQA
8383

8484
def _get_api_client(config, command_name=""):
8585
verify = config.insecure is None
86+
87+
if config.is_debugging and config.debugging == "1":
88+
ApiClient.enable_debug_logging()
89+
8690
if config.is_valid_with_token:
8791
return ApiClient(host=config.host, token=config.token, verify=verify,
8892
command_name=command_name)

databricks_cli/configure/provider.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
TOKEN = 'token'
3838
INSECURE = 'insecure'
3939
DEFAULT_SECTION = 'DEFAULT'
40+
DEBUG = 'debug'
4041

4142
# User-provided override for the DatabricksConfigProvider
4243
_config_provider = None
@@ -238,7 +239,11 @@ def get_config(self):
238239
password = os.environ.get('DATABRICKS_PASSWORD')
239240
token = os.environ.get('DATABRICKS_TOKEN')
240241
insecure = os.environ.get('DATABRICKS_INSECURE')
241-
config = DatabricksConfig(host, username, password, token, insecure)
242+
debugging = os.environ.get('DATABRICKS_DEBUGGING')
243+
if debugging is None:
244+
debugging = 0
245+
246+
config = DatabricksConfig(host, username, password, token, insecure, debugging=debugging)
242247
if config.is_valid:
243248
return config
244249
return None
@@ -256,14 +261,18 @@ def get_config(self):
256261
password = _get_option_if_exists(raw_config, self.profile, PASSWORD)
257262
token = _get_option_if_exists(raw_config, self.profile, TOKEN)
258263
insecure = _get_option_if_exists(raw_config, self.profile, INSECURE)
259-
config = DatabricksConfig(host, username, password, token, insecure)
264+
debugging = _get_option_if_exists(raw_config, self.profile, DEBUG)
265+
if debugging is None:
266+
debugging = 0
267+
268+
config = DatabricksConfig(host, username, password, token, insecure, debugging=debugging)
260269
if config.is_valid:
261270
return config
262271
return None
263272

264273

265274
class DatabricksConfig(object):
266-
def __init__(self, host, username, password, token, insecure): # noqa
275+
def __init__(self, host, username, password, token, insecure, debugging=0): # noqa
267276
self.host = host
268277
self.username = username
269278

@@ -274,6 +283,8 @@ def __init__(self, host, username, password, token, insecure): # noqa
274283

275284
self.token = token
276285
self.insecure = insecure
286+
self.debugging = debugging
287+
277288

278289
@classmethod
279290
def from_token(cls, host, token, insecure=None):
@@ -298,3 +309,7 @@ def is_valid_with_password(self):
298309
@property
299310
def is_valid(self):
300311
return self.is_valid_with_token or self.is_valid_with_password
312+
313+
@property
314+
def is_debugging(self):
315+
return self.debugging

databricks_cli/sdk/api_client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import base64
3232
import json
33+
import logging
3334
import warnings
3435
import requests
3536
import ssl
@@ -129,6 +130,27 @@ def perform_query(self, method, path, data = {}, headers = None):
129130
raise requests.exceptions.HTTPError(message, response=e.response)
130131
return resp.json()
131132

133+
@classmethod
134+
def enable_debug_logging(cls):
135+
# These two lines enable debugging at httplib level (requests->urllib3->http.client)
136+
# You will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
137+
# The only thing missing will be the response.body which is not logged.
138+
try:
139+
import http.client as http_client
140+
except ImportError:
141+
# Python 2
142+
import httplib as http_client
143+
144+
print ("HTTP debugging enabled")
145+
http_client.HTTPConnection.debuglevel = 1
146+
147+
# You must initialize logging, otherwise you'll not see debug output.
148+
logging.basicConfig()
149+
logging.getLogger().setLevel(logging.DEBUG)
150+
requests_log = logging.getLogger("requests.packages.urllib3")
151+
requests_log.setLevel(logging.DEBUG)
152+
requests_log.propagate = True
153+
132154

133155
def _translate_boolean_to_query_param(value):
134156
assert not isinstance(value, list), 'GET parameters cannot pass list of objects'

0 commit comments

Comments
 (0)