//! Ambient per-session tracing span for log routing. //! //! The `--debug` firehose router ([`crate::debug_log`]) fans events out to //! `~/.kigi/debug/.txt` by finding the enclosing session span's //! `session_id` field. [`with_session_ctx`] installs that span for the //! duration of a session's work. /// The `session_id` field name the debug-log firehose router keys on: /// `debug_log::SessionIdVisitor` stashes a `SessionId` extension on any span /// carrying this field — the span *name* is not load-bearing for routing. Shared /// so the `info_span!` here and the router in `debug_log` can't silently drift; a /// rename trips `session_span_exposes_router_field` below. pub(crate) const SESSION_ID_FIELD: &str = "session_id"; /// Build the per-session tracing span the firehose router routes by. The field /// name MUST be the literal `session_id` (tracing field names can't come from a /// const); the test below pins it against [`SESSION_ID_FIELD`]. fn session_span(session_id: &str) -> tracing::Span { tracing::info_span!("session", session_id = %session_id) } /// Run `fut` inside the per-session tracing span so the debug-log firehose /// routes its events to the session's file. pub async fn with_session_ctx(session_id: &str, fut: F) -> F::Output { use tracing::Instrument; fut.instrument(session_span(session_id)).await } #[cfg(test)] mod tests { use super::*; /// The debug-log firehose router (`debug_log`) finds the session span by its /// `session_id` field (not by name). That field name is a literal in /// `session_span` (tracing field names can't be a const), so pin it against the /// shared const here — a rename of either breaks this test instead of silently /// degrading routing to the per-pid fallback. #[test] fn session_span_exposes_router_field() { // A bare registry enables every callsite, so the span has live metadata. let subscriber = tracing_subscriber::registry(); tracing::subscriber::with_default(subscriber, || { let span = session_span("test-id"); let meta = span .metadata() .expect("session span must have metadata under an enabling subscriber"); assert!( meta.fields().field(SESSION_ID_FIELD).is_some(), "session span must expose `{SESSION_ID_FIELD}` for debug-log routing", ); }); } }