Skip to content
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
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ keywords = ["databricks", "arrow", "database", "sql"]
categories = ["database"]

[dependencies]
tracing = "0.1"
# The `log` feature makes `tracing::*` macros also emit `log::Record`s
# *when no tracing Subscriber is installed*. The pyo3 binding relies on
# this: it installs no Subscriber and instead routes those `log` records
# into Python's `logging` via `pyo3-log` (see `pyo3/src/logging.rs`).
# Direct-Rust / C-ABI consumers install the `fmt` Subscriber in
# `src/logging.rs` instead, which suppresses the `log` records — the two
# paths are mutually exclusive by construction.
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] }

arrow-array = "57"
Expand Down Expand Up @@ -93,6 +100,7 @@ dirs = "5" # Cross-platform config directory (~/.config/)
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
wiremock = "0.6"
tempfile = "3"
log = "0.4"

# TLS backend selection.
#
Expand Down
1 change: 1 addition & 0 deletions napi/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyo3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ arrow-pyarrow = "57"
# kernel uses for its `TypedValue::Date` / `TypedValue::Timestamp`
# variants — the parsed values flow directly into those.
chrono = "0.4"
pyo3-log = "0.13"
log = "0.4"
tracing = "0.1"
11 changes: 11 additions & 0 deletions pyo3/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ fn attach_attrs(
/// `err.code`, `err.message`, `err.sql_state`, etc.; `str(err)` is
/// the kernel's message text.
pub(crate) fn to_py_err(py: Python<'_>, err: KError) -> PyErr {
// Boundary-only breadcrumb: the kernel logged *what* failed; this
// records the kernel `Error` -> Python `KernelError` translation
// (and which code slug drove it), which is knowledge unique to the
// pyo3 layer. Helps explain "why did I get this exception type".
tracing::debug!(
target: crate::logging::PYO3_LOG_TARGET,
code = code_str(err.code),
retryable = err.retryable,
"mapping kernel error to KernelError: {}",
err.message
);
let py_err = KernelError::new_err(err.message.clone());
// Materialise the exception value so we can attach attributes.
let bound = py_err.value(py);
Expand Down
41 changes: 41 additions & 0 deletions pyo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mod auth;
mod errors;
mod executed_async;
mod fetch;
mod logging;
mod metadata;
mod params;
mod result_stream;
Expand All @@ -44,6 +45,35 @@ mod statement;

use pyo3::prelude::*;

/// Route kernel logs into Python's `logging`. Idempotent — safe to call
/// more than once (later calls are no-ops). Returns `True` if this call
/// performed the install, `False` if logging was already initialized (by
/// an earlier call or because the host process owns the `log` logger).
///
/// The connector calls this automatically when the kernel backend loads,
/// so `use_kernel=True` users need not call it themselves.
///
/// Records are forwarded under per-crate logger names: the kernel's own
/// events under `databricks.sql.kernel` (and the pyo3 boundary under
/// `databricks.sql.kernel.pyo3`), plus the underlying HTTP stack
/// (`hyper_util.client…`, `reqwest.connect`, `rustls`, …). Each is gated
/// by its Python logger's level, so deps stay quiet until a customer
/// raises them — e.g. `logging.getLogger("databricks.sql").setLevel(DEBUG)`
/// for everything, or silence the wire chatter with
/// `logging.getLogger("hyper_util").setLevel(WARNING)`.
#[pyfunction]
fn init_logging(py: Python<'_>) -> bool {
logging::init_logging(py)
}

/// Clear cached logger levels so a Python-side logging reconfiguration
/// applied *after* the first kernel log (e.g. raising
/// `databricks.sql.kernel` to DEBUG mid-session) takes effect.
#[pyfunction]
fn reset_logging() {
logging::reset_logging();
}

#[pymodule]
fn databricks_sql_kernel(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add("__version__", env!("CARGO_PKG_VERSION"))?;
Expand All @@ -54,5 +84,16 @@ fn databricks_sql_kernel(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<executed_async::ExecutedAsyncStatement>()?;
m.add_class::<metadata::Metadata>()?;
m.add_class::<result_stream::ResultStream>()?;
m.add_function(wrap_pyfunction!(init_logging, m)?)?;
m.add_function(wrap_pyfunction!(reset_logging, m)?)?;

// Install the log bridge at module import so *any* consumer of the
// bare wheel (the connector, integration tests, custom embedders)
// gets kernel logs in Python `logging` without an explicit
// `init_logging()` call. It's idempotent and never panics, so the
// connector's own call at kernel-backend load is a harmless no-op.
// Records still only surface once a customer raises the relevant
// logger's level, so this default-on install is silent until asked.
logging::init_logging(m.py());
Ok(())
}
Loading
Loading