Skip to content

Use a separate logger for unsafe thrift responses #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 2.6.x (Unreleased)

- Redact logged thrift responses by default
- Add support for OAuth on Databricks Azure

## 2.6.2 (2023-06-14)
Expand Down
26 changes: 24 additions & 2 deletions src/databricks/sql/thrift_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@

logger = logging.getLogger(__name__)

unsafe_logger = logging.getLogger("databricks.sql.unsafe")
unsafe_logger.setLevel(logging.DEBUG)

# To capture these logs in client code, add a non-NullHandler.
# See our e2e test suite for an example with logging.FileHandler
unsafe_logger.addHandler(logging.NullHandler())

# Disable propagation so that handlers for `databricks.sql` don't pick up these messages
unsafe_logger.propagate = False

THRIFT_ERROR_MESSAGE_HEADER = "x-thriftserver-error-message"
DATABRICKS_ERROR_OR_REDIRECT_HEADER = "x-databricks-error-or-redirect-message"
DATABRICKS_REASON_HEADER = "x-databricks-reason-phrase"
Expand Down Expand Up @@ -318,13 +328,25 @@ def attempt_request(attempt):

error, error_message, retry_delay = None, None, None
try:
logger.debug("Sending request: {}".format(request))
# The MagicMocks in our unit tests have a `name` property instead of `__name__`.
logger.debug(
"Sending request: {}(<REDACTED>)".format(
getattr(
method, "__name__", getattr(method, "name", "UnknownMethod")
)
)
)
unsafe_logger.debug("Sending request: {}".format(request))
response = method(request)

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

logger.debug("Received response: {}".format(response))
# We need to call type(response) here because thrift doesn't implement __name__ attributes for thrift responses
logger.debug(
"Received response: {}(<REDACTED>)".format(type(response).__name__)
)
unsafe_logger.debug("Received response: {}".format(response))
return response

except urllib3.exceptions.HTTPError as err:
Expand Down
4 changes: 4 additions & 0 deletions tests/e2e/driver_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

log = logging.getLogger(__name__)

unsafe_logger = logging.getLogger("databricks.sql.unsafe")
unsafe_logger.setLevel(logging.DEBUG)
unsafe_logger.addHandler(logging.FileHandler("./tests-unsafe.log"))

# manually decorate DecimalTestsMixin to need arrow support
for name in loader.getTestCaseNames(DecimalTestsMixin, 'test_'):
fn = getattr(DecimalTestsMixin, name)
Expand Down