Skip to content

feat(c-abi): add callback logging bridge - #278

Merged
vuanhphung merged 8 commits into
mainfrom
vu-phung/kernel-log-callback
Aug 24, 2026
Merged

vuanhphung merged 8 commits into
mainfrom
vu-phung/kernel-log-callback

Conversation

@vuanhphung

@vuanhphung vuanhphung commented Aug 20, 2026 •

Copy link
Copy Markdown
Collaborator

Adds a C ABI logging initializer that forwards Rust kernel tracing records through a caller-provided callback. This lets the Go driver route kernel records into its existing zerolog sink, so logger.SetLogOutput—including arbitrary io.Writer implementations and later destination changes—works the same on the Thrift and kernel paths. Go integration: databricks/databricks-sql-go#450.

This mirrors the existing Node implementation: its N-API binding installs a tracing layer, writes records to a bounded 4096-record queue, and drains them asynchronously through a ThreadsafeFunction into DBSQLLogger. The Go integration places the asynchronous handoff on the host side instead: this C callback delivers synchronously, then Go copies the borrowed strings into its own bounded 4096-record channel and drains them into zerolog. Neither bridge waits for a timer or a full batch before draining; Node batches records already available (up to 512), while Go forwards them one at a time.

Python does not need this C ABI entry point because PyO3 already uses pyo3_log to enter Python logging. Go reaches the kernel through the generic C ABI/cgo boundary, where the existing file/stderr initializer cannot represent its host logger.

Callback delivery is process-wide and first-call-wins: Success means level OFF was requested (which installs nothing) or this exact callback (keyed on callback + user_data) is the installed sink, and any initializer that loses — a file/stderr sink, a different callback, or a foreign subscriber — returns a distinct Internal rather than a misleading Success, so a host never assumes a dropped callback is live. The on_event bridge contains panics and guards against callback reentrancy. The message contains event-local fields; enclosing span fields are not included.

Tests: cargo +stable fmt --all -- --check; cargo clippy --all-targets -- -D warnings; cargo test; make c-abi-test.

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good — solid, well-tested callback logging bridge that correctly mirrors the existing kernel_init_logging OFF-level / first-call-wins semantics and shares LOGGING_INITIALIZED. One low-severity note: the callback layer drops enclosing-span fields that the file/stderr path surfaces, which slightly undercuts the docstring's "structured fields" claim.

Comment thread src/logging.rs Resolved

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good — a clean, well-documented C ABI callback logging bridge that correctly reuses the existing first-call-wins subscriber machinery, with matching Rust integration and C smoke tests. One low maintainability note (duplicated OFF/filter-derivation logic between the two init entry points). Nit: CallbackLogLayer::on_event allocates three CStrings per event on the emitting thread's hot path — acceptable given filtering gates volume, but worth noting if high-verbosity forwarding is expected.

Comment thread src/c_abi/session.rs Resolved

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good — the C ABI callback logging bridge is correct and well-covered. The nullable-fn-pointer FFI idiom, Send + Sync layer (user_data as usize), global filter placement, shared OFF/NULL/UTF-8 level resolution, NUL sanitization, and first-call-wins sharing via LOGGING_INITIALIZED are all sound, with unit + integration + C-smoke tests. One low-severity maintainability note inline (duplicated default-filter fallback). Nit: non-message string fields are rendered unquoted (name=value) by CallbackMessageVisitor, unlike the fmt layer which quotes them — harmless but a formatting divergence between the two sinks.

Comment thread src/logging.rs Resolved

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No issues identified by the review bot.

vuanhphung added a commit that referenced this pull request Aug 24, 2026
Address review findings on the callback logging bridge (#278):

- Replace the lossy `OnceLock<bool>` with a `LoggingOutcome` enum so a later
  initializer can tell whether its own sink actually won.
  `kernel_init_logging_callback` now returns `Internal` — not a misleading
  `Success` — when a file/stderr sink already won, so a host never believes a
  silently-dropped callback is live. File-open-failure, foreign-subscriber,
  and cross-sink cases now carry distinct, accurate messages (also fixing the
  prior "already installed" misreport after a log-file open error).
- Guard `CallbackLogLayer::on_event` with `catch_unwind` so a panic on a tokio
  worker can't unwind through tracing dispatch and abort in-flight work,
  mirroring the `catch_ffi` / `block_on_ffi` guards on the C-ABI entry points.
- Add a thread-local reentrancy guard so a callback that re-emits a kernel
  event drops the nested record instead of recursing without bound.

Enclosing-span fields (e.g. `session_id`) remain intentionally excluded from
the callback message, as documented.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
@vuanhphung
vuanhphung force-pushed the vu-phung/kernel-log-callback branch from a0827c6 to e5e1082 Compare August 24, 2026 02:34

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No issues identified by the review bot.

Address review findings on the callback logging bridge (#278):

- Replace the lossy `OnceLock<bool>` with a `LoggingOutcome` enum so a later
  initializer can tell whether *its own* sink actually won.
  `kernel_init_logging_callback` now returns `Internal` — not a misleading
  `Success` — whenever it did not install the sink: a file/stderr sink won
  first, or a *different* callback won first. The winning callback sink retains
  the callback + `user_data` identity, so a second, different registration is
  rejected (a host never believes a dropped callback is live) while
  re-registering the identical callback stays idempotent. File-open-failure,
  foreign-subscriber, and cross-sink cases each carry distinct, accurate
  messages (also fixing the prior "already installed" misreport after a
  log-file open error).
- Guard `CallbackLogLayer::on_event` with `catch_unwind` so a panic on a tokio
  worker can't unwind through tracing dispatch and abort in-flight work,
  mirroring the `catch_ffi` / `block_on_ffi` guards on the C-ABI entry points.
- Add a thread-local reentrancy guard so a callback that re-emits a kernel
  event drops the nested record instead of recursing without bound.

Enclosing-span fields (e.g. `session_id`) remain intentionally excluded from
the callback message, as documented.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good — solid, well-tested C ABI callback logging bridge. The LoggingOutcome/LoggingSink refactor preserves first-call-wins semantics, default_env_filter is behavior-equivalent to the prior inline default, and the on_event panic + reentrancy guards are correct (Drop-based reset survives unwind). One Low: the callback initializer's documented "Success means this callback is installed" contract doesn't account for the OFF-level path, which returns Success without installing anything.

Comment thread include/databricks_kernel.h Resolved
…back

`kernel_init_logging_callback` short-circuits on level `OFF` and returns
`Success` without installing any callback sink (mirroring `kernel_init_logging`).
The callback contract said "`Success` means this exact callback is the installed
sink", omitting the OFF case that `kernel_init_logging`'s own docs already note.
Add the "(or level OFF)" clause to the C header and the Rust doc-comment so the
callback contract is as precise as the file/stderr one.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No issues identified by the review bot.

Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No issues identified by the review bot.

@vuanhphung
vuanhphung added this pull request to the merge queue Aug 24, 2026
Merged via the queue into main with commit fada7c8 Aug 24, 2026
18 of 19 checks passed

This branch was previously deployed

1 inactive deployment
azure-prod — f4c3556d Deployed Aug 24, 2026 by vuanhphung via followup #965
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants