Repository navigation
fix(logging): honor LoggingConfig on the Rust Session::open path - #234
Conversation
`SessionBuilder::logging(LoggingConfig)` was accepted and stored on `SessionConfig::logging`, but nothing on the Rust `Session::open` path ever consumed it — the subscriber installer (`logging::init_logging`) was reachable only through the C ABI's `kernel_init_logging`. So a direct-Rust caller that configured a log destination got no log output and no error: a silent no-op (issue #213, CONNPROP-004). `Session::open` now installs the tracing subscriber from the caller's `LoggingConfig`, mirroring the C ABI path. The install is idempotent and process-wide (`OnceLock`, first-call-wins); a degraded outcome (log file unopenable, or a subscriber already installed by pyo3 / the host) is not propagated as an error, so it never fails `open()`. Two supporting changes: - `LogConfig` now carries a full `EnvFilter` directive (`filter`) plus a `PathBuf` destination, unifying the two entry points: the public `LoggingConfig.filter` is used verbatim, while the C ABI's bare level is expanded via `filter_from_level` (OFF handled at the C ABI, which returns Success without installing). A directory-form destination (an existing directory, or a trailing separator) resolves to a log file created inside it rather than being opened as a file, matching the reference driver's LogPath vs LogFile semantics on the single `file` field. - The `klog` `tracing`->`log` bridge tests move from `src/klog.rs` to a new `tests/klog_bridge.rs` integration binary. Those assertions require that NO tracing subscriber is installed in the process, but the lib test binary now installs one whenever a test opens a session (via the fix above), which suppresses the `log` bridge. A dedicated binary that opens no session keeps the bridge intact. Closes #213 Co-authored-by: Isaac Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
There was a problem hiding this comment.
Verdict: 1 Critical
One critical regression: Session::open calls init_logging unconditionally, so it installs a global tracing subscriber even with an empty LoggingConfig. Because the pyo3 binding opens sessions through this same path and relies on the tracing→log bridge (the crate uses tracing's log feature, not log-always), this silently breaks all kernel logging into Python logging for every pyo3 consumer — the exact init_logging-from-pyo3 hazard src/logging.rs documents. Gate the install on an explicitly-configured filter/file. The directory-resolution and test-relocation changes otherwise look sound.
| // unopenable, or a subscriber already installed by pyo3 / the host) | ||
| // is a logging degradation, not a reason to fail `open()`, so it is | ||
| // deliberately not propagated as an error. | ||
| let _ = crate::logging::init_logging(&crate::logging::LogConfig { |
There was a problem hiding this comment.
🔴 Critical — Session::open calls init_logging unconditionally, even when the caller configured no logging. With filter = None and file = None, init_logging still falls through to the RUST_LOG/warn default and calls try_init(), installing a process-global tracing subscriber.
The pyo3 binding opens sessions through this same KSession::open path (pyo3/src/session.rs:314), and it never sets a LoggingConfig — so every Python Session(...) now installs the kernel's tracing subscriber. Because the crate uses tracing's log feature (not log-always, Cargo.toml:34), installing any subscriber suppresses the tracing→log bridge that pyo3_log depends on. Result: all kernel logs stop reaching Python logging for every pyo3 consumer, and warn-level kernel events get dumped to stderr in Python processes.
This directly violates the invariant documented in src/logging.rs: the direct-Rust and pyo3 paths are "mutually exclusive" and "Do not call init_logging from the pyo3 crate." The PR wires init_logging into the shared Session::open, defeating that separation.
Secondary consequence on the C ABI: a caller who set level OFF (now handled in kernel_init_logging by returning Success without installing, leaving LOGGING_INITIALIZED unset) will still get a warn stderr subscriber installed by the very next Session::open, contradicting the OFF intent.
Fix: only install when the caller explicitly configured a destination or filter. When both are None, leave logging untouched so the pyo3 bridge (and the C-ABI OFF outcome) are preserved — this also matches pre-PR behavior for RUST_LOG-only direct-Rust callers.
…igured Addresses review on #234: `Session::open` called `init_logging` unconditionally, so an empty `LoggingConfig` (filter=None, file=None) still fell through to the RUST_LOG/warn default and installed a process-global tracing subscriber. The pyo3 binding opens sessions through this same path and never sets a `LoggingConfig`; installing any subscriber suppresses the `tracing`->`log` bridge `pyo3_log` relies on, silently breaking all kernel logging into Python. It also let a C ABI `kernel_init_logging(OFF, ...)` be overridden by a stray warn subscriber on the next `Session::open`. Gate the install on `filter.is_some() || file.is_some()` so an unconfigured `LoggingConfig` stays a true no-op, preserving the "direct-Rust vs pyo3 are mutually exclusive" invariant in `src/logging.rs`. New regression test `tests/klog_bridge.rs:: empty_logging_config_open_does_not_install_subscriber`: opens a session with default logging and asserts the `log` bridge is still live (a kernel-target event is still captured). Verified it fails without the gate. Co-authored-by: Isaac Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
There was a problem hiding this comment.
Verdict: 1 Medium
Solid, well-tested fix — Session::open now consumes LoggingConfig, the is_some() gate correctly preserves the pyo3 tracing→log bridge, and the klog bridge tests are relocated for good reason. One medium concern: the public LoggingConfig.filter doc example uses the underscore crate-name form (databricks_sql_kernel=debug), which — now that the filter is used verbatim as an EnvFilter directive — matches nothing against the kernel's ::-delimited target and reproduces the very silent no-op being fixed.
Other findings
- 🟡 Medium — The public
LoggingConfig.filterdoc example steers callers into the exact silent no-op this PR is fixing.
This field is now consumed verbatim as an EnvFilter directive on the direct-Rust path (src/logging.rs:155: EnvFilter::new(directive.clone())). The kernel emits on target KERNEL_LOG_TARGET = "databricks::sql::kernel" (::-delimited), and this PR's own code/comments emphasize that a single underscore token would match nothing (filter_from_level exists precisely to expand to the :: form; see the doc at src/logging.rs:99-104).
Yet the public doc example here reads:
/// Log filter, e.g. `databricks_sql_kernel=debug`. If `None`, the
A caller who copies that example verbatim — LoggingConfig { filter: Some("databricks_sql_kernel=debug".into()), file: Some(...) } — installs a subscriber whose only directive targets databricks_sql_kernel, which does not match the kernel's databricks::sql::kernel target. EnvFilter drops the events, the log file stays empty, and open() reports no error: the same silent no-op the PR set out to eliminate, just relocated from "filter never consumed" to "filter consumed but mismatched."
Before this PR the field was never read, so the misleading example was harmless; now it is a live trap. Recommend updating the example (and the fallback-default doc at src/config.rs:1087) to the correct ::-delimited form, e.g. databricks::sql::kernel=debug, matching the KERNEL_LOG_TARGET contract.
Addresses review on #234 (Medium): now that `LoggingConfig.filter` is consumed verbatim as an `EnvFilter` directive, the doc example's underscore crate-name form (`databricks_sql_kernel=debug`) is a live trap — it matches nothing against the kernel's `::`-delimited target `databricks::sql::kernel`, so the log file stays empty with no error: the same silent no-op this PR fixes, relocated to a mismatched filter. Correct the `LoggingConfig.filter` doc example (and document the `file` directory-resolution behavior), the `RUST_LOG` example in the logging module doc, and an internal `RUST_LOG` debug-hint comment to the `::`-delimited form matching the KERNEL_LOG_TARGET contract. Docs only; no behavior change. Co-authored-by: Isaac Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
There was a problem hiding this comment.
Verdict: 1 Low
Looks good — the core fix (wiring LoggingConfig into Session::open with a load-bearing is_some() gate to preserve the pyo3 tracing→log bridge) is correct, well-documented, and well-tested, and the klog bridge-test relocation is justified. One low-severity note: relocating the OFF handling to an early return in the C ABI subtly changes first-call-wins stickiness for OFF and bypasses the file_path validation, and leaves the LOGGING_INITIALIZED doc comment in logging.rs stale.
| .as_deref() | ||
| .is_some_and(|l| l.eq_ignore_ascii_case("off")) | ||
| { | ||
| return KernelStatusCode::Success; |
There was a problem hiding this comment.
🔵 Low — Moving the OFF handling out of init_logging and into an early return Success here changes two things worth noting:
-
OFFno longer participates in the once-per-process "first-call-wins" contract. Previouslyinit_logging(OFF)committedLOGGING_INITIALIZEDtotrue, so a laterkernel_init_logging("DEBUG", file)was a no-op. Now the OFF path returns before touchingLOGGING_INITIALIZED, so a subsequent call with a real level will install a subscriber. In the pure C-ABI flow theis_some()gate inSession::openkeeps OFF sticky for session opens, but two directkernel_init_loggingcalls (OFF then a real level) now behave differently than before. This is likely an improvement, but it's an unannounced semantic change. -
The OFF short-circuit now runs before the
file_pathUTF-8 validation that the surrounding comment explicitly calls "NOT best-effort". Previouslykernel_init_logging(OFF, <non-UTF-8 path>)surfaced the path error; now it returnsSuccessand never validates the path. Defensible (logging is off, path is moot), but it's a change from prior behavior.
Separately, the LOGGING_INITIALIZED doc comment in src/logging.rs ("...or intentionally skipped for level=OFF") is now stale — init_logging no longer has any OFF handling. Consider updating it to reflect that OFF is handled at the C ABI.
Summary
Fixes #213 (CONNPROP-004).
SessionBuilder::logging(LoggingConfig)was accepted and stored onSessionConfig::logging, but nothing on the RustSession::openpath ever consumed it — the subscriber installer (logging::init_logging) was reachable only through the C ABI'skernel_init_logging. A direct-Rust caller that configured a log destination got no log output and no error: a silent no-op.Session::opennow installs the tracing subscriber from the caller'sLoggingConfig, mirroring the C ABI path. The install is idempotent and process-wide (OnceLock, first-call-wins); a degraded outcome (log file unopenable, or a subscriber already installed by pyo3 / the host) is not propagated as an error, so it never failsopen().Root cause
Session::openreads everySessionConfiggroup (connection,auth,http,proxy,tls,result, …) exceptlogging. The only consumer oflogging::init_loggingwaskernel_init_loggingin the C ABI, so the Rust and PyO3/napi wrappers (which callSession::opendirectly) never installed a subscriber from config. (telemetryis a reserved empty struct andtransporthas a single variant, so those two remaining unread fields are intentional, not bugs.)What changed
Session::openinstalls logging fromLoggingConfig— the actual fix.LogConfigunified around a fullEnvFilterdirective (filter: Option<String>) plus aPathBufdestination. The publicLoggingConfig.filteris used verbatim; the C ABI's bare level is expanded via the newfilter_from_levelhelper. LevelOFFis now handled at the C ABI (returnsSuccesswithout installing), soinit_loggingno longer special-cases it.resolve_log_file): a destination that names an existing directory, or ends with a path separator, resolves to<dir>/databricks-sql-kernel.loginstead of being opened as a file. This mirrors the reference ODBC driver'sLogPath(directory) vsLogFile(file) semantics (reference PR databricks/databricks-odbc#178) on the kernel's singlefilefield, and matches the issue title ("resolves to a log file").klogbridge tests relocated fromsrc/klog.rsto a newtests/klog_bridge.rsintegration binary. Thosetracing→log-bridge assertions require that no tracing subscriber is installed in the process; the lib-test binary now installs one whenever a test opens a session (via the fix above, e.g. the c_abi retry test), which suppresses the bridge. Isolating them in a binary that opens no session keeps the bridge intact — following the guidance already documented in theklogtests' own CONSTRAINT note.Reference behavior (Step 3.5)
The behavior is defined by the reference ODBC driver (databricks/databricks-odbc#178):
LogFilenames a file,LogPathnames a directory (a log file is created inside it,LogFilewins when both set). The kernel exposes only a singlefilefield, so directory-vs-file is disambiguated at open time byresolve_log_file. The coverage PR's kernel test (databricks/databricks-driver-test#1105) asserts only the primary contract — a configured file destination is actually written — and documents the directory sub-cases as N/A for the kernel; this PR satisfies that and additionally makes the single field robust for the directory form.Test plan
tests/v0_logging_wiremock.rs::logging_config_file_destination_is_written— opens a session withLoggingConfig { filter, file }, runs a query against a wiremock SEA endpoint, and asserts the configured log file is non-empty. Verified it fails before the fix (size 0, silent no-op) and passes after. Also asserts a secondSession::openis a harmless no-op (idempotency through the public path).logging.rsunit tests:filter_from_level_targets_kernel_log_target,resolve_log_file_directory_resolves_inside,resolve_log_file_trailing_separator_resolves_inside,resolve_log_file_file_path_unchanged.klog_emits_contract_target/klog_propagates_correlation_fieldtotests/klog_bridge.rs(behaviorally identical — emit on the publicKERNEL_LOG_TARGET).cargo test(748 lib + all integration + doc tests) green;cargo fmt --allandcargo clippy --all-targets -- -D warningsclean;make c-abi-test(C ABI smoke, incl.kernel_init_logging) exit 0.Closes #213
This pull request and its description were written by Isaac.