Skip to content

Commit 7fcfa7b

Browse files
author
Jesse
authored
Use a separate logger for unsafe thrift responses (databricks#153)
--------- Signed-off-by: Jesse Whitehouse <[email protected]>
1 parent 54e3769 commit 7fcfa7b

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 2.6.x (Unreleased)
44

5+
- Redact logged thrift responses by default
56
- Add support for OAuth on Databricks Azure
67

78
## 2.6.2 (2023-06-14)

src/databricks/sql/thrift_backend.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@
3434

3535
logger = logging.getLogger(__name__)
3636

37+
unsafe_logger = logging.getLogger("databricks.sql.unsafe")
38+
unsafe_logger.setLevel(logging.DEBUG)
39+
40+
# To capture these logs in client code, add a non-NullHandler.
41+
# See our e2e test suite for an example with logging.FileHandler
42+
unsafe_logger.addHandler(logging.NullHandler())
43+
44+
# Disable propagation so that handlers for `databricks.sql` don't pick up these messages
45+
unsafe_logger.propagate = False
46+
3747
THRIFT_ERROR_MESSAGE_HEADER = "x-thriftserver-error-message"
3848
DATABRICKS_ERROR_OR_REDIRECT_HEADER = "x-databricks-error-or-redirect-message"
3949
DATABRICKS_REASON_HEADER = "x-databricks-reason-phrase"
@@ -318,13 +328,25 @@ def attempt_request(attempt):
318328

319329
error, error_message, retry_delay = None, None, None
320330
try:
321-
logger.debug("Sending request: {}".format(request))
331+
# The MagicMocks in our unit tests have a `name` property instead of `__name__`.
332+
logger.debug(
333+
"Sending request: {}(<REDACTED>)".format(
334+
getattr(
335+
method, "__name__", getattr(method, "name", "UnknownMethod")
336+
)
337+
)
338+
)
339+
unsafe_logger.debug("Sending request: {}".format(request))
322340
response = method(request)
323341

324342
# Calling `close()` here releases the active HTTP connection back to the pool
325343
self._transport.close()
326344

327-
logger.debug("Received response: {}".format(response))
345+
# We need to call type(response) here because thrift doesn't implement __name__ attributes for thrift responses
346+
logger.debug(
347+
"Received response: {}(<REDACTED>)".format(type(response).__name__)
348+
)
349+
unsafe_logger.debug("Received response: {}".format(response))
328350
return response
329351

330352
except urllib3.exceptions.HTTPError as err:

tests/e2e/driver_tests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828

2929
log = logging.getLogger(__name__)
3030

31+
unsafe_logger = logging.getLogger("databricks.sql.unsafe")
32+
unsafe_logger.setLevel(logging.DEBUG)
33+
unsafe_logger.addHandler(logging.FileHandler("./tests-unsafe.log"))
34+
3135
# manually decorate DecimalTestsMixin to need arrow support
3236
for name in loader.getTestCaseNames(DecimalTestsMixin, 'test_'):
3337
fn = getattr(DecimalTestsMixin, name)

0 commit comments

Comments
 (0)